-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin updating codeblocks (update js and fix line numbers)
- Loading branch information
1 parent
d660f2b
commit 653be29
Showing
4 changed files
with
70 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
// Based on https://brandur.org/fragments/shiki | ||
|
||
import { codeToHtml } from "https://esm.sh/shiki@1.10.0"; | ||
import { codeToHtml } from "https://esm.sh/shiki@1.22.0"; | ||
|
||
const htmlEscapes = new Map([ | ||
["&", "&"], | ||
["<", "<"], | ||
[">", ">"], | ||
[""", '"'], | ||
["'", "'"], | ||
]); | ||
|
||
function unescapeHTMLEntities(str) { | ||
for (const [escaped, unescaped] of htmlEscapes) { | ||
str = str.replaceAll(escaped, unescaped); | ||
} | ||
return str; | ||
return str.replace(/&(?:amp|lt|gt|quot|#39);/g, (match) => htmlEscapes.get(match)); | ||
} | ||
|
||
const processCodeBlock = async (codeBlock) => { | ||
const classNames = codeBlock.getAttribute("class"); | ||
if (!classNames) return; | ||
|
||
const language = classNames | ||
.split(" ") | ||
.map((c) => c.split("-")) | ||
.find(([prefix]) => prefix === "language") | ||
?.slice(1) | ||
.join("-") | ||
.toLowerCase(); | ||
const getLanguageFromClassNames = (classNames) => { | ||
if (!classNames) return null; | ||
const languageClass = classNames.split(" ").find((c) => c.startsWith("language-")); | ||
return languageClass ? languageClass.slice(9).toLowerCase() : null; | ||
}; | ||
|
||
const processCodeBlock = async (codeBlock) => { | ||
const language = getLanguageFromClassNames(codeBlock.className); | ||
if (!language) return; | ||
|
||
const code = unescapeHTMLEntities(codeBlock.innerHTML); | ||
codeBlock.parentElement.outerHTML = await codeToHtml(code, { | ||
const code = unescapeHTMLEntities(codeBlock.textContent.trim()); | ||
const highlightedCode = await codeToHtml(code, { | ||
lang: language, | ||
theme: "vitesse-dark", | ||
}); | ||
|
||
const tempElement = document.createElement("div"); | ||
tempElement.innerHTML = highlightedCode; | ||
codeBlock.parentElement.replaceWith(tempElement.firstElementChild); | ||
}; | ||
|
||
const highlightAllCodeBlocks = async () => { | ||
const codeBlocks = document.querySelectorAll("pre code"); | ||
await Promise.all(Array.from(codeBlocks).map(processCodeBlock)); | ||
}; | ||
|
||
await Promise.all(Array.from(document.querySelectorAll("pre code")).map(processCodeBlock)); | ||
if (document.readyState === "loading") { | ||
document.addEventListener("DOMContentLoaded", highlightAllCodeBlocks); | ||
} else { | ||
await highlightAllCodeBlocks(); | ||
} |
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,39 +1,46 @@ | ||
// Based on https://brandur.org/fragments/shiki | ||
|
||
import { codeToHtml } from "https://esm.sh/shiki@1.10.0"; | ||
import { codeToHtml } from "https://esm.sh/shiki@1.22.0"; | ||
|
||
const htmlEscapes = new Map([ | ||
["&", "&"], | ||
["<", "<"], | ||
[">", ">"], | ||
[""", '"'], | ||
["'", "'"], | ||
]); | ||
|
||
function unescapeHTMLEntities(str) { | ||
for (const [escaped, unescaped] of htmlEscapes) { | ||
str = str.replaceAll(escaped, unescaped); | ||
} | ||
return str; | ||
return str.replace(/&(?:amp|lt|gt|quot|#39);/g, (match) => htmlEscapes.get(match)); | ||
} | ||
|
||
const processCodeBlock = async (codeBlock) => { | ||
const classNames = codeBlock.getAttribute("class"); | ||
if (!classNames) return; | ||
|
||
const language = classNames | ||
.split(" ") | ||
.map((c) => c.split("-")) | ||
.find(([prefix]) => prefix === "language") | ||
?.slice(1) | ||
.join("-") | ||
.toLowerCase(); | ||
const getLanguageFromClassNames = (classNames) => { | ||
if (!classNames) return null; | ||
const languageClass = classNames.split(" ").find((c) => c.startsWith("language-")); | ||
return languageClass ? languageClass.slice(9).toLowerCase() : null; | ||
}; | ||
|
||
const processCodeBlock = async (codeBlock) => { | ||
const language = getLanguageFromClassNames(codeBlock.className); | ||
if (!language) return; | ||
|
||
const code = unescapeHTMLEntities(codeBlock.innerHTML); | ||
codeBlock.parentElement.outerHTML = await codeToHtml(code, { | ||
const code = unescapeHTMLEntities(codeBlock.textContent.trim()); | ||
const highlightedCode = await codeToHtml(code, { | ||
lang: language, | ||
theme: "vitesse-dark", | ||
}); | ||
|
||
const tempElement = document.createElement("div"); | ||
tempElement.innerHTML = highlightedCode; | ||
codeBlock.parentElement.replaceWith(tempElement.firstElementChild); | ||
}; | ||
|
||
const highlightAllCodeBlocks = async () => { | ||
const codeBlocks = document.querySelectorAll("pre code"); | ||
await Promise.all(Array.from(codeBlocks).map(processCodeBlock)); | ||
}; | ||
|
||
await Promise.all(Array.from(document.querySelectorAll("pre code")).map(processCodeBlock)); | ||
if (document.readyState === "loading") { | ||
document.addEventListener("DOMContentLoaded", highlightAllCodeBlocks); | ||
} else { | ||
await highlightAllCodeBlocks(); | ||
} |
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