Parcel v2 transformer for loading markdown with parsed front-matter.
This is the parcel v2 successor of the parcel v1 plugin parcel-plugin-md-fm
(see Migrating from v1).
Warning
Renamed package and repository. This project was previously published to npm as
parcel-plugin-md-fm (repository: umstek/parcel-plugin-md-fm). The old npm package is
deprecated and will not receive further updates — switch to parcel-transformer-md-fm
via Migrating from v1. Old GitHub links redirect to this repository
automatically.
- Parcel v2
- Node.js >= 20
With pnpm:
pnpm add -D parcel-transformer-md-fmWith npm:
npm install parcel-transformer-md-fm --save-devWith yarn:
yarn add --dev parcel-transformer-md-fmParcel v2 has no automatic plugin discovery; wire the transformer into a
.parcelrc in your project root:
{
"extends": "@parcel/config-default",
"transformers": {
"*.md": ["parcel-transformer-md-fm"],
"*.mdown": ["parcel-transformer-md-fm"],
"*.markdown": ["parcel-transformer-md-fm"]
}
}Importing a markdown file gives you an object with the markdown content
(without the front-matter block) and the parsed front-matter as data:
import { content, data } from "./file.md";or as a default import:
import frontMatter from "./file.md";
console.log(frontMatter.content, frontMatter.data);If you get errors or red underlines in editors, add a markdown.d.ts file declaring
the extensions you import (shown for *.md; repeat for *.mdown and *.markdown):
declare module "*.md" {
export const content: string;
export const data: Record<string, unknown>;
const frontMatter: {
content: string;
data: Record<string, unknown>;
isEmpty: boolean;
excerpt: string;
};
export default frontMatter;
}pnpm install
pnpm testThe suite includes unit tests for the transformer and an integration test that
runs a real parcel build in a scratch consumer project
(test/integration/fixture) and asserts on the
bundle output. The example directory contains a small browser app
using the transformer.
This uses the excellent gray-matter library under the hood which separates
and then translates yaml or other front-matter from a markdown file. See
jonschlinkert/gray-matter
for the exact result format.
Markdown is returned as a string (content) without further parsing so that you can use it with a parser/formatter of your choice like markdown-to-jsx (if you're using react).
If you used the parcel v1 plugin parcel-plugin-md-fm:
- Upgrade your project to parcel v2 (v1 plugins do not work with parcel v2).
- Remove
parcel-plugin-md-fmand installparcel-transformer-md-fm. Parcel v2 validates theparcel-transformer-*naming for transformers referenced from.parcelrc, so the package was renamed as part of the migration. - Add the
.parcelrcwiring shown above. Parcel v1 discoveredparcel-plugin-*packages automatically; parcel v2 requires explicit configuration. - Markdown imports keep the same shape: the exported object still contains
content,data,isEmptyandexcerpt. The one deliberate change: theorigfield (a JSON-serialized Buffer of the whole source file) is no longer embedded in the bundle output.
The renamed package restarts versioning at 0.1.0; there is no semver
continuity with parcel-plugin-md-fm releases.