Skip to content

Commit

Permalink
Add syntax highlighting html rewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Oct 4, 2024
1 parent 8be58c4 commit c360697
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
67 changes: 66 additions & 1 deletion packages/astro-md/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@ import { myRemark } from "../my-remark";
import { remarkObsidian } from "../remark-obsidian";
import remarkFrontmatter from "remark-frontmatter";

const decode = (str: string) =>
str.replace(/&#x(\d+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));

const theme = "nord";

class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
private lang: string = "";
private code: string = "";
cache: KVNamespace;

constructor(private runtime: Runtime["runtime"]) {
this.cache = runtime.env.KV_HIGHLIGHT;
}

element(element: Element) {
this.lang = element.getAttribute("data-lang") ?? "";
this.code = "";
}
async text(text: Text) {
this.code += decode(text.text);
if (text.lastInTextNode) {
const hashBuffer = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(`${theme}-${this.lang}-${this.code}`)
);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");

const cachedResult = await this.cache.get(hash);
if (cachedResult) {
text.replace(cachedResult, { html: true });
return;
}

const res = await fetch(
`https://highlight.val.just-be.dev?lang=${this.lang}&theme=${theme}`,
{
method: "POST",
body: this.code,
headers: {
Accept: "text/html",
},
}
);
const data = await res.text();
this.runtime.ctx.waitUntil(this.cache.put(hash, data));
text.replace(data, { html: true });
} else {
text.remove();
}
}
}

export const onRequest = defineMiddleware(async (context, next) => {
const fileResolver = async (path: string) => {
return (await context.locals.runtime.env.KV_MAPPINGS.get(path)) ?? path;
Expand All @@ -18,5 +73,15 @@ export const onRequest = defineMiddleware(async (context, next) => {
],
});
context.locals.render = processor.render;
return next();
const res = await next();
if (import.meta.env.DEV && typeof HTMLRewriter === "undefined") {
// @ts-expect-error Isn't defined on globalThis, but that's fine
globalThis.HTMLRewriter = (
await import("@worker-tools/html-rewriter/base64")
).HTMLRewriter;
}

return new HTMLRewriter()
.on("code[data-lang]", new SyntaxHighlightRewriter(context.locals.runtime))
.transform(res);
});
3 changes: 3 additions & 0 deletions packages/astro-md/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"dependencies": {
"@astrojs/markdown-remark": "catalog:",
"remark-frontmatter": "^5.0.0"
},
"devDependencies": {
"@worker-tools/html-rewriter": "0.1.0-pre.19"
}
}
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Generated by Wrangler on Wed Aug 21 2024 20:59:22 GMT-0400 (Eastern Daylight Time)
// by running `wrangler types`
// Generated by Wrangler by running `wrangler types`

interface Env {
KV_MAPPINGS: KVNamespace;
KV_HIGHLIGHT: KVNamespace;
NODE_VERSION: "v20.16.0";
PNPM_VERSION: "v9.7.0";
SITE: string;
Expand Down
4 changes: 4 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ PNPM_VERSION = "v9.7.0"
binding = "KV_MAPPINGS"
id = "36e4319084c4498fa08e5bf00f36331a"

[[kv_namespaces]]
binding = "KV_HIGHLIGHT"
id = "9503a7c1e6f948cb8f1754c08e046179"

[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "just-be-dev"
Expand Down

0 comments on commit c360697

Please sign in to comment.