Skip to content

fix: prevent square brackets in code from being parsed as title#2419

Merged
antfu merged 1 commit into
slidevjs:mainfrom
h-kono-it:fix/markdown-parser-square-brackets
Jan 7, 2026
Merged

fix: prevent square brackets in code from being parsed as title#2419
antfu merged 1 commit into
slidevjs:mainfrom
h-kono-it:fix/markdown-parser-square-brackets

Conversation

@h-kono-it

@h-kono-it h-kono-it commented Dec 29, 2025

Copy link
Copy Markdown
Contributor

Description

Fixes an issue where square brackets on the first line of code blocks were incorrectly interpreted as block titles.

For example, C# attributes like [Flags] at the beginning of code blocks were being parsed as the title instead of being treated as part of the code content.

Changes

  • Modified the regex in code-wrapper.ts to only match spaces and tabs (not newlines) on the opening line
  • Changed \s* to [ \t]* to prevent matching newlines before parsing optional title brackets
  • Added test cases to verify the fix works correctly with and without titles

Test Cases

  1. Code block with square brackets but no title - ensures [Flags] stays in code
  2. Code block with both a title and square brackets in code - ensures both work correctly

Before (Current Behavior)

The first test case incorrectly extracts [Flags] as the title:

 exports[`code-wrapper with square brackets in code 1`] = `
 "
 
-<CodeBlockWrapper v-bind="{}" :title='"Flags"' :ranges='[]'>
+<CodeBlockWrapper v-bind="{}" :title='""' :ranges='[]'>
 
-\`\`\`csharp [Flags]
+\`\`\`csharp
+[Flags]
 enum MyEnum {}
 \`\`\`
 
 </CodeBlockWrapper>
 "
 `;

Visual Example

The following C# code demonstrates the issue:

[Flags]
public enum Permission
{
    None = 0,    // 0000
    Read = 1,    // 0001
    Write = 2,   // 0010
    Delete = 4   // 0100
}

Before the fix:

image

Fixes an issue where square brackets on the first line of code blocks
(e.g., `[Flags]` attribute in C#) were incorrectly interpreted as
block titles. Changed regex to only match spaces and tabs (not newlines)
on the opening line.
@netlify

netlify Bot commented Dec 29, 2025

Copy link
Copy Markdown

Deploy Preview for slidev failed.

Name Link
🔨 Latest commit a78f5d9
🔍 Latest deploy log https://app.netlify.com/projects/slidev/deploys/69527ea395f70700089c080f

@antfu
antfu merged commit 719e6e3 into slidevjs:main Jan 7, 2026
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants