Skip to content

Commit 7189db9

Browse files
committed
workaround twoslash
1 parent 3db0647 commit 7189db9

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

packages/slidev/node/syntax/markdown-it/markdown-it-shiki.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ import { isTruthy } from '@antfu/utils'
44
import { fromAsyncCodeToHtml } from '@shikijs/markdown-it/async'
55
import { escapeVueInCode } from '../transform/utils'
66

7-
export default async function MarkdownItShiki({ data: { config }, mode, utils }: ResolvedSlidevOptions) {
8-
const transformers = [
9-
...utils.shikiOptions.transformers || [],
10-
(config.twoslash === true || config.twoslash === mode)
11-
&& (await import('@shikijs/vitepress-twoslash')).transformerTwoslash({
7+
export default async function MarkdownItShiki({ data: { config }, mode, utils: { shiki, shikiOptions } }: ResolvedSlidevOptions) {
8+
async function getTwoslashTransformer() {
9+
const [,,{ transformerTwoslash }] = await Promise.all([
10+
// trigger the shiki to load the langs
11+
shiki.codeToHast('', { lang: 'js', ...shikiOptions }),
12+
shiki.codeToHast('', { lang: 'ts', ...shikiOptions }),
13+
14+
import('@shikijs/vitepress-twoslash'),
15+
])
16+
return transformerTwoslash({
1217
explicitTrigger: true,
1318
twoslashOptions: {
1419
handbookOptions: {
1520
noErrorValidation: true,
1621
},
1722
},
18-
}),
23+
})
24+
}
25+
26+
const transformers = [
27+
...shikiOptions.transformers || [],
28+
(config.twoslash === true || config.twoslash === mode) && await getTwoslashTransformer(),
1929
{
2030
pre(pre) {
2131
this.addClassToHast(pre, 'slidev-code')
@@ -27,8 +37,8 @@ export default async function MarkdownItShiki({ data: { config }, mode, utils }:
2737
} satisfies ShikiTransformer,
2838
].filter(isTruthy) as ShikiTransformer[]
2939

30-
return fromAsyncCodeToHtml(utils.shiki.codeToHtml, {
31-
...utils.shikiOptions,
40+
return fromAsyncCodeToHtml(shiki.codeToHtml, {
41+
...shikiOptions,
3242
transformers,
3343
})
3444
}

packages/types/src/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { MarkdownItShikiOptions } from '@shikijs/markdown-it'
22
import type { KatexOptions } from 'katex'
3-
import type { ShorthandsBundle } from 'shiki/core'
3+
import type { CodeOptionsThemes, ShorthandsBundle } from 'shiki/core'
44
import type { SlidevData } from './types'
55

66
export interface RootsInfo {
@@ -58,7 +58,7 @@ export interface ResolvedSlidevOptions extends RootsInfo, SlidevEntryOptions {
5858

5959
export interface ResolvedSlidevUtils {
6060
shiki: ShorthandsBundle<string, string>
61-
shikiOptions: MarkdownItShikiOptions
61+
shikiOptions: MarkdownItShikiOptions & CodeOptionsThemes
6262
katexOptions: KatexOptions | null
6363
indexHtml: string
6464
define: Record<string, string>

0 commit comments

Comments
 (0)