Skip to content

Commit 719e6e3

Browse files
authored
fix: prevent square brackets in code from being parsed as title (#2419)
1 parent aef781c commit 719e6e3

3 files changed

Lines changed: 55 additions & 1 deletion

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'-]+)?\s*(?:\[(.*?)\])?\s*(?:\{([\w*,|-]+)\}\s*?(\{[^}]*\})?([^\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.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`code-wrapper with square brackets in code 1`] = `
4+
"
5+
6+
<CodeBlockWrapper v-bind="{}" :title='""' :ranges='[]'>
7+
8+
\`\`\`csharp
9+
[Flags]
10+
enum MyEnum {}
11+
\`\`\`
12+
13+
</CodeBlockWrapper>
14+
"
15+
`;
16+
17+
exports[`code-wrapper with title and square brackets in code 1`] = `
18+
"
19+
20+
<CodeBlockWrapper v-bind="{}" :title='"MyEnum.cs"' :ranges='[]'>
21+
22+
\`\`\`csharp [MyEnum.cs]
23+
[Flags]
24+
enum MyEnum {}
25+
\`\`\`
26+
27+
</CodeBlockWrapper>
28+
"
29+
`;
30+
331
exports[`external snippet 1`] = `
432
"
533
\`\`\`ts {2|3|4}{lines:true}

test/transform.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,29 @@ it('external snippet', () => {
177177

178178
expect(ctx.s.toString()).toMatchSnapshot()
179179
})
180+
181+
it('code-wrapper with square brackets in code', () => {
182+
const ctx = createTransformContext(`
183+
\`\`\`csharp
184+
[Flags]
185+
enum MyEnum {}
186+
\`\`\`
187+
`)
188+
189+
transformCodeWrapper(ctx)
190+
191+
expect(ctx.s.toString()).toMatchSnapshot()
192+
})
193+
194+
it('code-wrapper with title and square brackets in code', () => {
195+
const ctx = createTransformContext(`
196+
\`\`\`csharp [MyEnum.cs]
197+
[Flags]
198+
enum MyEnum {}
199+
\`\`\`
200+
`)
201+
202+
transformCodeWrapper(ctx)
203+
204+
expect(ctx.s.toString()).toMatchSnapshot()
205+
})

0 commit comments

Comments
 (0)