Gathering detailed insights and metrics for micromark-extension-gfm-footnote
Gathering detailed insights and metrics for micromark-extension-gfm-footnote
Gathering detailed insights and metrics for micromark-extension-gfm-footnote
Gathering detailed insights and metrics for micromark-extension-gfm-footnote
micromark extension to support GFM footnotes
npm install micromark-extension-gfm-footnote
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
67 Commits
3 Forks
6 Watching
1 Branches
13 Contributors
Updated on 02 Sept 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.7%
586,645
Compared to previous day
Last week
4%
3,350,162
Compared to previous week
Last month
10.7%
13,771,481
Compared to previous month
Last year
127.3%
125,015,519
Compared to previous year
8
micromark extensions to support GFM footnotes.
This package contains extensions that add support for footnotes as enabled by
GFM to micromark
.
GitHub announced footnotes on September 30, 2021 but did not specify them in their GFM spec. As they are implemented in their parser and supported in all places where other GFM features work, they can be considered part of GFM. GitHub employs several other features (such as mentions or frontmatter) that are either not in their parser, or not in all places where GFM features work, which should not be considered GFM.
The implementation of footnotes on github.com is currently buggy.
The bugs have been reported on cmark-gfm
.
This micromark extension matches github.com except for its bugs.
This project is useful when you want to support footnotes in markdown.
You can use these extensions when you are working with micromark
.
To support all GFM features, use
micromark-extension-gfm
instead.
When you need a syntax tree, combine this package with
mdast-util-gfm-footnote
.
All these packages are used in remark-gfm
, which focusses on
making it easier to transform content by abstracting these internals away.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install micromark-extension-gfm-footnote
In Deno with esm.sh
:
1import {gfmFootnote, gfmFootnoteHtml} from 'https://esm.sh/micromark-extension-gfm-footnote@2'
In browsers with esm.sh
:
1<script type="module"> 2 import {gfmFootnote, gfmFootnoteHtml} from 'https://esm.sh/micromark-extension-gfm-footnote@2?bundle' 3</script>
Say our document example.md
contains:
1Using footnotes is fun![^1] They let you reference relevant information without disrupting the flow of what you’re trying to say.[^bignote]
2
3[^1]: This is the first footnote.
4[^bignote]: Here’s one with multiple paragraphs and code.
5
6 Indent paragraphs to include them in the footnote.
7
8 ```
9 my code
10 ```
11
12 Add as many paragraphs as you like.
13
14Text here and here and here.
15[Learn more about markdown and footnotes in markdown](https://docs.github.com/en/github/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#footnotes)
…and our module example.js
looks as follows:
1import fs from 'node:fs/promises' 2import {micromark} from 'micromark' 3import {gfmFootnote, gfmFootnoteHtml} from 'micromark-extension-gfm-footnote' 4 5const output = micromark(await fs.readFile('example.md'), { 6 extensions: [gfmFootnote()], 7 htmlExtensions: [gfmFootnoteHtml()] 8}) 9 10console.log(output)
…now running node example.js
yields:
1<p>Using footnotes is fun!<sup><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref="" aria-describedby="footnote-label">1</a></sup> They let you reference relevant information without disrupting the flow of what you’re trying to say.<sup><a href="#user-content-fn-bignote" id="user-content-fnref-bignote" data-footnote-ref="" aria-describedby="footnote-label">2</a></sup></p> 2<p>Text here and here and here. 3<a href="https://docs.github.com/en/github/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#footnotes">Learn more about markdown and footnotes in markdown</a></p> 4<section data-footnotes="" class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2> 5<ol> 6<li id="user-content-fn-1"> 7<p>This is the first footnote. <a href="#user-content-fnref-1" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩</a></p> 8</li> 9<li id="user-content-fn-bignote"> 10<p>Here’s one with multiple paragraphs and code.</p> 11<p>Indent paragraphs to include them in the footnote.</p> 12<pre><code>my code 13</code></pre> 14<p>Add as many paragraphs as you like. <a href="#user-content-fnref-bignote" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩</a></p> 15</li> 16</ol> 17</section>
This package exports the identifiers
defaultBackLabel
,
gfmFootnote
, and
gfmFootnoteHtml
.
There is no default export.
The export map supports the development
condition.
Run node --conditions development module.js
to get instrumented dev code.
Without this condition, production code is loaded.
defaultBackLabel(referenceIndex, rereferenceIndex)
Generate the default label that GitHub uses on backreferences
(BackLabelTemplate
).
gfmFootnote()
Create an extension for micromark
to enable GFM footnote syntax.
Extension for micromark
that can be passed in extensions
to enable GFM
footnote syntax (Extension
).
gfmFootnoteHtml(options?)
Create an extension for micromark
to support GFM footnotes when serializing
to HTML.
options
(HtmlOptions
, optional)
— configurationExtension for micromark
that can be passed in htmlExtensions
to support GFM
footnotes when serializing to HTML
(HtmlExtension
).
BackLabelTemplate
Generate a back label dynamically (TypeScript type).
For the following markdown:
1Alpha[^micromark], bravo[^micromark], and charlie[^remark]. 2 3[^remark]: things about remark 4[^micromark]: things about micromark
This function will be called with:
0
and 0
for the backreference from things about micromark
to
alpha
, as it is the first used definition, and the first call to it0
and 1
for the backreference from things about micromark
to
bravo
, as it is the first used definition, and the second call to it1
and 0
for the backreference from things about remark
to
charlie
, as it is the second used definitionreferenceIndex
(number
)
— index of the definition in the order that they are first referenced,
0-indexedrereferenceIndex
(number
)
— index of calls to the same definition, 0-indexedBack label to use when linking back from definitions to their reference
(string
).
HtmlOptions
Configuration (TypeScript type).
clobberPrefix
Prefix to use before the id
attribute on footnotes to prevent them from
clobbering (string
, default: 'user-content-'
).
Pass ''
for trusted markdown and when you are careful with polyfilling.
You could pass a different prefix.
DOM clobbering is this:
1<p id="x"></p> 2<script>alert(x) // `x` now refers to the `p#x` DOM element</script>
The above example shows that elements are made available by browsers, by their
ID, on the window
object.
This is a security risk because you might be expecting some other variable at
that place.
It can also break polyfills.
Using a prefix solves these problems.
label
Textual label to use for the footnotes section (string
, default:
'Footnotes'
).
Change it when the markdown is not in English.
This label is typically hidden visually (assuming a sr-only
CSS class
is defined that does that) and so affects screen readers only.
labelAttributes
Attributes to use on the footnote label (string
, default:
'class="sr-only"'
).
Change it to show the label and add other attributes.
This label is typically hidden visually (assuming an sr-only
CSS class
is defined that does that) and so affects screen readers only.
If you do have such a class, but want to show this section to everyone,
pass an empty string.
You can also add different attributes.
👉 Note:
id="footnote-label"
is always added, because footnote calls use it witharia-describedby
to provide an accessible label.
labelTagName
HTML tag name to use for the footnote label element (string
, default:
'h2'
).
Change it to match your document structure.
This label is typically hidden visually (assuming a sr-only
CSS class
is defined that does that) and so affects screen readers only.
backLabel
Textual label to describe the backreference back to footnote calls
(BackLabelTemplate
or string
,
default: defaultBackLabel
).
Change it when the markdown is not in English.
This label is used in the aria-label
attribute on each
backreference (the ↩
links).
It affects users of assistive technology.
GitHub’s own algorithm to parse footnote definitions contains several bugs. These are not present in this project. The issues relating to footnote definitions are:
href
]
does not work in footnote
identifiersCommonMark
prevents links in links, GitHub does not prevent
footnotes (which turn into links) in linksWhen authoring markdown with footnotes it’s recommended to use words instead of numbers (or letters or anything with an order) as identifiers. That makes it easier to reuse and reorder footnotes.
It’s recommended to place footnotes definitions at the bottom of the document.
GFM footnotes do not, on their own, relate to anything in HTML. When a footnote reference matches with a definition, they each relate to several elements in HTML.
The reference relates to <sup>
and <a>
elements in HTML:
1<sup><a href="#user-content-fn-x" id="user-content-fnref-x" data-footnote-ref="" aria-describedby="footnote-label">1</a></sup></p>
…where x
is the identifier used in the markdown source and 1
the number of
corresponding, listed, definition.
See § 4.5.19 The sub
and sup
elements,
§ 4.5.1 The a
element, and
§ 3.2.6.6 Embedding custom non-visible data with the data-*
attributes
in the HTML spec, and
§ 6.8 aria-describedby
property
in WAI-ARIA, for more info.
When one or more definitions are referenced, a footnote section is generated at
the end of the document, using <section>
, <h2>
, and <ol>
elements:
1<section data-footnotes="" class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2> 2<ol>…</ol> 3</section>
Each definition is generated as a <li>
in the <ol>
in the order they were
first referenced:
1<li id="user-content-fn-1">…</li>
Backreferences are injected at the end of the first paragraph, or, when there
is no paragraph, at the end of the definition.
When a definition is referenced multiple times, multiple backreferences are
generated.
Further backreferences use an extra counter in the href
attribute and
visually in a <span>
after ↩
.
1<a href="#user-content-fnref-1" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩</a> <a href="#user-content-fnref-1-2" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content">↩<sup>2</sup></a>
See
§ 4.5.1 The a
element,
§ 4.3.6 The h1
, h2
, h3
, h4
, h5
, and h6
elements,
§ 4.4.8 The li
element,
§ 4.4.5 The ol
element,
§ 4.4.1 The p
element,
§ 4.3.3 The section
element, and
§ 4.5.19 The sub
and sup
elements
in the HTML spec, and
§ 6.8 aria-label
property
in WAI-ARIA, for more info.
The following CSS is needed to make footnotes look a bit like GitHub (and fixes
a bug).
For the complete actual CSS see
sindresorhus/github-markdown-css
.
1/* Style the footnotes section. */ 2.footnotes { 3 font-size: smaller; 4 color: #8b949e; 5 border-top: 1px solid #30363d; 6} 7 8/* Hide the section label for visual users. */ 9.sr-only { 10 position: absolute; 11 width: 1px; 12 height: 1px; 13 padding: 0; 14 overflow: hidden; 15 clip: rect(0, 0, 0, 0); 16 word-wrap: normal; 17 border: 0; 18} 19 20/* Place `[` and `]` around footnote references. */ 21[data-footnote-ref]::before { 22 content: '['; 23} 24 25[data-footnote-ref]::after { 26 content: ']'; 27}
Footnotes form with, roughly, the following BNF:
1gfmFootnoteReference = gfmFootnoteLabel 2 3gfmFootnoteDefinitionStart = gfmFootnoteLabel ":" *spaceOrTab 4; Restriction: blank line allowed. 5gfmFootnoteDefinitionCont = 4(spaceOrTab) 6 7; Restriction: maximum `999` codes between `^` and `]`. 8gfmFootnoteLabel = "[" "^" 1*(gfmFootnoteLabelByte / gfmFootnoteLabelEscape) "]" 9gfmFootnoteLabelByte = text - "[" - "\\" - "]" 10gfmFootnoteLabelEscape = "\\" ["[" / "\\" / "]"] 11 12; Any byte (u8) 13byte = %x0000-FFFF 14spaceOrTab = "\t" / " " 15eol = "\n" / "\r" / "\r\n" 16line = byte - eol 17text = line - spaceOrTab
Further lines after gfm_footnote_definition_start
that are not prefixed with
gfm_footnote_definition_cont
cause the footnote definition to be exited,
except when those lines are lazy continuation or blank.
Like so many things in markdown, footnote definition too are complex.
See § Phase 1: block structure in CommonMark
for more
on parsing details.
The identifiers in the label
parts are interpreted as the
string content type.
That means that character escapes and character references are allowed.
Definitions match to references through identifiers.
To match, both labels must be equal after normalizing with
normalizeIdentifier
.
One definition can match to multiple calls.
Multiple definitions with the same, normalized, identifier are ignored: the
first definition is preferred.
To illustrate, the definition with the content of x
wins:
1[^a]: x 2[^a]: y 3 4[^a]
Importantly, while labels can include string content (character escapes and character references), these are not considered when matching. To illustrate, neither definition matches the reference:
1[^a&b]: x 2[^a\&b]: y 3 4[^a&b]
Because footnote definitions are containers (like block quotes and list items), they can contain more footnote definitions. They can even include references to themselves.
This package is fully typed with TypeScript.
It exports the additional types BackLabelTemplate
and HtmlOptions
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
micromark-extension-gfm-footnote@^2
, compatible with Node.js 16.
This package works with micromark
version 3
and later.
This package is safe.
Setting clobberPrefix = ''
is dangerous, it opens you up to DOM clobbering.
The labelTagName
and labelAttributes
options are unsafe when used with user
content, they allow defining arbitrary HTML.
micromark-extension-gfm
— support all of GFMmdast-util-gfm-footnote
— support all of GFM in mdastmdast-util-gfm
— support all of GFM in mdastremark-gfm
— support all of GFM in remarkSee contributing.md
in micromark/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More