Skip to content

Commit 09b2aed

Browse files
committed
highlight: added the cdn for the highlighter
1 parent ae7ca7d commit 09b2aed

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

static/client.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ function setThemeIcon(theme) {
77
: '<i class="fa-solid fa-moon"></i>';
88
}
99

10+
function highlightBlocks(root = document) {
11+
if (!window.hljs) return;
12+
root.querySelectorAll("pre code").forEach((block) => {
13+
hljs.highlightElement(block);
14+
});
15+
}
16+
17+
function syncHighlightTheme(theme) {
18+
const dark = document.getElementById("hljs-theme-dark");
19+
const light = document.getElementById("hljs-theme-light");
20+
if (!dark || !light) return;
21+
if (theme === "light") {
22+
dark.disabled = true;
23+
light.disabled = false;
24+
} else {
25+
dark.disabled = false;
26+
light.disabled = true;
27+
}
28+
}
29+
1030
function toggleTheme() {
1131
const html = document.documentElement;
1232
const isLight = html.getAttribute("data-theme") === "light";
@@ -15,10 +35,12 @@ function toggleTheme() {
1535
html.removeAttribute("data-theme");
1636
localStorage.setItem("theme", "dark");
1737
setThemeIcon("dark");
38+
syncHighlightTheme("dark");
1839
} else {
1940
html.setAttribute("data-theme", "light");
2041
localStorage.setItem("theme", "light");
2142
setThemeIcon("light");
43+
syncHighlightTheme("light");
2244
}
2345
}
2446

@@ -30,9 +52,13 @@ document.addEventListener("DOMContentLoaded", function () {
3052
if (savedTheme === "light") {
3153
html.setAttribute("data-theme", "light");
3254
setThemeIcon("light");
55+
syncHighlightTheme("light");
3356
} else {
3457
setThemeIcon("dark");
58+
syncHighlightTheme("dark");
3559
}
60+
61+
highlightBlocks();
3662
});
3763

3864
// Add smooth scrolling for anchor links
@@ -60,6 +86,7 @@ ws.onmessage = (event) => {
6086
const date = new Date();
6187
console.log(`updating content: ${date.toLocaleTimeString()}`);
6288
content.innerHTML = event.data;
89+
highlightBlocks(content);
6390
};
6491

6592
ws.onclose = () => console.log("closed");

templates/main.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@
1010
crossorigin="anonymous"
1111
referrerpolicy="no-referrer"
1212
/>
13+
<link
14+
id="hljs-theme-dark"
15+
rel="stylesheet"
16+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css"
17+
/>
18+
<link
19+
id="hljs-theme-light"
20+
rel="stylesheet"
21+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css"
22+
disabled
23+
/>
24+
<script
25+
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"
26+
defer
27+
></script>
1328
<title>{{ title }} - mdwatch</title>
1429
<style>
1530
{{ style | safe }}

0 commit comments

Comments
 (0)