-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
178b532
commit 20d7a8f
Showing
3 changed files
with
20 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import { codeToHtml } from 'https://esm.sh/[email protected]' | ||
|
||
export default async function () { | ||
const langs = { | ||
javascript: 'js', | ||
js: 'js', | ||
html: 'html', | ||
} | ||
|
||
const highlighter = await shiki.getHighlighter({ | ||
theme: 'css-variables', | ||
langs: ['js', 'html', 'shell'], | ||
}) | ||
const codeBlocks = document.querySelectorAll('pre code[class*="language-"]') | ||
|
||
codeBlocks.forEach((block) => { | ||
const lang = block.className.replace('language-', '') | ||
const html = highlighter.codeToHtml(block.textContent, { | ||
lang: langs[lang] || lang, | ||
}) | ||
block.innerHTML = html | ||
const observer = new window.MutationObserver(theme) | ||
observer.observe(document.documentElement, { | ||
attributes: true, | ||
subtree: false, | ||
}) | ||
function theme() { | ||
const colorMode = document.documentElement.getAttribute('data-theme') | ||
codeBlocks.forEach(async (block) => { | ||
const lang = block.className.replace('language-', '') | ||
const html = await codeToHtml(block.textContent, { | ||
theme: colorMode === 'light' ? 'github-light' : 'monokai', | ||
lang: langs[lang] || lang, | ||
}) | ||
block.innerHTML = html | ||
}) | ||
} | ||
theme() | ||
} |