Table of Contents situation

Since some people have been commenting on the table of contents situation, this is a thread to explain my plans for upcoming changes.

The status quo is that the ToC is fine except when the page has headings that skip levels. This is most easily felt if you decide that your content files should start at <h2>, so your SuperMD headings always have at least ## in front of them.

This is what it looks like:

Obviously not optimal.

A PR was submitted to try and fix this issue by detecting the “highest” heading level (or lowest numerically, depending on how you want to look at it), in order to normalize the table of contents at that level. This is done also in light of general HTML authoring best practices that say that documents should only have a single <h1> in them.

I’m not going to merge that PR for a few reasons, some of which are not relevant to this discussion, so I’ll focus on the main key point:

Even if the ToC is normalized at the top level, you will still be able to skip levels “in the middle” of it, still being able to cause breakages. For example if we have:

# title1
## title2
#### title4
### title3

This will still render like this:

And the presence of a h3 makes it problematic trying to “compact” the ToC anyway.

So my take is that heading jumps should result in a build error as that’s also nice to catch when you jump a level by mistake.

This leaves the “single h1” issue not resolved though. I have solution for that as well but I would like first to make it clear that I think that all “best practices” should be evaluated critically, and that I generally don’t agree with many of the “semantic HTML” best practices.

In particular, my understanding of the HTML spec is that it is unrealistic (and actually detrimental) to expect content authors to use headings to mark a content hierarchy reliably.

On the other hand instead, we can (and should) aim for using SuperMD headings to define a correct content hierarchy.

To put this into more concrete terms, I proposed above to make smd heading jumps a build error, but I will never agree to add a check to SuperHTML that headings in a template have no jumps.

Hopefully I’ve made it clear how to me it’s SuperMD semantics that drive HTML generation and never the other way around.

So, what do we do then wrt headings? Instead of having authors start at ## in SuperMD, we change how headings render to HTML.

SuperMD files will always start their sectioning at # and they will not allow jumps, but the HTML renderer can be configured to start at <h2> for those who care about following the advice presented by the MDN.

4 Likes

I’m currently upgrading my personal websites with a debug build of Zine that implements this change and in some of them I have used the convention of starting at ‘##’ because I had not created a class-based style for the page’s main <h1> title.

So far I have found gaps in some heading levels on the official website, so this change has actually caught a mistake already.

Aside from everything else, it’s nice that all Zine sites are now more consistent and easy to edit for Zine users: no matter the codebase, you know that content files must always start at ‘#’. In hindsight it’s kind of bonkers that this is not the norm.

1 Like

The new ToC with the headings_h2 update works great :slight_smile:! The migration was not so easy because I have blog posts with a read time of more then an hour (and code blogs with non heading comments starting with # haha… led to a small conflict with the substitute s/^#//) and a lot of headings, but in the end it worked! Thanks for the update @kristoff

The only thing I can’t do right now is having a ToC with links to the headings, but I guess this is a bigger problem then just the ToC, because the heading do not produce an ID per default. I think I will continue my hugo to zine migration without a linked ToC for now, I can live with this :slight_smile:

Great work!

You can have links in the ToC, but it does require some extra work on your part :^)

The reason why Zine doesn’t auto-link your headings is because deriving the fragment id from the text means that if you ever edit those titles, you will break all deep-links to your content. Internal broken links will be caught by Zine but you will also have broken any potentially existing external deep link to your content.

So you’re expected to give an ID manually to your headings, in which case Zine will generate a link to them in your ToC.

The general way to give an id to a chunk of text is (just an FYI, not relevant to ToC links):

[foo]($text.id('foo'))

In the case of headings (the default choice for getting links in the ToC):

# [Preserve Old Links]($heading.id('old-links'))

And you can also do it with content sections (in case you need them):

# [Preserve Old Links]($section.id('old-links'))
1 Like

Nice, it works!

I build a small substitute to simplify the id replacement in vim, maybe that helps someone later:

:%s#^\(\#\+\)\s\+\(.*\)#\=printf('%s [%s]($heading.id(''%s''))', submatch(1), submatch(2), substitute(tolower(substitute(submatch(2), '[^[:alnum:]_]', '-', 'g')), '-$', '', ''))#gc
2 Likes

That’s a great idea. It’s not lost on me how annoying it is to have to type everything out manually, but in Zine I prefer correctness over convenience and over time I found myself appreciating this approach more and more, as Zine catches mistakes that otherwise would go unnoticed.

Unfortunately when it comes to heading IDs nothing in the current toolchain can help smooth out the usecase. I guess in theory I could have Zine modify the file in place to add ids to everything but it would be a janky solution and people would probably (rightfully) not like it, starting from myself.

Maybe once SuperMD gains a language server, this can be an action that gets offered to the user, but in the meantime (or if you don’t want an LSP) having your own shortcut is the way to go.