lexi-lambda / racket-commonmark

Fast, CommonMark-compliant Markdown parser written in Racket

Home Page:https://lexi-lambda.github.io/racket-commonmark/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interest check: support unique footnote anchors

otherjoel opened this issue · comments

When published as HTML, sometimes multiple rendered documents will appear on the same page, such as on a blog that displays the full text of multiple posts on its home page. If the sources use identical footnote labels (e.g. [^1], [^2], etc.), then the anchors for the reference links and backlinks will collide when they are displayed on the same page.

Not every project will be affected by this. But for those that are, could we add a new parameter named, e.g., footnote-label-prefix, defaulting to "", and prefix its value to all footnote labels in the document during parsing?

#lang racket/base

(require commonmark racket/file)

(define (my-post-renderer source-file)
  (define source-str (file->string source-file)
  (parameterize ([footnote-label-prefix (my-unique-id-maker source-file)])
    (document->html (string->document source-str))))

If you are not interested, please feel free to close this issue without comment. I understand it’s an edge case, and it’s already possible to avoid this by taking care to use reasonably unique labels when writing the markdown source. Also, I could always traverse the AST after the parse and prefix all the labels before rendering. I just thought I’d ask, since it would be faster and more ergonomic to handle it during at parse time.

I agree that this is a good idea. I’m generally fairly open to adding more options to customize the output of the default HTML renderer, I just didn’t happen to implement much more than I personally needed.

If you want to open a PR that adds support for something like this, I’d be happy to accept it, provided it is accompanied by tests and documentation. I do think this is really a rendering concern, though, not a parsing concern, so it should probably be a parameter used by the HTML renderer. Also, it might as well be made more general than just adding a prefix, so it might as well accept an arbitrary (-> string? string?). It’s also worth thinking about whether it makes sense to apply to all anchors, including those added for section titles, or if it should be specific to footnotes.

Thanks! I agree, it makes much more sense for the renderer to handle this.