Skip to content

fix: detect ATX headings indented up to 3 spaces#26

Merged
zerochae merged 1 commit into
masterfrom
fix/md-indented-heading
Jul 24, 2026
Merged

fix: detect ATX headings indented up to 3 spaces#26
zerochae merged 1 commit into
masterfrom
fix/md-indented-heading

Conversation

@zerochae

@zerochae zerochae commented Jul 24, 2026

Copy link
Copy Markdown
Owner

Problem

The Markdown parser missed ATX headings that had any leading whitespace.
# Title was detected, but # Title (indented) was not — even though
CommonMark treats up to 3 leading spaces as a valid heading.

Cause

The heading pattern was anchored at column 0:

local hashes, rest = line:match "^(#+)%s+(.*)$"

Any space before # made the match fail.

Fix

Allow 0–3 leading spaces, and keep 4+ spaces as an indented code block
(per CommonMark):

local indent, hashes, rest = line:match "^( *)(#+)%s+(.*)$"
if hashes and #hashes <= 6 and #indent <= 3 then

Tests

Added regression tests in tests/parser_spec.lua:

  • headings with 1–3 leading spaces are detected ( # H2, ### H3)
  • a heading indented 4+ spaces is ignored (indented code block)

Full suite: 78 passed / 0 failed.

Scope

Other parsers were audited for the same class of bug. Org headlines, reST
section adornments, and Vim-help rules are all required to sit in column 0 by
their formats, so their column-0 anchoring is correct — no change needed. The
HTML heading matcher already allows leading whitespace.

The markdown parser anchored the heading pattern at column 0, so a heading
with any leading whitespace (e.g. "  # Title") was not detected. Allow 0-3
leading spaces per CommonMark; 4+ spaces remain an indented code block.
@zerochae zerochae self-assigned this Jul 24, 2026
@zerochae
zerochae merged commit 4f5bc08 into master Jul 24, 2026
3 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.

1 participant