Package detail

marked-footnote

bent1059.5kMIT1.3.0

A marked extension to support GFM footnotes

marked, marked-extension, gfm, footnote

readme

marked-footnote

A marked extension to support GFM footnotes.

Install

You can install marked-footnote using npm or yarn:

npm i marked-footnote
# or
yarn add marked-footnote

Usage

Once you've installed this extension, you can use it in your marked configuration. Here's an example of how to configure it:

Browser

Say we have the following file example.html:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Marked Footnote</title>

    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown-light.min.css"
      integrity="sha512-bm684OXnsiNuQSyrxuuwo4PHqr3OzxPpXyhT66DA/fhl73e1JmBxRKGnO/nRwWvOZxJLRCmNH7FII+Yn1JNPmg=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />

    <style>
      #content {
        margin: 0 auto;
        padding: 1rem;
        max-width: 928px;
      }

      .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        word-wrap: normal;
        border: 0;
      }
    </style>
  </head>

  <body class="markdown-body">
    <div id="content"></div>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked-footnote/dist/index.umd.min.js"></script>
    <script>
      const md = `# Example

[^1]: This is a footnote content.

Here is a simple footnote[^1]. With some additional text after it[^@#$%] and without disrupting the blocks[^bignote].

[^bignote]: The first paragraph of the definition.

    Paragraph two of the definition.

    > A blockquote with
    > multiple lines.

    ~~~
    a code block
    ~~~

    | Header 1 | Header 2 |
    | -------- | -------- |
    | Cell 1   | Cell 2   |

    A \`final\` paragraph before list.

    - Item 1
    - Item 2
      - Subitem 1
      - Subitem 2

[^@#$%]: A footnote on the label: "@#$%".
`

      document.getElementById('content').innerHTML = new marked.Marked()
        .use(markedFootnote())
        .parse(md)
    </script>
  </body>
</html>

Try marked-footnote on RunKit

Node.js

Say we have the following file example.md:

# Example

[^1]: This is a footnote content.

