Skip to content

fix(toc): link TOC entries to the heading anchor and strip heading tags - #185

Merged
ekalinin merged 2 commits into
masterfrom
fix/heading-markup
Oct 30, 2026
Merged

ekalinin merged 2 commits into
masterfrom
fix/heading-markup

Conversation

@ekalinin

Copy link
Copy Markdown
Owner

Summary

Fixes #40, fixes #67: a heading that contains a link gets a TOC entry that links to the
wrong place, and its text keeps the raw HTML that GitHub renders for the heading.

Heading Before After
## On <sub><sub>[Go to Top](#top)</sub></sub> * [On <sub><sub><a href="#top">Go to Top</a></sub></sub>](#top) * [On Go to Top](#on-go-to-top)
## [My issue](https://github.com/ekalinin/github-markdown-toc/issues/67) * [<a href="https://...">My issue</a>](https://github.com/ekalinin/github-markdown-toc/issues/67) * [My issue](#my-issue)
## See [docs](#top) here * [See <a href="#top">docs</a> here](#top) * [See docs here](#see-docs-here)
## A ![img](x.png) B * [A <a ...><img ...></a> B](x.png) * [A B](#a--b)
## Linux vs. Samba ***permissions*** * [Linux vs. Samba <em><strong>permissions</strong></em>](#linux-vs-samba-permissions) * [Linux vs. Samba permissions](#linux-vs-samba-permissions)
  • gh-md-toc (gh_toc_grab):
    • The link was the first href of the heading line, so a link in the heading text came
      before the heading's own anchor. href_regex now matches the anchor element
      (class="anchor"), and href is taken from it, in both awk branches (default and
      OS/390).
    • All HTML tags are removed from the heading text in common_awk_script; the text
      inside them is kept. HTML entities stay as GitHub renders them
      (Tom &amp; Jerry &lt;3). It is done on text in awk, not with sed over the line,
      which would also remove the anchor element.
    • Steps 4 and 5 of the comment at the top of the file are updated to the current
      expressions.
  • tests/tests.bats: a new test on a new fixture test_headingmarkup.md. The Repairing setext heading support, certain uses of backticks after GitHub API format changes #145 test
    now expects * [Title two](#title-two) instead of * [<em>Title two</em>](#title-two),
    and the same line is updated in the TOC of test_setextwithformatting.md, because that
    test runs with --hide-footer (--insert) and rewrites the fixture.
  • openspec: the fix-heading-markup change (proposal, design, specs, tasks), not
    archived yet.

Output change: entries of headings with inline formatting or inline HTML (*em*,
**strong**, <sub>, links, images) lose the tags, so a TOC that --insert put into
such a file changes on the next run. Entries of headings without tags do not change.

Not included: decoding HTML entities, collapsing the spaces left by a removed element
(A B, as in GitHub's own aria-label), keeping backticks in entries (#153), the
separate OS/390 text regex.

Testing

Fixes #40 and #67. A link in the heading text was taken as the link of
the TOC entry, and the entry text kept the raw HTML of the heading. The
link now comes from the heading's own anchor (class="anchor"), and all
tags are removed from the text: `## [My issue](url)` gives
`* [My issue](#my-issue)`.
@ekalinin
ekalinin merged commit b8d6403 into master Oct 30, 2026
1 check passed
@ekalinin
ekalinin deleted the fix/heading-markup branch October 30, 2026 11:42
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.

Remove URL if the title is a URL Failing to create the headers correctly when I use <sub><sub><sub>[Go to Top](#top)</sub></sub></sub>

1 participant