Skip to content

Commit

Permalink
workaround twoslash
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx committed Jan 30, 2025
1 parent 3db0647 commit 7189db9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions packages/slidev/node/syntax/markdown-it/markdown-it-shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ import { isTruthy } from '@antfu/utils'
import { fromAsyncCodeToHtml } from '@shikijs/markdown-it/async'
import { escapeVueInCode } from '../transform/utils'

export default async function MarkdownItShiki({ data: { config }, mode, utils }: ResolvedSlidevOptions) {
const transformers = [
...utils.shikiOptions.transformers || [],
(config.twoslash === true || config.twoslash === mode)
&& (await import('@shikijs/vitepress-twoslash')).transformerTwoslash({
export default async function MarkdownItShiki({ data: { config }, mode, utils: { shiki, shikiOptions } }: ResolvedSlidevOptions) {
async function getTwoslashTransformer() {
const [,,{ transformerTwoslash }] = await Promise.all([
// trigger the shiki to load the langs
shiki.codeToHast('', { lang: 'js', ...shikiOptions }),
shiki.codeToHast('', { lang: 'ts', ...shikiOptions }),

import('@shikijs/vitepress-twoslash'),
])
return transformerTwoslash({
explicitTrigger: true,
twoslashOptions: {
handbookOptions: {
noErrorValidation: true,
},
},
}),
})
}

const transformers = [
...shikiOptions.transformers || [],
(config.twoslash === true || config.twoslash === mode) && await getTwoslashTransformer(),
{
pre(pre) {
this.addClassToHast(pre, 'slidev-code')
Expand All @@ -27,8 +37,8 @@ export default async function MarkdownItShiki({ data: { config }, mode, utils }:
} satisfies ShikiTransformer,
].filter(isTruthy) as ShikiTransformer[]

return fromAsyncCodeToHtml(utils.shiki.codeToHtml, {
...utils.shikiOptions,
return fromAsyncCodeToHtml(shiki.codeToHtml, {
...shikiOptions,
transformers,
})
}
4 changes: 2 additions & 2 deletions packages/types/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MarkdownItShikiOptions } from '@shikijs/markdown-it'
import type { KatexOptions } from 'katex'
import type { ShorthandsBundle } from 'shiki/core'
import type { CodeOptionsThemes, ShorthandsBundle } from 'shiki/core'
import type { SlidevData } from './types'

export interface RootsInfo {
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface ResolvedSlidevOptions extends RootsInfo, SlidevEntryOptions {

export interface ResolvedSlidevUtils {
shiki: ShorthandsBundle<string, string>
shikiOptions: MarkdownItShikiOptions
shikiOptions: MarkdownItShikiOptions & CodeOptionsThemes
katexOptions: KatexOptions | null
indexHtml: string
define: Record<string, string>
Expand Down

0 comments on commit 7189db9

Please sign in to comment.