From ae4db41da163581e52fd444123155e2af586732e Mon Sep 17 00:00:00 2001 From: Quang Phan Date: Thu, 29 Feb 2024 21:59:10 +0700 Subject: [PATCH] pass filename to highlighter --- .changeset/wild-sloths-drop.md | 5 +++++ packages/mdsvex/src/transformers/index.ts | 6 ++++-- packages/mdsvex/src/types.ts | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 .changeset/wild-sloths-drop.md diff --git a/.changeset/wild-sloths-drop.md b/.changeset/wild-sloths-drop.md new file mode 100644 index 00000000..4d36b048 --- /dev/null +++ b/.changeset/wild-sloths-drop.md @@ -0,0 +1,5 @@ +--- +'mdsvex': minor +--- + +Pass filename to highligher diff --git a/packages/mdsvex/src/transformers/index.ts b/packages/mdsvex/src/transformers/index.ts index a992fcca..417df56d 100644 --- a/packages/mdsvex/src/transformers/index.ts +++ b/packages/mdsvex/src/transformers/index.ts @@ -547,7 +547,7 @@ export function highlight_blocks({ } } - return async function (tree) { + return async function (tree, vFile) { if (highlight_fn) { const nodes: (Code | HTML)[] = []; visit(tree, 'code', (node) => { @@ -560,7 +560,9 @@ export function highlight_blocks({ node.value = await highlight_fn( node.value, (node as Code).lang, - (node as Code).meta + (node as Code).meta, + //@ts-ignore + vFile.filename, ); }) ); diff --git a/packages/mdsvex/src/types.ts b/packages/mdsvex/src/types.ts index 3d5a25c8..51b98da0 100644 --- a/packages/mdsvex/src/types.ts +++ b/packages/mdsvex/src/types.ts @@ -153,7 +153,8 @@ export type Layout = Record; export type Highlighter = ( code: string, lang: string | null | undefined, - metastring: string | null | undefined + metastring: string | null | undefined, + filename?: string, ) => string | Promise; interface HighlightOptions { /**