litscript icon indicating copy to clipboard operation
litscript copied to clipboard

As a user I want the TOC to show the first Title instead of filename by default

Open arcmode opened this issue 4 years ago • 2 comments

Hey @johtela , Litscript is looking awesome, thanks for working on it!

Here's my issue. I was trying to get a nice TOC but given my project has a lot of "index.ts" files I feel like I need either:

  • A new configuration for the TOC in order to let html-waver or lits-template decide what to use
  • A different default value for the TOC entries

(thinking about using the first title in the document as TOC entry)

If there is a way to achieve what I need without having to write the toc.json file myself that would be awesome, is there any?

Also, I am more than happy to send a PR if you provide some guidance and acceptance criteria

Thanks

arcmode avatar Mar 06 '20 23:03 arcmode

Hi! Glad you like my project 🙂

I think there is a simple-ish solution to achieve what you are looking for. As you figured out, html-weaver module is in charge of both updating the TOC and converting markdown to HTML. Easiest way to extract the 1st level heading from markdown is to customize the rendering pipeline of markdown-it in the same way as fenced code blocks and tables have been hooked.

Basically we just capture the heading in the renderHtml method and render it normally. We can store the heading to a new property of the HtmlWeaver class (for example heading1). The addTocEntry method could check if we got the heading from markdown and give it to the template instead of the basename of the file, like this:

            tmp.addTocEntry(this.toc, this.heading1 ||
                path.basename(relPath, this.getFileExt()), relPath)

Of course, we need to set the this.heading1 to null or undefined before calling the this.mdit.render() to reset the value.

Since this a minor change, I can implement it myself. That said, I'm of course happy to receive PRs as well. My only criteria for PRs is that they conform to the literate style; that is, contain documentation in comments that explains what the code does. That's kinda the point of the tool 😉

johtela avatar Mar 10 '20 19:03 johtela

Thanks @johtela ! I think I can give it a try this weekend, if I do I will let you know here

arcmode avatar Mar 10 '20 21:03 arcmode