markdown-it / markdown-it-footnote

Footnotes plugin for markdown-it markdown parser
https://markdown-it.github.io/
MIT License
210 stars 59 forks source link

Add customization examples #47

Closed akaleeroy closed 2 years ago

akaleeroy commented 2 years ago

Regarding #28 what would be a good example to add to the README? I've used the following to customize footnotes for EPUB ebooks:

const backrefLabel = 'back to text';

const epubRules = {
  footnote_ref: ['<a', '<a epub:type="noteref"'],
  footnote_open: ['<li', '<li epub:type="footnote"'],
  footnote_anchor: ['<a', `<a aria-label="${backrefLabel}"`],
}

Object.keys(epubRules).map(rule => {
  let defaultRender = md.renderer.rules[rule];
  md.renderer.rules[rule] = (tokens, idx, options, env, self) => {
    return defaultRender(tokens, idx, options, env, self).replace(...epubRules[rule]);
  }
})

Since renderers return a string this just does string replacement to add required attributes to the markup.

Reference: Markdown-It Architecture > Renderer

Seems to have worked, but I'm not sure if this is a good example to add. Which is why I opened this issue.

rlidwka commented 2 years ago

Yes, it is as good of an example as you can get with the current architecture.

Personally, I'd like to have markdown AST, transform it to HTML DOM and serialize that. Then you'd be able to write a plugin to add epub:type attribute between these two steps. We don't have that, so extending renderer is a reasonable approach.

rlidwka commented 2 years ago

added your example to README, thank you