')
.replace(/\*\*(.*?)\*\*/g, '$1')
.replace(/\n- /g, '\n• ')
.replace(/\n/g, ' ');
let html = `
${data.title}
${diffStars}${data.weight || 'medium'} weight
${notesHtml}
Key Facts for Quick Revision
${factsHtml}
${pyqYears ? `
PYQ Years: ${pyqYears}
` : ''}
${crossLinks ? `
Related Topics: ${crossLinks}
` : ''}
`;
container.innerHTML = html;
apiRenderedNotes[topicId] = html;
}
// --- Override switch functions to use API when available ---
const origSwitchMCQ = window.switchMCQ;
window.switchMCQ = function(topic, btn) {
// Hide all containers
document.querySelectorAll('.mcq-container').forEach(c => c.classList.remove('active'));
document.querySelectorAll('.mcq-topic-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// Check if API has data for this topic
const hasAPI = apiLoaded && apiTopics.some(t => CATEGORY_MAP[t.category] === topic);
if (hasAPI) {
// Hide old hardcoded version if visible
const oldDiv = document.getElementById('mcq-' + topic);
if (oldDiv) oldDiv.classList.remove('active');
renderAPIMCQSection(topic);
} else {
// Fall back to hardcoded
const apiDiv = document.getElementById('mcq-api-' + topic);
if (apiDiv) apiDiv.classList.remove('active');
renderMCQ(topic);
const el = document.getElementById('mcq-' + topic);
if (el) el.classList.add('active');
}
};
const origSwitchNotes = window.switchNotes;
window.switchNotes = function(topic, btn) {
document.querySelectorAll('.notes-container').forEach(c => c.classList.remove('active'));
document.querySelectorAll('.notes-topic-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
const hasAPI = apiLoaded && apiTopics.some(t => CATEGORY_MAP[t.category] === topic);
if (hasAPI) {
const oldDiv = document.getElementById('notes-' + topic);
if (oldDiv) oldDiv.classList.remove('active');
renderAPINotesSection(topic);
} else {
const apiDiv = document.getElementById('notes-api-' + topic);
if (apiDiv) apiDiv.classList.remove('active');
renderNotes(topic);
const el = document.getElementById('notes-' + topic);
if (el) el.classList.add('active');
}
};
// Load API data on page load and refresh the active tabs
loadAPITopics().then(() => {
// Re-render ancient history (default tab) with API data
if (apiLoaded) {
// Remove old hardcoded ancient MCQ
const oldMCQ = document.getElementById('mcq-ancient');
if (oldMCQ) oldMCQ.classList.remove('active');
renderAPIMCQSection('ancient');
// Remove old hardcoded ancient notes
const oldNotes = document.getElementById('notes-ancient');
if (oldNotes) oldNotes.classList.remove('active');
renderAPINotesSection('ancient');
}
});