Skip to content

Markdown parser: Fix bug with --- yaml separator in code blocks#2313

Merged
jneen merged 1 commit into
rouge-ruby:mainfrom
Ravlen:code-block-yaml-separator
Jul 6, 2026
Merged

Markdown parser: Fix bug with --- yaml separator in code blocks#2313
jneen merged 1 commit into
rouge-ruby:mainfrom
Ravlen:code-block-yaml-separator

Conversation

@Ravlen

@Ravlen Ravlen commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

When I put a --- as the first line in a codeblock, it seems to get parsed as a setex header by accident. For example, this should work fine:

```markdown
---
title: This sample has YAML frontmatter.
---

Some text.

Solution

All we need to do is parse the backticks first, and move the setex header check to be after the backticks check.

Screenshots

Before After
Screenshot 2026-07-06 at 13 41 24 Screenshot 2026-07-06 at 13 38 58

Comment thread lib/rouge/lexers/markdown.rb
@jneen

jneen commented Jul 3, 2026

Copy link
Copy Markdown
Member

This should be fixable without a negative lookbehind, which I try and avoid due to performance and correctness issues. Are setex headers really allowed to have newlines in them?

@Ravlen

Ravlen commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@jneen Setex headers shouldn't have newlines, that comment is just showing how I can throw in a newline and Rouge no longer treats it like a header. Basically, I'm trying to tell it to treat "Text + newline + ---" as a header, but only if the text is not the start of a codeblock. I'm not sure how to do simply, the lookaround seemed like the smallest fix.

Or phrased differently, setex headers are never preceded by newlines or triple backtick lines. I'll tinker locally and see if I can figure something else out 🤔

Edit: Update, I might have figured it out! If you move the heading checks to be after the backtick check, that seems to do it. I've updated the PR and also came up with a simpler example to demonstrate it (the old one still works too).

@Ravlen
Ravlen force-pushed the code-block-yaml-separator branch from 92e1921 to 94bcf22 Compare July 6, 2026 04:40
@Ravlen Ravlen changed the title Fix bug with yaml separator in code blocks Markdown parser: Fix bug with yaml separator in code blocks Jul 6, 2026
@Ravlen Ravlen changed the title Markdown parser: Fix bug with yaml separator in code blocks Markdown parser: Fix bug with --- yaml separator in code blocks Jul 6, 2026
It could be seen as a setex header if there is an ---
immediately after the triple backticks. So handle the codeblock backticks
first, and the setex header check afterwards
@Ravlen
Ravlen force-pushed the code-block-yaml-separator branch from 94bcf22 to 251a2c9 Compare July 6, 2026 04:54
@jneen

jneen commented Jul 6, 2026

Copy link
Copy Markdown
Member

Looks great, thank you!

@jneen
jneen added this pull request to the merge queue Jul 6, 2026
Merged via the queue into rouge-ruby:main with commit aed8a2f Jul 6, 2026
10 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.

2 participants