Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please support markdown footnotes [^1] [^note] #1628

Open
thediveo opened this issue Aug 20, 2021 · 6 comments
Open

Please support markdown footnotes [^1] [^note] #1628

thediveo opened this issue Aug 20, 2021 · 6 comments
Milestone

Comments

@thediveo
Copy link

Feature request

What problem does this feature solve?

To cite the Markdown Guide:

Footnotes allow you to add notes and references without cluttering the body of the document.

For instance, in docsified software manuals this would help with structuring the information given better: useful background information can be given and clearly linked with the main text, yet without making the main text too full of details.

What does the proposed API look like?

According to the Markdown Guide:

A footnote[^1]

[^1] This is a footnote.

How should this be implemented in your opinion?

I'm not sure I understand this question in the context of this particular feature request.

One thing I notice it that docify authors might want to have control over the placement of footnotes: either at the end of a "page" or, alternatively, before the next same-level heading.

Are you willing to work on this yourself?

Unfortunately, I completely lack the necessary Javascript and markdown parsing experience to code this.

@sy-records
Copy link
Member

We use marked, is "footnote" supported in marked.js?

if you want, you can put them all at the end of your document, sort of like footnotes.

Here's an example of reference links in action:

I get 10 times more traffic from [Google] [1] than from
[Yahoo] [2] or [MSN] [3].

  [1]: http://google.com/        "Google"
  [2]: http://search.yahoo.com/  "Yahoo Search"
  [3]: http://search.msn.com/    "MSN Search"

Using the implicit link name shortcut, you could instead write:

I get 10 times more traffic from [Google][] than from
[Yahoo][] or [MSN][].

  [google]: http://google.com/        "Google"
  [yahoo]:  http://search.yahoo.com/  "Yahoo Search"
  [msn]:    http://search.msn.com/    "MSN Search"

I get 10 times more traffic from Google than from Yahoo or MSN.

see https://github.com/markedjs/marked/blob/master/test/specs/original/markdown_documentation_syntax.md#span-elements

@thediveo
Copy link
Author

@sy-records I appreciate your suggestion, but this is difficult to keep consistent in larger documents and lacks the chance of proper CSS styling (unless you add in convoluted HTML interspersed with markdown).

Looking more around the link you gave, I found this answer with code snippets of how to add that feature after marked parsing and before rendering: markedjs/marked#1562 (comment)

Unfortunately, I lack any experience in this area as to how marked is used in docsify and how to tap into the parsing and rendering. :(

@thediveo
Copy link
Author

thediveo commented Aug 21, 2021

I've adapted markedjs/marked#1562 (comment) to docsify's link handling as follows (not a plugin, that's out of reach for me):

  <script>
    const footnoteMatch = /^\[\^([^\]]+)\]:([\s\S]*)$/
    const referenceMatch = /\[\^([^\]]+)\](?!\()/g
    const referencePrefix = "marked-fnref"
    const footnotePrefix = "marked-fn"
    const footnoteTemplate = (ref, text) => {
      return `<sup id="${footnotePrefix}-${ref}" class="footnote-symbol">${ref}</sup><span class="footnote-text">${text}</span>`
    }
    const footnoteContainerTemplate = (text) => {
      return `<div class="marked-footnotes"><h4>References</h4>${text}</div>`
    }
    const referenceTemplate = (ref) => {
      return `<sup id="${referencePrefix}-${ref}" class="footnote-reference-symbol"><a href="${window.location.hash.split("?")[0]}?id=${footnotePrefix}-${ref}">${ref}</a></sup>`
    }
    const interpolateReferences = (text) => {
      return text.replace(referenceMatch, (_, ref) => {
        return referenceTemplate(ref)
      })
    }
    const interpolateFootnotes = (text) => {
      const found = text.match(footnoteMatch)
      if (found) {
        const replacedText = text.replace(footnoteMatch, (_, value, text) => {
            return footnoteTemplate(value, text)
        })
        return replacedText /* footnoteContainerTemplate(replacedText) */
      }
      return text
    }

    window.$docsify = {
      markdown: function (marked, renderer) {
        marked.setOptions({
          smartypants: true,
          renderer: Object.assign(renderer, {
            paragraph(text) {
              return marked.Renderer.prototype.paragraph.apply(null, [
                interpolateReferences(interpolateFootnotes(text))
            ])},
            text(text) {
              return marked.Renderer.prototype.text.apply(null, [
                interpolateReferences(interpolateFootnotes(text))
            ])},
          }),
        })
        return marked
      },
    ...
  </script>
  • needs an explicit "#### References" or similar, as it never sees the footnote text definition en bloc, but only as individual texts.
  • is there any way to add classes to the outer paragraph?
  • I can't get multiple paragraphs working in a single footnote definition working.

Any ideas?

@onedge
Copy link

onedge commented Jan 25, 2022

I wrote some code to handle footnotes in the [^1] pattern.
I hope this helps you.

index.html

plugins: [
        function(hook, vm) {
          hook.beforeEach(function(html) {
          ...
          // footnote
            if (/\[\^\d+\][^:]/.test(html)) {
              html = html
                .replace(/\[\^(\d+)\][^:]/gm, '<sup class="footnote-symbol" id="ft$1">[\[$1]\](#ftref$1)</sup>')
                .replace(/\[\^(\d+)\]\: /gm, '<strong class="footnote-reference-symbol" id="ftref$1">[\[$1\]](#ft$1)</strong>:leftwards_arrow_with_hook: ');
            }

example.md

> Between people an island exists.  I want visit that island. [^1]

### Reference

[^1]: Island by Jung Hyun-jong [(ref)](https://jaypsong.blog/2011/11/10/island-by-jung-hyun-jong/)

It looks like this.

Between people an island exists. I want visit that island. 1

Reference

[1] Island by Jung Hyun-jong (ref)


If you want to process patterns like [^note] too, do this.

index.html

plugins: [
        function(hook, vm) {
          hook.beforeEach(function(html) {
          ...
          // footnote
            if (/\[\^\.+\][^:]/.test(html)) {
              html = html
                .replace(/\[\^(\.+)\][^:]/gm, '<sup class="footnote-symbol" id="ft$1">[\[$1]\](#ftref$1)</sup>')
                .replace(/\[\^(\.+)\]\: /gm, '<strong class="footnote-reference-symbol" id="ftref$1">[\[$1\]](#ft$1)</strong>:leftwards_arrow_with_hook: ');
            }

@sy-records
Copy link
Member

Hi there.
Thanks @onedge, I have published it as a plugin that can be directly referenced.

<script src="//cdn.jsdelivr.net/npm/@sy-records/docsify-footnotes/lib/index.min.js"></script>

@jhildenbiddle
Copy link
Member

We are likely better served by allowing users to add marked extensions as needed.

@jhildenbiddle jhildenbiddle added this to the 6.x milestone Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants