Skip to content

armgit / pdfcn

Public Imported from GitHub
0

Beautiful pdf components, built on Takumi and Forme. 100% Free, Zero config, one command setup.

fix: remove node:crypto dependency from highlight-code

Aniket-508dacb4a8bcc70
diff --git a/lib/highlight-code.ts b/lib/highlight-code.ts
index c2ca4bd..7bef1fa 100644
--- a/lib/highlight-code.ts
+++ b/lib/highlight-code.ts
@@ -1,5 +1,3 @@
-import { createHash } from "node:crypto";
-
import { LRUCache } from "lru-cache";
import type { ShikiTransformer } from "shiki";
import { codeToHtml } from "shiki";
@@ -9,6 +7,14 @@ const highlightCache = new LRUCache<string, string>({
ttl: 1000 * 60 * 60,
});
+const hash = (input: string) => {
+ let h = 0;
+ for (let i = 0; i < input.length; i += 1) {
+ h = Math.trunc(Math.imul(31, h) + input.codePointAt(i));
+ }
+ return h.toString(36);
+};
+
export const transformers = [
{
code(node) {
@@ -48,9 +54,7 @@ export const transformers = [
] as ShikiTransformer[];
export const highlightCode = async (code: string, language = "tsx") => {
- const cacheKey = createHash("sha256")
- .update(`${language}:${code}`)
- .digest("hex");
+ const cacheKey = hash(`${language}:${code}`);
const cached = highlightCache.get(cacheKey);
if (cached) {