mdformat-footnote
mdformat-footnote is a plugin for the opinionated Markdown formatter `mdformat`. It extends `mdformat` to properly parse, validate, and format Pandoc-style footnotes, ensuring consistent styling within Markdown documents. The current version is 0.1.3, and releases are tied to supporting new `mdformat` versions or addressing specific formatting issues.
Common errors
-
Error: Could not format "/path/to/your_file.md". The formatted Markdown renders to different HTML than the input Markdown. This is likely a bug in mdformat. Please create an issue report here...
cause This error often occurs when `mdformat` (or a plugin like `mdformat-footnote`) encounters Markdown syntax it cannot correctly parse or format without changing the semantic meaning of the document. For footnotes, this might indicate an issue with how the plugin is interpreting or reordering them, or a conflict with other extensions.fixFirst, ensure `mdformat-footnote` is installed and the 'footnote' extension is enabled. If the problem persists, try isolating the footnote-related content to confirm it's an `mdformat-footnote` issue. The `mdformat` core maintainers fixed similar issues in early versions of `mdformat-footnote` (e.g., Issue #6), so upgrading to the latest version might resolve it. If the issue is with 'orphan' footnotes (definitions without references), consider using the `--keep-footnote-orphans` CLI option or `keep_orphans=True` in the API. -
mdformat: error: unrecognized arguments: --footnote
cause The `--footnote` argument is not a valid way to enable the `mdformat-footnote` plugin. `mdformat` uses a generic `--extensions` flag for all parser extension plugins.fixUse the `--extensions` flag with the value `footnote`. Correct CLI usage is: `mdformat --extensions footnote your_file.md`. -
ModuleNotFoundError: No module named 'mdformat_footnote'
cause The `mdformat-footnote` package is not installed in the current Python environment or is not accessible to `mdformat`.fixInstall the package using pip: `pip install mdformat-footnote`. If using `pipx`, ensure it's injected into the `mdformat` virtual environment: `pipx inject mdformat mdformat-footnote`.
Warnings
- breaking mdformat-footnote v0.1.2 upgraded to support mdformat v1 and Python >=3.10. Older versions of mdformat-footnote will not work with mdformat v1+ or Python versions below 3.10.
- gotcha mdformat is a CommonMark formatter. Without the `mdformat-footnote` plugin enabled, any footnote syntax will be treated as non-CommonMark and may be backslash-escaped or otherwise incorrectly handled, leading to a loss of intended Markdown structure.
- gotcha The `mdformat` core documentation recommends pinning `mdformat` dependency versions because its formatting style may change in each version. This also applies to `mdformat-footnote` and could lead to unexpected reformatting.
Install
-
pip install mdformat mdformat-footnote -
pipx install mdformat pipx inject mdformat mdformat-footnote
Imports
- mdformat
from mdformat_footnote import FootnotePlugin
import mdformat # No direct import for mdformat_footnote's plugin module is typically needed for userland code, as mdformat loads it.
Quickstart
import mdformat
markdown_with_footnotes = (
"""
Here is a footnote reference.[^1]
And here's a longer one.[^bignote]
[^1]: This is the first footnote.
[^bignote]: Here's one with multiple paragraphs and code.
Subsequent paragraphs are indented to show that they belong to the previous footnote.
"""
)
# Using the Python API to format with the footnote extension
formatted_markdown = mdformat.text(markdown_with_footnotes, extensions={'footnote'})
print("--- Original ---")
print(markdown_with_footnotes)
print("\n--- Formatted ---")
print(formatted_markdown)
# To use via CLI (assuming mdformat and mdformat-footnote are installed):
# Save the above markdown to a file, e.g., 'input.md'
# Then run: mdformat --extensions footnote input.md