Skip to content

Commit a542937

Browse files
authored
Merge pull request #9 from santoshxshrestha/hilight
feat: highlighting
2 parents ab87b4a + fa4eda3 commit a542937

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
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");

static/global.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ code {
133133

134134
pre {
135135
background: var(--code-bg);
136-
padding: 16px;
136+
padding: 2px;
137137
overflow: auto;
138138
border-radius: 6px;
139139
color: var(--code-color);

templates/main.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
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.11.1/styles/github-dark.min.css"
17+
integrity="sha512-rO+olRTkcf304DQBxSWxln8JXCzTHlKnIdnMUwYvQa9/Jd4cQaNkItIUj6Z4nvW1dqK0SKXLbn9h4KwZTNtAyw=="
18+
crossorigin="anonymous"
19+
referrerpolicy="no-referrer"
20+
/>
21+
<link
22+
id="hljs-theme-light"
23+
rel="stylesheet"
24+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/github.min.css"
25+
integrity="sha512-0aPQyyeZrWj9sCA46UlmWgKOP0mUipLQ6OZXu8l4IcAmD2u31EPEy9VcIMvl7SoAaKe8bLXZhYoMaE/in+gcgA=="
26+
crossorigin="anonymous"
27+
referrerpolicy="no-referrer"
28+
disabled
29+
/>
30+
<script
31+
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"
32+
integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw=="
33+
crossorigin="anonymous"
34+
referrerpolicy="no-referrer"
35+
defer
36+
></script>
37+
1338
<title>{{ title }} - mdwatch</title>
1439
<style>
1540
{{ style | safe }}

0 commit comments

Comments
 (0)