Skip to content

Commit

Permalink
fix(vitepress): support custom explicitTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 5, 2024
1 parent 61b87f3 commit bd38e14
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/vitepress-twoslash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down

0 comments on commit bd38e14

Please sign in to comment.