Skip to content

Commit 49c54b1

Browse files
committed
feat(shiki): allow custom highlighters
1 parent e753dd2 commit 49c54b1

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

packages/rehype-shiki/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@node-core/rehype-shiki",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"type": "module",
55
"exports": {
66
".": "./src/index.mjs",

packages/rehype-shiki/src/highlighter.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ export const getLanguageByName = (language, langs) => {
1818
);
1919
};
2020

21+
/**
22+
* @typedef {Object} SyntaxHighlighter
23+
* @property {import('@shikijs/core').HighlighterCoreSync} shiki - The underlying shiki core instance.
24+
* @property {(code: string, lang: string, meta?: Record<string, any>) => string} highlightToHtml - Highlights code and returns inner HTML of the <code> tag.
25+
* @property {(code: string, lang: string, meta?: Record<string, any>) => any} highlightToHast - Highlights code and returns a HAST tree.
26+
*/
27+
2128
/**
2229
* Factory function to create a syntax highlighter instance with utility methods.
2330
*
2431
* @param {Object} params - Parameters for highlighter creation.
2532
* @param {import('@shikijs/core').HighlighterCoreOptions} [params.coreOptions] - Core options for the highlighter.
2633
* @param {import('@shikijs/core').CodeToHastOptions} [params.highlighterOptions] - Additional options for highlighting.
34+
* @returns {SyntaxHighlighter}
2735
*/
2836
const createHighlighter = ({ coreOptions = {}, highlighterOptions = {} }) => {
2937
const options = {

packages/rehype-shiki/src/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import createHighlighter, { getLanguageByName } from './highlighter.mjs';
2121
* @property {boolean} [wasm=false] - Enable WebAssembly for the regex engine
2222
* @property {boolean} [twoslash=false] - Enable twoslash
2323
* @property {import('@shikijs/twoslash').TransformerTwoslashIndexOptions} [twoslashOptions] - Twoslash configuration options
24-
* @param {import('@shikijs/core').HighlighterCoreOptions} [coreOptions] - Core options for the highlighter.
25-
* @param {import('@shikijs/core').CodeToHastOptions} [highlighterOptions] - Additional options for highlighting.
24+
* @property {import('@shikijs/core').HighlighterCoreOptions} [coreOptions] - Core options for the highlighter.
25+
* @property {import('@shikijs/core').CodeToHastOptions} [highlighterOptions] - Additional options for highlighting.
2626
*/
2727

2828
/**

packages/rehype-shiki/src/plugin.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,10 @@ function isCodeBlock(node) {
5454
}
5555

5656
/**
57-
* @param {import('./index.mjs').HighlighterOptions} options
57+
* @param {import('./index.mjs').HighlighterOptions & { highlighter: import('./highlighter.mjs').SyntaxHighlighter }} options
5858
*/
59-
export default function rehypeShikiji(options) {
60-
let highlighter;
61-
62-
return async function (tree) {
59+
export default ({ highlighter, ...options }) =>
60+
async tree => {
6361
highlighter ??= await createHighlighter(options);
6462

6563
visit(tree, 'element', (_, index, parent) => {
@@ -195,4 +193,3 @@ export default function rehypeShikiji(options) {
195193
parent.children.splice(index, 1, ...children);
196194
});
197195
};
198-
}

0 commit comments

Comments
 (0)