@gatilab/emdash-footnotes
v0.1.0
Published
Accessible footnotes for EmDash: a Portable Text footnote mark, the WordPress [fn]...[/fn] shortcode, numbered superscript references, hover previews and a footnotes list. Port of the Marketers Delight Footnotes drop-in.
Maintainers
Readme
Footnotes for EmDash
Numbered, accessible footnotes for EmDash. Type [fn]Your note[/fn] in a paragraph and the reader gets a superscript number with a hover preview, plus a numbered list at the end of the entry with links back to every citation.
This is the EmDash port of the Marketers Delight Footnotes drop-in. It reads the same [fn] shortcode, numbers notes the same way and uses the same element ids and class names, so content migrated from WordPress renders as it did there, links to #footnote_… included.
Install
pnpm add @gatilab/emdash-footnotes// astro.config.mjs
import emdash from "emdash/astro";
import { footnotes } from "@gatilab/emdash-footnotes";
export default defineConfig({
integrations: [
emdash({
plugins: [footnotes({ preview: "tooltip" })],
}),
],
});Then render entry content with <FootnotesPortableText> instead of EmDash's <PortableText>:
---
import { FootnotesPortableText } from "@gatilab/emdash-footnotes/astro";
---
<FootnotesPortableText value={post.data.content} />It takes every <PortableText> prop (components, listNestingMode, …) and passes them on. In visual-editing mode it hands the value through untouched, so editors always edit the raw [fn] text.
EmDash has no global content filter like WordPress's the_content, so the shortcodes are processed where content renders. A plain <PortableText> prints [fn] as text.
Writing footnotes
| You write | You get |
| --- | --- |
| [fn]Measured on five sites.[/fn] | An inline note with its own number. |
| [fn id="hosts"] | A named note, defined in the list block or in definitions. |
| [fn id="hosts"]Hosts tested: A, B.[/fn] | Defines hosts and cites it. Later [fn id="hosts"] reuse it. |
| [fn align="right"]…[/fn] | Puts this margin note on the right (sidenote mode). |
| [[fn]], [[fn]…[/fn]] | The literal text [fn], [fn]…[/fn]. |
- Formatting inside a note is kept. In the editor,
[fn]See+ Smith +, p. 4[/fn]is one note with italics, and links work too. This is also how the WordPress importer stores[fn]See <em>Smith</em>[/fn]. - Shortcodes inside
codeformatting and code blocks are left as text. - A footnote cited twice keeps one number. Each citation gets its own id (
footnote_hosts,footnote_hosts_2), and the list links back to each one. - Every inline note is a separate footnote, numbered
inline-1,inline-2… like the drop-in. Turn onmergeDuplicatesto give identical inline notes one number. - Notes with no text print nothing, as in the drop-in.
The Footnotes list block
Editors get one block, Footnotes list, in the slash menu:
- Where it sits, the list renders. Without it, the list goes at the end of the entry. A second list block in the same entry renders nothing.
- Heading overrides the list heading for this entry.
- Named footnotes rows (ID + text) define notes for
[fn id="…"]. This replaces the drop-in's Footnotes meta box. Row text may contain inline HTML: links,<em>,<strong>,<code>.
Notes cited after the block still appear in its list.
Footnote annotations
Content written through the API or migrated from another Portable Text source may carry footnotes as annotations:
{ "_type": "block", "markDefs": [{ "_key": "n1", "_type": "footnote", "text": "Doe, <em>Journal</em>, 1997." }],
"children": [{ "_type": "span", "text": "the 1997 study", "marks": ["n1"] }] }These render too: the reference goes right after the annotated text. text may hold inline HTML; body may hold Portable Text blocks or spans. Note the editor limitation below before storing content this way.
Options
Pass options to footnotes() in astro.config.mjs, set them in the admin under the plugin's settings, or pass them as props to any component. Props win over admin settings, and admin settings win over astro.config.mjs.
| Option | Default | Drop-in equivalent |
| --- | --- | --- |
| preview | "tooltip" | "sidenote" is the drop-in's margin notes; "toggle" is its "Always toggle footnotes". "none" makes the number a plain link to the list. |
| showList | true | md_footnotes_show_list filter. With the list off, numbers become buttons that open the note. |
| heading | "Footnotes" | md_footnotes_list_title filter. An empty heading stays available to screen readers. |
| headingLevel | 4 | The drop-in's <h4>. |
| numbering | "decimal" | New: lower-roman, upper-roman, lower-alpha, upper-alpha, symbols (*, †, ‡, §, ‖, ¶, then doubled). |
| backlinkSymbol | "↩︎" | The drop-in's ↩, with a text-presentation selector so iOS doesn't draw an emoji. |
| backlinkLabel | "Back to reference %s" | Same label; %s is the footnote label. |
| closeLabel | "Close footnote" | Same label. |
| definitions | {} | The meta box rows: { hosts: "Hosts tested: <em>A</em>, B." }. List-block rows override these. |
| mergeDuplicates | false | New. |
| idPrefix | "" | New. Set it when one page renders several entries, so ids stay unique, such as "post-12-". |
| listId | "footnotes" | The drop-in's #footnotes. |
| baseUrl | "" | The drop-in prefixed links with the permalink. Leave it empty on the entry's own page; set it for excerpts shown elsewhere. |
Plugin-only: stripMetadata (default true) removes [fn] notes from the description, Open Graph and Twitter tags <EmDashHead> renders, through a page:metadata hook.
Components
---
import { FootnotesPortableText, FootnotesText, FootnotesList } from "@gatilab/emdash-footnotes/astro";
---
<!-- Entry content -->
<FootnotesPortableText value={post.data.content} preview="sidenote" numbering="lower-roman" />
<!-- A plain string, or legacy HTML (footnote text is sanitized, the rest is yours) -->
<FootnotesText text={post.data.excerpt} />
<FootnotesText text={legacyHtml} html />
<!-- The list somewhere else in the template -->
<FootnotesPortableText value={post.data.content} appendList={false} />
<aside><FootnotesList footnotes={footnotes} /></aside>For the last pattern, get footnotes from processFootnotes(value, { appendList: false }). <FootnoteRef> and <FootnotesList> are also registered with EmDash as renderers for the footnoteRef and footnotesList types, so you can override either through components.
Styling
The components import a small stylesheet. It inherits the site's fonts and sets no colors you can't change. Every value is a custom property with a fallback, so set any of them on :root or on your content wrapper:
:root {
--footnote-accent: #c0392b;
--footnote-badge-bg: #f6f6f4;
--footnote-tooltip-width: 26rem;
}| Property | Used for |
| --- | --- |
| --footnote-accent | Number text and focus rings (default currentColor) |
| --footnote-badge-bg, --footnote-badge-bg-hover | Number badge |
| --footnote-surface, --footnote-ink | Tooltip background and text (default system Canvas / CanvasText) |
| --footnote-muted, --footnote-border, --footnote-shadow, --footnote-radius | Details |
| --footnote-font-size | Preview and list text (default 0.875em) |
| --footnote-tooltip-width | Tooltip max width |
| --footnote-sidenote-width, --footnote-sidenote-gap | Margin notes |
Sidenotes float into the margin from 75rem (1200px) wide and toggle below that, so they need empty space beside the text column. The stylesheet is also exported on its own: @gatilab/emdash-footnotes/styles/footnotes.css. Class names match the drop-in (.footnote, .footnote-number, .footnote-text, .footnotes), so ported theme CSS keeps working.
Accessibility
- The reference is
<sup><a role="doc-noteref">linking to its list item. Itsaria-describedbypoints at the note text, so a screen reader reads the note on focus. In"none"mode it points at the list heading. - The list is
<section role="doc-endnotes">labelled by its heading. Each item links back to each citation withrole="doc-backlink"and a label such as "Back to reference 2 (1)". - Tooltips open on hover and on keyboard focus, stay open while the pointer moves onto them, and close with Escape (WCAG 1.4.13).
- Toggles are real links or buttons with
aria-expandedandaria-controls. Escape closes open notes and returns focus to the number when focus was inside the note. The close button has an accessible name. - Without JavaScript, numbers still link to the list, and tooltips still work.
Core API
Framework-free functions from @gatilab/emdash-footnotes/core (also re-exported from the package root):
| Function | |
| --- | --- |
| processFootnotes(value, options) | Portable Text in, { value, footnotes, listHtml, hasListBlock } out. Never mutates the input. |
| processFootnotesString(text, options) | A string in, { html, footnotes, listHtml } out. html: true treats the input as HTML. |
| stripFootnotes(text) | Removes notes, for meta tags and feeds. |
| footnoteAnnotationsToShortcodes(value) | Rewrites footnote annotations as [fn] text so the editor can open the content. |
| renderFootnoteRef, renderFootnotesList | The markup on its own. |
| matchFootnotes, scanFootnoteTokens, parseAttributes | The shortcode scanner. |
| formatFootnoteNumber(n, style) | Labels: formatFootnoteNumber(4, "symbols") gives §. |
| sanitizeInlineHtml, spansToHtml | Note text sanitizing and span rendering. |
EmDash limitations
Written against emdash 1.4.0:
- No editor mark for footnotes. Plugins can add Portable Text blocks (
admin.portableTextBlocks) and field widgets, but not marks or annotations. The editor's mark set is fixed, and it refuses to open a field whose spans carry any other mark ("unsupported formatting"). So the shortcode is the authoring path in the editor, and footnote annotations are rendered but not editable. RunfootnoteAnnotationsToShortcodes()on such content before storing it if editors need to touch it. - No content filter. Plugin component entries can only register renderers for block and inline object types, so footnotes are processed by
<FootnotesPortableText>, not by EmDash's own<PortableText>. - Metadata tags. EmDash keeps the first contribution for each meta tag. If another plugin rewrites the same description (for example, Dynamic Dates resolving
[year]), whichever runs first wins for descriptions that contain both kinds of shortcode.
Differences from the drop-in
- Tooltip previews are the default. The drop-in's margin notes are
preview: "sidenote". - The number is a link to the list (a button only when the list is off) instead of always a button.
[fn id="x"]Text[/fn]with no matching definition keeps the idx, so later[fn id="x"]can cite it. The drop-in renamed itinline-N.- In
[fn id="a"] … [fn]b[/fn], the first tag is a reference. WordPress's parser would have swallowed the second note into the first. - The note preview is a
<span role="note">, not<cite>, which is meant for titles of works.
License
MIT. Copyright (c) 2026 Gatilab.
