-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhide-html-code.html
More file actions
63 lines (56 loc) · 2.08 KB
/
hide-html-code.html
File metadata and controls
63 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<script type="text/javascript">
// Hide code blocks that start with %%html
(function() {
function hideHtmlBlocks() {
// Try multiple selectors in case Quarto structure varies
const selectors = [
'div.sourceCode.cell-code',
'div.cell-code.sourceCode',
'.cell-code'
];
let codeBlocks = [];
for (const selector of selectors) {
codeBlocks = document.querySelectorAll(selector);
if (codeBlocks.length > 0) break;
}
codeBlocks.forEach(block => {
const code = block.querySelector('code.sourceCode') || block.querySelector('code');
if (code) {
const textContent = code.textContent || code.innerText || '';
if (textContent.trim().startsWith('%%html')) {
// Hide the code block
block.style.display = 'none';
block.classList.add('hide-code-block');
// The toggle is actually a <details> element wrapping the code block
// Find the parent <details> element (it has class "code-fold")
let details = block.closest('details.code-fold');
if (!details) {
// Alternative: find parent that contains the summary with "Code Toggle"
let parent = block.parentElement;
while (parent && parent !== document.body) {
const summary = parent.querySelector('summary');
if (summary && (summary.textContent.includes('Code Toggle') || summary.textContent.includes('Toggle'))) {
details = parent;
break;
}
parent = parent.parentElement;
}
}
if (details) {
details.style.display = 'none';
details.classList.add('hide-code-block');
}
}
}
});
}
// Run immediately if DOM is ready, otherwise wait
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', hideHtmlBlocks);
} else {
hideHtmlBlocks();
}
// Also run after a short delay in case content loads dynamically
setTimeout(hideHtmlBlocks, 100);
})();
</script>