Skip to content

๐ŸŽ“ Parses and validates Markdown frontmatter (YAML) to data object.

License

Notifications You must be signed in to change notification settings

phuctm97/remark-parse-frontmatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

29 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽ“ remark-parse-frontmatter

Github checks npm version Code style GitHub license

Parses and validates Markdown frontmatter (YAML) to file.data.frontmatter.

Validation is done by revalidator.

Built for Remark 12, won't work with Remark 13. Requires remark-frontmatter.

Example

example.js:

const processor = remark()
  .use(require("remark-frontmatter"))
  .use(require("remark-parse-frontmatter"))
  .freeze();

const file = processor.processSync(`
---
title: Hello, World!
---
`);

console.log(file.data.frontmatter);

Output:

{
  title: "Hello, World!"
}

Usage

Install

yarn add remark-parse-frontmatter

Configure

Unified / Remark:

// Without validation
unified()
  .use(require("remark-parse"))
  .use(require("remark-frontmatter"))
  .use(require("remark-parse-frontmatter"))
  .use(require("remark-stringify"));

// With validation
unified()
  .use(require("remark-parse"))
  .use(require("remark-frontmatter"))
  .use(require("remark-parse-frontmatter"), {
    properties: {
      title: { type: "string", required: true },
      tags: { type: "array", maxItems: 4 },
    },
  })
  .use(require("remark-stringify"));
// Without validation.
remark()
  .use(require("remark-frontmatter"))
  .use(require("remark-parse-frontmatter"));

// With validation.
remark()
  .use(require("remark-frontmatter"))
  .use(require("remark-parse-frontmatter"), {
    properties: {
      title: { type: "string", required: true },
      tags: { type: "array", maxItems: 4 },
    },
  });

MDX:

// Without validation.
mdx(mdxText, {
  remarkPlugins: [
    require("remark-unwrap-texts"),
    require("remark-parse-frontmatter"),
  ],
});

// With validation.
mdx(mdxText, {
  remarkPlugins: [
    require("remark-unwrap-texts"),
    [
      require("remark-parse-frontmatter"),
      {
        properties: {
          title: { type: "string", required: true },
          tags: { type: "array", maxItems: 4 },
        },
      },
    ],
  ],
});

Made by @phuctm97.