Skip to content

Include HTML defined headings in TOC - #41

Open
deberlhe-jb wants to merge 5 commits into
brokenhandsio:mainfrom
deberlhe-jb:feat/include-html-headings-in-toc
Open

deberlhe-jb wants to merge 5 commits into
brokenhandsio:mainfrom
deberlhe-jb:feat/include-html-headings-in-toc

Conversation

@deberlhe-jb

Copy link
Copy Markdown
Contributor

The table of contents was generated from headings defined in MarkDown syntax.

Markdown allows HTML blocks, but these where just added as is in the output, ignoring headers it might have in it.

This finds headings from HTML blocks and generates the TOC entries with the following rules :

  • when heading has no id attribute, generates one from the title-slug (as done with Markdown), and add it to the HTML
  • when heading has a user defined id attribute, use it
  • when toc.permalink is enabled, add it to the HTML (as done with Markdown)

Unit test added to cover this new behavior.

@0xTim

0xTim commented Oct 2, 2026

Copy link
Copy Markdown
Member

Just an FYI I'll be working on a load of Kiln stuff next week so will review all the open PRs then

@deberlhe-jb

deberlhe-jb commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @0xTim, I know you're busy, just checking if there's any chance this could be reviewed/merged this month ?

@0xTim 0xTim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this. There are three main issues I think we need to fix:

  • attributes are allowed to be unquoted (providing no spaces), single quoted, or double quoted. We only look for double quotes in this PR, losing attributes, which would include IDs that are valid
  • Unclosed tags run across newlines, which will cause problems downstream. This isn't too much of an issue for most things, as the HTML will be invalid, but will be a problem with comments
  • The comment about losing inner HTML that affects not only the ToC (where it would be valid to strip) but also the page HTML which is a problem

Comment thread Tests/KilnTests/MarkdownTests.swift Outdated
</section>
</section>
<section>
<h2>Another <strong>big</strong> section</h3>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're losing HTML information here are we not? This isn't in the ToC, this is in the page code which would be a regression

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed feedback!
You're right, I did not have unquoted or single-quoted attributes in mind.
I'll rework these three topics and let you know when I'm done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright, I pushed fixes for the different attribute syntax and for the loss of page HTML in headers.
I'm failing to see where unclosed tags like comments would cause issues though, as this code would not be called for tags that are not header ones.
Removal of HTML tags from the ToC entries would also remove comments, but this seems fine to me as I believe comments should be for developers rather than end-users.

@deberlhe-jb
deberlhe-jb requested a review from 0xTim November 9, 2026 20:30
@deberlhe-jb

Copy link
Copy Markdown
Contributor Author

@0xTim this merge request is ready for review as well

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