Skip to content

Commit 7e5b177

Browse files
chore: cleanup syntax highlighting plugin code
1 parent 1b92f7e commit 7e5b177

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type RehypeSyntaxHighlightingOptions = {
3232
theme?: ShikiTheme;
3333
themes?: Record<'light' | 'dark', ShikiTheme>;
3434
codeStyling?: 'dark' | 'system';
35+
linkMap?: Map<string, string>;
3536
};
3637

3738
let highlighterPromise: Promise<Highlighter> | null = null;
@@ -50,7 +51,8 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
5051
options = {}
5152
) => {
5253
return async (tree) => {
53-
const asyncNodesToProcess: Promise<void>[] = [];
54+
const nodesToProcess: Promise<void>[] = [];
55+
5456
const themesToLoad: ShikiTheme[] = [];
5557
if (options.themes) {
5658
themesToLoad.push(options.themes.dark);
@@ -97,30 +99,33 @@ export const rehypeSyntaxHighlighting: Plugin<[RehypeSyntaxHighlightingOptions?]
9799
getLanguage(child, DEFAULT_LANG_ALIASES) ??
98100
DEFAULT_LANG;
99101

100-
asyncNodesToProcess.push(
102+
nodesToProcess.push(
101103
(async () => {
102104
await cdnTwoslash.prepareTypes(toString(node));
103-
if (!DEFAULT_LANGS.includes(lang)) {
104-
await highlighter.loadLanguage(lang);
105-
traverseNode(node, index, parent, highlighter, lang, options);
106-
} else {
107-
traverseNode(node, index, parent, highlighter, lang, options);
108-
}
105+
if (!DEFAULT_LANGS.includes(lang)) await highlighter.loadLanguage(lang);
106+
traverseNode({ node, index, parent, highlighter, lang, options });
109107
})()
110108
);
111109
});
112-
await Promise.all(asyncNodesToProcess);
110+
await Promise.all(nodesToProcess);
113111
};
114112
};
115113

116-
const traverseNode = (
117-
node: Element,
118-
index: number,
119-
parent: Element | Root | MdxJsxTextElementHast | MdxJsxFlowElementHast,
120-
highlighter: Highlighter,
121-
lang: ShikiLang,
122-
options: RehypeSyntaxHighlightingOptions
123-
) => {
114+
function traverseNode({
115+
node,
116+
index,
117+
parent,
118+
highlighter,
119+
lang,
120+
options,
121+
}: {
122+
node: Element;
123+
index: number;
124+
parent: Element | Root | MdxJsxTextElementHast | MdxJsxFlowElementHast;
125+
highlighter: Highlighter;
126+
lang: ShikiLang;
127+
options: RehypeSyntaxHighlightingOptions;
128+
}) {
124129
try {
125130
let code = toString(node);
126131

@@ -133,7 +138,7 @@ const traverseNode = (
133138
node.data.meta = meta.join(' ').trim() || undefined;
134139
}
135140

136-
const linkMap: Map<string, string> = new Map();
141+
const linkMap = options.linkMap ?? new Map();
137142
const splitCode = code.split('\n');
138143
for (const [i, line] of splitCode.entries()) {
139144
const parsedLineComment = parseLineComment(line);
@@ -186,6 +191,6 @@ const traverseNode = (
186191
}
187192
throw err;
188193
}
189-
};
194+
}
190195

191196
export { UNIQUE_LANGS, DEFAULT_LANG_ALIASES, SHIKI_THEMES, ShikiLang, ShikiTheme };

0 commit comments

Comments
 (0)