Skip to content

Commit

Permalink
Begin updating codeblocks (update js and fix line numbers)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeclanChidlow committed Oct 17, 2024
1 parent d660f2b commit 653be29
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 64 deletions.
47 changes: 27 additions & 20 deletions docs/scripts/syntax-highlighting.js
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([
["&", "&"],
["&lt;", "<"],
["&gt;", ">"],
["&quot;", '"'],
["&#39;", "'"],
]);

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();
}
20 changes: 8 additions & 12 deletions docs/styles/type/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pre {

code {
background: none;
padding: 0;
}
}

Expand All @@ -25,17 +26,12 @@ code {
counter-reset: step;
counter-increment: step 0;

.line {
&::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
}
&:last-child::before {
content: none;
}
.line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
}
}
47 changes: 27 additions & 20 deletions input/global/scripts/syntax-highlighting.js
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([
["&amp;", "&"],
["&lt;", "<"],
["&gt;", ">"],
["&quot;", '"'],
["&#39;", "'"],
]);

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();
}
20 changes: 8 additions & 12 deletions input/global/styles/type/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pre {

code {
background: none;
padding: 0;
}
}

Expand All @@ -25,17 +26,12 @@ code {
counter-reset: step;
counter-increment: step 0;

.line {
&::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
}
&:last-child::before {
content: none;
}
.line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
}
}

0 comments on commit 653be29

Please sign in to comment.