From bd38e14ae3e90b4e7463db52370abad76b40db45 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 5 Feb 2024 16:40:22 +0100 Subject: [PATCH] fix(vitepress): support custom explicitTrigger --- packages/vitepress-twoslash/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/vitepress-twoslash/src/index.ts b/packages/vitepress-twoslash/src/index.ts index 742bba0fa..69f060791 100644 --- a/packages/vitepress-twoslash/src/index.ts +++ b/packages/vitepress-twoslash/src/index.ts @@ -21,15 +21,23 @@ export interface VitePressPluginTwoslashOptions extends TransformerTwoslashOptio * Add this to `markdown.codeTransformers` in `.vitepress/config.ts` */ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}): ShikiTransformer { + const { + explicitTrigger = true, + } = options + const twoslash = createTransformerFactory( createTwoslasher(), )({ langs: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'], - explicitTrigger: true, renderer: rendererFloatingVue(options), ...options, + explicitTrigger, }) + const trigger = explicitTrigger instanceof RegExp + ? explicitTrigger + : /\btwoslash\b/ + return { ...twoslash, name: '@shikijs/vitepress-twoslash', @@ -39,7 +47,7 @@ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {} options.transformers?.splice(options.transformers.indexOf(cleanup), 1) // Disable v-pre for twoslash, because we need render it with FloatingVue - if (options.meta?.__raw?.includes('twoslash')) { + if (!explicitTrigger || options.meta?.__raw?.match(trigger)) { const vPre = options.transformers?.find(i => i.name === 'vitepress:v-pre') if (vPre) options.transformers?.splice(options.transformers.indexOf(vPre), 1)