kotlinx-knit icon indicating copy to clipboard operation
kotlinx-knit copied to clipboard

Knit creates invalid TOC links when text contains quotation mark `"`

Open aSemy opened this issue 2 years ago • 1 comments

Given some markdown

#### Using `JsonUnquotedLiteral` to encode `"null"` is forbidden

To avoid creating an inconsistent state, encoding a String equal to `"null"` is forbidden. 
Use [JsonNull] or [JsonPrimitive] instead.

Knit generates an invalid link

    * [Using `JsonUnquotedLiteral` to encode `"null"` is forbidden](#using-jsonunquotedliteral-to-encode-"null"-is-forbidden)
image

If I change the link to remove the quotation marks, then it works as expected.

    * [Using `JsonUnquotedLiteral` to encode `"null"` is forbidden](#using-jsonunquotedliteral-to-encode-null-is-forbidden)

aSemy avatar Oct 20 '22 15:10 aSemy

I found this comment, https://gist.github.com/asabaylus/3071099?permalink_comment_id=1593627#gistcomment-1593627

The code that creates the anchors is here: https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb

  1. It downcases the string
  2. remove anything that is not a letter, number, space or hyphen (see the source for how Unicode is handled)
  3. changes any space to a hyphen.
  4. If that is not unique, add "-1", "-2", "-3",... to make it unique

It's from 2015, so this might be outdated. Perhaps this logic can be used to make the link handling more robust.

See also: https://github.com/imthenachoman/nGitHubTOC

aSemy avatar Oct 20 '22 16:10 aSemy