Here is a simple footnote[^1]. With some additional text after it[^@#$%] and without disrupting the blocks[^bignote].

[^bignote]: The first paragraph of the definition.

    Paragraph two of the definition.

    > A blockquote with
    > multiple lines.

    ```
    a code block
    ```

    | Header 1 | Header 2 |
    | -------- | -------- |
    | Cell 1   | Cell 2   |

    A `final` paragraph before list.

    - Item 1
    - Item 2
      - Subitem 1
      - Subitem 2

[^@#$%]: A footnote on the label: "@#$%".

Note: The position of a footnote in your Markdown does not influence where the footnote will be rendered. You can write a footnote right after your reference to the footnote, and the footnote will still render at the bottom of the Markdown.

And our module example.js looks as follows:

import { readFileSync } from 'node:fs'
import { Marked } from 'marked'
import markedFootnote from 'marked-footnote'

const html = new Marked()
  .use(markedFootnote())
  .parse(readFileSync('example.md', 'utf8'))

console.log(html)

Now, running node example.js yields:

<h1>Example</h1>
<p>
  Here is a simple footnote<sup
    ><a
      id="footnote-ref-1"
      href="#footnote-1"
      data-footnote-ref
      aria-describedby="footnote-label"
      >1</a
    ></sup
  >. With some additional text after it<sup
    ><a
      id="footnote-ref-%40%23%24%25"
      href="#footnote-%40%23%24%25"
      data-footnote-ref
      aria-describedby="footnote-label"
      >2</a
    ></sup
  >
  and without disrupting the blocks<sup
    ><a
      id="footnote-ref-bignote"
      href="#footnote-bignote"
      data-footnote-ref
      aria-describedby="footnote-label"
      >3</a
    ></sup
  >.
</p>
<section class="footnotes" data-footnotes>
  <h2 id="footnote-label" class="sr-only">Footnotes</h2>
  <ol>
    <li id="footnote-1">
      <p>
        This is a footnote content.
        <a
          href="#footnote-ref-1"
          data-footnote-backref
          aria-label="Back to reference 1"
          >↩</a
        >
      </p>
    </li>
    <li id="footnote-%40%23%24%25">
      <p>
        A footnote on the label: &quot;@#$%&quot;.
        <a
          href="#footnote-ref-%40%23%24%25"
          data-footnote-backref
          aria-label="Back to reference @#$%"
          >↩</a
        >
      </p>
    </li>
    <li id="footnote-bignote">
      <p>The first paragraph of the definition.</p>
      <p>Paragraph two of the definition.</p>
      <blockquote>
        <p>A blockquote with multiple lines.</p>
      </blockquote>
      <pre><code>a code block
</code></pre>
      <table>
        <thead>
          <tr>
            <th>Header 1</th>
            <th>Header 2</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Cell 1</td>
            <td>Cell 2</td>
          </tr>
        </tbody>
      </table>
      <p>A <code>final</code> paragraph before list.</p>
      <ul>
        <li>Item 1</li>
        <li>
          Item 2
          <ul>
            <li>Subitem 1</li>
            <li>Subitem 2</li>
          </ul>
        </li>
      </ul>
      <a
        href="#footnote-ref-bignote"
        data-footnote-backref
        aria-label="Back to reference bignote"
        >↩</a
      >
    </li>
  </ol>
</section>

By default, this plugin does not place footnote markers in square brackets ([1]), instead like this: 1. So you will need to add the style as shown below to your CSS:

[data-footnote-ref]::before {
  content: '[';
}

[data-footnote-ref]::after {
  content: ']';
}

Options

The marked-footnote extension accepts the following configuration options:

  • prefixId: The prefix ID for footnotes. Defaults to 'footnote-'.

  • prefixData: The prefix for the main data attribute for footnotes. Defaults to '', an empty string, making the attribute equal data-footnotes.

  • description: The description of footnotes, used by aria-labeledby attribute. Defaults to 'Footnotes'.

  • refMarkers: If set to true, it will place footnote reference in square brackets, like this: [1]. Defaults to false.

  • footnoteDivider: If set to true, it will insert a horizontal rule above the footnotes at the bottom of the page. Defaults to false.

  • sectionClass: The CSS class set to the element wrapping all footnotes at the bottom of the page. Can be set to an empty string to remove the CSS class. Defaults to 'footnotes'.

  • headingClass: The CSS class set to the heading element introducing the footnotes at the bottom of the page for screen reader users. Can be set to an empty string to remove the CSS class. Defaults to 'sr-only'.

  • backRefLabel: ARIA label of the links referring back to the location in the text of the page where the footnote has been referred from. Defaults to 'Back to reference {0}'. The placeholder {0} will be replaced by the footnote marker text.

Limitations

When considering the use of footnotes in your content, it's important to keep in mind the following accessibility and usability factors:

  1. Screen Reader Compatibility: Screen readers may not effectively convey footnotes. They tend to read the footnote number without indicating that it's a footnote or using superscript. Additionally, they may not identify the link to the footnote text.
  2. Accessibility Challenges: Footnotes pose challenges for all users on a web page. To access them, one often needs to scroll to the end of the page, read the footnote, and then click back to the main content. Not everyone is aware that they should click on the footnote at the end, potentially causing them to lose their place. Moreover, if the same footnote is repeated multiple times, clicking on the link could lead to the wrong location.

Related

See extensions list.

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2023-2024.

changelog

marked-footnote 1.3.0 (2025-06-21)

Features

  • Enable custom CSS classes, footnote divider, localisation, source (d56e749)

marked-footnote 1.2.4 (2024-09-02)

Bug Fixes

  • footnote: resolves footnote links for repeated, out-of-order references (7e48c89), closes #109

marked-footnote 1.2.3 (2024-08-29)

Bug Fixes

marked-footnote 1.2.2 (2023-12-16)

Bug Fixes

  • ensure the footnotes token is consistently reset during each parsing process (6f4d6d9), closes #34

marked-footnote 1.2.1 (2023-12-13)

Bug Fixes

  • preserve the value of the first token by leaving a space to ensure that the subsequent item is properly accessed (3e26ab5), closes #27
  • reset refs number on each parsing (d72b9da), closes #28

marked-footnote 1.2.0 (2023-12-11)

Features

  • use sequential number for footnote refs and resolve footnote order (1d53da3), closes #26

marked-footnote 1.1.3 (2023-11-26)

Bug Fixes

  • allows link in footnote content (a3e49ac), closes #23

marked-footnote 1.1.2 (2023-11-03)

Bug Fixes

  • always begin with empty footnotes items (94aa01a)

marked-footnote 1.1.1 (2023-10-26)

Bug Fixes

  • resolves ERR_REQUIRE_ESM in CommonJS mocules (f876e00)

marked-footnote 1.1.0 (2023-10-20)

Bug Fixes

  • resolves umd mime issue (f98d31a)

Features

  • build for the esm, cjs, and umd formats (30bc148)

marked-footnote 1.0.1 (2023-10-18)

Bug Fixes

  • removes the "dir" attribute from the footnotes heading (152f77e)

marked-footnote 1.0.0 (2023-10-10)

Features