Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache based on theme #187

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
const codeData = element.children[0]?.data as ElementData | undefined;

element.children = [tree]
.map((tree) => {

Check warning on line 66 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint/Typecheck/Build/Test

Excessive complexity detected.
const pre = tree.children[0];
const themeNames = getThemeNames(theme);
const themeNamesString = themeNames.join(' ');
Expand Down Expand Up @@ -162,7 +162,7 @@
.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 @@
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 @@
: Object.values(theme),
langs: ['plaintext'],
});
globalHighlighterCache.set(options, cachedHighlighter);
globalHighlighterCache.set(key, cachedHighlighter);
}

const defaultCodeBlockLang =
Expand Down Expand Up @@ -226,7 +228,7 @@
const highlighter = await cachedHighlighter;
if (!highlighter) return;

visit(tree, 'element', (element, _, parent) => {

Check warning on line 231 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint/Typecheck/Build/Test

Excessive complexity detected.
if (isInlineCode(element, parent)) {
const textElement = element.children[0];
if (!isText(textElement)) return;
Expand Down Expand Up @@ -270,7 +272,7 @@
console.error(e);
}

visit(tree, 'element', (element, _, parent) => {

Check warning on line 275 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint/Typecheck/Build/Test

Excessive complexity detected.
if (isInlineCode(element, parent)) {
const textElement = element.children[0];
if (!isText(textElement)) return;
Expand Down Expand Up @@ -422,7 +424,7 @@
counterMap: new Map<string, number>(),
};

visit(codeTree, 'element', (element) => {

Check warning on line 427 in src/index.ts

View workflow job for this annotation

GitHub Actions / Lint/Typecheck/Build/Test

Excessive complexity detected.
if (
element.tagName === 'code' &&
/srebmuNeniLwohs(?!(.*)(\/))/.test(reverseString(meta))
Expand Down
Loading