Skip to content

fix: prevent mixed =/- chars in Setext-style headings (+tests)#1606

Open
Jah-yee wants to merge 2 commits into
Python-Markdown:masterfrom
Jah-yee:fix/setext-heading-regex
Open

fix: prevent mixed =/- chars in Setext-style headings (+tests)#1606
Jah-yee wants to merge 2 commits into
Python-Markdown:masterfrom
Jah-yee:fix/setext-heading-regex

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented May 22, 2026

Summary

This PR fixes issue #1604 - mixed =/- characters in Setext-style headers were incorrectly matched.

Changes

  • markdown/blockprocessors.py: Changed regex from [=-]+ to (?:[=]+|[-]+) to match only homogeneous runs
  • tests/test_syntax/blocks/test_headers.py: Added 3 test cases for mixed-char rejection

Tests Added

  • test_setext_mixed_chars_not_h1: =- should not form an H1
  • test_setext_mixed_chars_not_h2: -= should not form an H2
  • test_setext_mixed_multiple_chars: Mixed runs are not valid headers

Closes #1604

Jah-yee added 2 commits May 22, 2026 23:43
The regex [=-]+ incorrectly matches mixed runs like '=-' or '-='
which are not valid Setext headers (only = or - alone are valid).

Fix using (?:[=]+|[-]+) to match only homogeneous runs.

Fixes issue Python-Markdown#1604
Reviewer requested tests for the fix preventing mixed =/- chars
in Setext-style headings (issue Python-Markdown#1604, PR Python-Markdown#1605).

Add 3 tests:
- test_setext_mixed_chars_not_h1: =- should not form H1
- test_setext_mixed_chars_not_h2: -= should not form H2
- test_setext_mixed_multiple_chars: mixed runs not valid headers
@Jah-yee
Copy link
Copy Markdown
Author

Jah-yee commented May 22, 2026

Added 3 test cases in tests/test_syntax/blocks/test_headers.py covering mixed-char rejection (test_setext_mixed_chars_not_h1, test_setext_mixed_chars_not_h2, test_setext_mixed_multiple_chars). All tests pass. The fix is ready for review!

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.

Incorrect parsing of Setext-style headings

1 participant