Skip to content

Commit 89f05bf

Browse files
committed
fix: twoslash code block parsing
1 parent 9e74427 commit 89f05bf

4 files changed

Lines changed: 52 additions & 8 deletions

File tree

packages/slidev/node/syntax/transform/code-wrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { MarkdownTransformContext } from '@slidev/types'
22
import { normalizeRangeStr } from './utils'
33

44
// eslint-disable-next-line regexp/no-super-linear-backtracking
5-
export const reCodeBlock = /^```([\w'-]+)?[ \t]*(?:\[(.*?)\])?[ \t]*(?:\{([\w*,|-]+)\}[ \t]*(\{[^}]*\})?([^\r\n]*))?\r?\n((?:(?!^```)[\s\S])*?)^```$/gm
5+
export const reCodeBlock = /^```([\w '-]+)?[ \t]*(?:\[(.*?)\])?[ \t]*(?:\{([\w*,|-]+)\}[ \t]*(\{[^}]*\})?([^\r\n]*))?\r?\n((?:(?!^```)[\s\S])*?)^```$/gm
66

77
/**
88
* Transform code block with wrapper

test/__snapshots__/transform-all.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Default Slot
99
1010
<CodeBlockWrapper v-bind="{lines:true}" :title='""' :ranges='["2","3","4"]'>
1111
12-
\`\`\`ts
12+
\`\`\`ts
1313
function _foo() {
1414
// ...
1515
}
@@ -44,7 +44,7 @@ Foo \`{{ code }}\`
4444
4545
<CodeBlockWrapper v-bind="{}" :title='"with-title"' :ranges='[]'>
4646
47-
\`\`\`js [with-title]
47+
\`\`\`js [with-title]
4848
const a = 1
4949
\`\`\`
5050
@@ -55,7 +55,7 @@ const a = 1
5555
5656
<CodeBlockWrapper v-bind="{}" :title='"npm"' :ranges='[]'>
5757
58-
\`\`\`sh [npm]
58+
\`\`\`sh [npm]
5959
npm i slidev
6060
\`\`\`
6161
@@ -64,7 +64,7 @@ npm i slidev
6464
6565
<CodeBlockWrapper v-bind="{}" :title='"yarn"' :ranges='[]'>
6666
67-
\`\`\`sh [yarn]
67+
\`\`\`sh [yarn]
6868
yarn add slidev
6969
\`\`\`
7070
@@ -73,7 +73,7 @@ yarn add slidev
7373
7474
<CodeBlockWrapper v-bind="{}" :title='"pnpm"' :ranges='[]'>
7575
76-
\`\`\`sh [pnpm]
76+
\`\`\`sh [pnpm]
7777
pnpm add slidev
7878
\`\`\`
7979
@@ -88,7 +88,7 @@ pnpm add slidev
8888
8989
<CodeBlockWrapper v-bind="{}" :title='""' :ranges='[]'>
9090
91-
\`\`\`ts
91+
\`\`\`ts
9292
function _foo() {
9393
// ...
9494
}

test/__snapshots__/transform.test.ts.snap

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports[`code-wrapper with title and square brackets in code 1`] = `
1919
2020
<CodeBlockWrapper v-bind="{}" :title='"MyEnum.cs"' :ranges='[]'>
2121
22-
\`\`\`csharp [MyEnum.cs]
22+
\`\`\`csharp [MyEnum.cs]
2323
[Flags]
2424
enum MyEnum {}
2525
\`\`\`
@@ -28,6 +28,31 @@ enum MyEnum {}
2828
"
2929
`;
3030
31+
exports[`code-wrapper with twoslash 1`] = `
32+
"
33+
34+
35+
36+
<CodeBlockWrapper v-bind="{}" :title='"filename.ts"' :ranges='[]'>
37+
38+
\`\`\`ts [filename.ts]
39+
const a = 1
40+
\`\`\`
41+
42+
</CodeBlockWrapper>
43+
44+
45+
<CodeBlockWrapper v-bind="{}" :title='"filename.ts"' :ranges='[]'>
46+
47+
\`\`\`ts twoslash [filename.ts]
48+
const a = 1
49+
\`\`\`
50+
51+
</CodeBlockWrapper>
52+
53+
"
54+
`;
55+
3156
exports[`external snippet 1`] = `
3257
"
3358
\`\`\`ts {2|3|4}{lines:true}

test/transform.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,22 @@ enum MyEnum {}
203203

204204
expect(ctx.s.toString()).toMatchSnapshot()
205205
})
206+
207+
it('code-wrapper with twoslash', () => {
208+
const ctx = createTransformContext(`
209+
210+
211+
\`\`\`ts [filename.ts]
212+
const a = 1
213+
\`\`\`
214+
215+
\`\`\`ts twoslash [filename.ts]
216+
const a = 1
217+
\`\`\`
218+
219+
`)
220+
221+
transformCodeWrapper(ctx)
222+
223+
expect(ctx.s.toString()).toMatchSnapshot()
224+
})

0 commit comments

Comments
 (0)