Skip to content

Commit

Permalink
fix: cache based on theme (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Mar 29, 2024
1 parent 0782823 commit 50e9a32
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function apply(
.flatMap((c) => c);
}

const globalHighlighterCache = new WeakMap<Options, Promise<Highlighter>>();
const globalHighlighterCache = new Map<string, Promise<Highlighter>>();
const hastParser = unified().use(rehypeParse, { fragment: true });

export default function rehypePrettyCode(
Expand All @@ -184,7 +184,9 @@ export default function rehypePrettyCode(
onVisitCaption,
} = options;

let cachedHighlighter = globalHighlighterCache.get(options);
const key = JSON.stringify(theme);

let cachedHighlighter = globalHighlighterCache.get(key);
if (!cachedHighlighter) {
cachedHighlighter = getHighlighter({
themes:
Expand All @@ -193,7 +195,7 @@ export default function rehypePrettyCode(
: Object.values(theme),
langs: ['plaintext'],
});
globalHighlighterCache.set(options, cachedHighlighter);
globalHighlighterCache.set(key, cachedHighlighter);
}

const defaultCodeBlockLang =
Expand Down

0 comments on commit 50e9a32

Please sign in to comment.