unified

Project: manovotny/remark-mdx-metadata

Package: remark-mdx-metadata@1.0.1

  1. Dependents: 0
  2. Remark transformer for modifying MDX metadata.
  1. remark 214
  2. markdown 154
  3. remark-plugin 82
  4. mdx 35
  5. meta 10

remark-mdx-metadata

Remark transformer for modifying MDX metadata.

This is a remark plugin for externally modifying an MDX's metadata, which is useful for when you want to add or update properties like a last edited datetime or a link to edit on GitHub.

Installation

NPM

$ npm i remark-mdx-metadata

Yarn

$ yarn add remark-mdx-metadata

Usage

This plugin requires remark-mdx to parse mdx correctly,

Say we have the following file, example.mdx:

export const meta = {
    existingProp: 'existing value'
};

# Title

Content.

And our script, example.js, looks as follows:

const vfile = require('to-vfile');
const remark = require('remark');
const mdx = require('remark-mdx');
const mdxMetadata = require('remark-mdx-metadata');

(async () => {
    const file = await vfile.read('example.mdx');
    const result = await remark()
        .use(mdx)
        .use(mdxMetadata, {
            meta: {
                lastEdited: `${new Date().toISOString()}`
            }
        })
        .process(file);

    console.log(result.toString());
})();

Now, running node example yields:

export const meta = {
    existingProp: 'existing value',
    lastEdited: '2018-09-02T18:58:18.000Z'
};

# Title

Content.

You can try this yourself by downloading or cloning the project, installing dependencies, and running yarn example.

API

remark().use(mdxMetadata[, options])

Adds or updates MDX metadata with the metadata supplied.

Options

meta

Type: Object

Specifies the metadata to add or update.

License

MIT © Michael Novotny