Skip to content

Commit bfe2781

Browse files
committed
fix: code block parsing
1 parent 89f05bf commit bfe2781

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ 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'-]*)(?:\[([^\]]*)\])?[ \t]*(?:\{([\w*,|-]+)\}[ \t]*(\{[^}]*\})?([^\r\n]*))?\r?\n((?:(?!^```)[\s\S])*?)^```$/gm
66

77
/**
88
* Transform code block with wrapper
99
*/
1010
export function transformCodeWrapper(ctx: MarkdownTransformContext) {
1111
ctx.s.replace(
1212
reCodeBlock,
13-
(full, lang = '', title = '', rangeStr: string = '', options = '', attrs = '', code: string) => {
13+
(full, lang = '', title = '', rangeStr = '', options = '', attrs = '', code: string) => {
1414
const ranges = normalizeRangeStr(rangeStr)
1515
code = code.trimEnd()
1616
options = options.trim() || '{}'

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: 3 additions & 3 deletions
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
\`\`\`
@@ -35,7 +35,7 @@ exports[`code-wrapper with twoslash 1`] = `
3535
3636
<CodeBlockWrapper v-bind="{}" :title='"filename.ts"' :ranges='[]'>
3737
38-
\`\`\`ts [filename.ts]
38+
\`\`\`ts [filename.ts]
3939
const a = 1
4040
\`\`\`
4141
@@ -44,7 +44,7 @@ const a = 1
4444
4545
<CodeBlockWrapper v-bind="{}" :title='"filename.ts"' :ranges='[]'>
4646
47-
\`\`\`ts twoslash [filename.ts]
47+
\`\`\`ts [filename.ts]
4848
const a = 1
4949
\`\`\`
5050

0 commit comments

Comments
 (0)