Skip to content

fix(html-nav): generate heading anchors the way GitHub does - #115

Merged
drevendev merged 1 commit into
masterfrom
beautifulrepo/github-heading-slugs-20261023
Oct 23, 2026
Merged

drevendev merged 1 commit into
masterfrom
beautifulrepo/github-heading-slugs-20261023

Conversation

@drevendev

Copy link
Copy Markdown
Owner

Reader problem and change

A maintainer who linked to a heading with the anchor GitHub actually generates got a red check, and a link GitHub would break passed. html_navigation.py collapsed runs of spaces, decoded entities before stripping tags, kept emphasis underscores and image alt text, and dropped combining marks.

github_heading_slug now applies GitHub's rule to the rendered heading text:

  • lowercase;
  • keep letters, marks, decimal digits, letter numbers, connector punctuation, hyphens and spaces;
  • turn each space into its own hyphen.

What counts as text:

  • code-span text counts literally;
  • images, tags, comments, emphasis markers and backslash escapes are handled before the filter.

The masked copy still decides which lines are headings, so nothing inside code blocks counts. The text now comes from the source line. That retires restore_heading_code and protect_inline_code_angles. The duplicate-suffix logic is unchanged; GitHub confirms intro, intro-1, intro-2.

Closes #102

Evidence

  • Every expected value in tests/test_github_heading_slugs.py, 24 headings plus duplicates and a comment case, was read back from GitHub's Markdown API on 2026-10-23 (gh api markdown -f mode=markdown, id="user-content-…"). It covers all rows of the issue's table, plus images (logo--here), emoji (emoji--party), NBSP, tabs, escapes and snake_case.
  • Against github-slugger's 78 fixtures, the disagreements drop from 31 to 7. Six of those are artefacts of the comparison: each fixture is a separate document, and spaces at the edges are ones Markdown strips. The seventh is characters newer than the Unicode version GitHub uses.

Verification

  • The five local checks passed on Windows 11 with Python 3.14.7: catalog OK; docs links 194; HTML navigation 205, unchanged, so all of BeautifulRepo's own anchors still resolve; 278 tests OK, 7 skipped; repo audit 0 findings.
  • The existing heading tests (run-python--m-unittest, use-a-href-with-html, literal-suffix duplicates) still pass.
  • Accessibility and visual checks: not applicable.

Limitations

  • Setext headings are not scanned, as before.
  • Characters assigned after the Unicode version of GitHub's Ruby may be kept or dropped differently. Python 3.14 knows newer letters.

🤖 Generated with Claude Code

html_navigation.py collapsed runs of spaces, decoded entities before stripping
tags, kept emphasis underscores and image alt text, and dropped combining
marks. Valid links to GitHub's anchors failed (terms--conditions, a--b--c) and
links to collapsed ones passed.

github_heading_slug now mirrors GitHub's rule on the rendered text: lowercase,
keep letters, marks, digits (decimal and letter numbers), connector punctuation,
hyphens and spaces, and turn each space into a hyphen. Code-span text counts
literally; images, tags, comments, emphasis markers and backslash escapes are
handled before the filter. The masked copy still decides which lines are
headings; the text now comes from the source line, which retires
restore_heading_code and protect_inline_code_angles.

Every expected value in tests/test_github_heading_slugs.py was read back from
GitHub's Markdown API (mode=markdown) on 2026-10-23. Against github-slugger's 78
fixtures the remaining 7 differences are comparison artefacts (separate
documents, edge spaces Markdown strips) or Unicode-version differences.

Closes #102

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@drevendev
drevendev force-pushed the beautifulrepo/github-heading-slugs-20261023 branch from 4c656bd to 09b86e8 Compare October 23, 2026 07:51

@andy-zen-dev andy-zen-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 09b86e89c702e0c8891a9d53bde39b3214f4c336: all four changed files, the existing HTML navigation/fragment scanner, issue #102 acceptance, CONTRIBUTING.md and the new slug fixtures. No AGENTS.md exists at this head. The implementation preserves the masked-source gate for recognizing headings while deriving slug text from original ATX heading content; this addresses repeated spaces, HTML-entity/tag ordering, escaped punctuation, inline code, emphasis, combining marks, and duplicate suffixes. Tests cover the issue's reproduced mismatches and a previously false-positive dead fragment.

All five required jobs in Actions run 39920557540 succeeded (Python 3.11, 3.14, 3.14 Windows, Ubuntu/Windows action self-tests). I did not execute independent local tests or verify GitHub's Markdown API responses myself. The documented limitation to ATX headings and potential Unicode-version differences remains. No merge-blocking finding for the documented scope.

@drevendev
drevendev merged commit 681d51d into master Oct 23, 2026
5 checks passed
@drevendev
drevendev deleted the beautifulrepo/github-heading-slugs-20261023 branch October 23, 2026 08:22
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.

html_navigation: heading anchors differ from GitHub's, so valid links fail and dead ones pass

2 participants