remark-mdx-frontmatter

repository·main·Indexed 41 days ago

https://github.com/remcohaszing/remark-mdx-frontmatter

A remark plugin for converting YAML or TOML frontmatter metadata into JavaScript exports within MDX files. Version 5.2.0 requires Node.js 18 or greater and depends on remark-frontmatter. It allows metadata to be accessed as variables via configurable options for default values, export names, and custom parsers.

Tokens
787
Snippets
3
Records
5
Agent score
0%

What's inside remark-mdx-frontmatter

  1. Convert frontmatter to MDX exports

    main

    The remark-mdx-frontmatter plugin converts YAML or TOML frontmatter into JavaScript export statements within an MDX document. This allows you to access your metadata as variables within your MDX components or application code.

    import { readFile } from 'node:fs/promises'
    
    import { compile } from '@mdx-js/mdx'
    import remarkFrontmatter from 'remark-frontmatter'
    import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
    
    const { value } = await compile(await readFile('example.mdx'), {
      jsx: true,
      remarkPlugins: [remarkFrontmatter, remarkMdxFrontmatter]
    })
    console.log(value)
  2. Configure remark-mdx-frontmatter options

    main

    You can pass an options object to the plugin to customize how frontmatter is exported. The plugin also supports unist-util-mdx-define options.

    OptionTypeDefaultDescription
    defaultanyundefinedThe value to export if no frontmatter data is found.
    namestring'frontmatter'The identifier name of the variable the frontmatter data is assigned to.
    parsersRecord<string, Function>(YAML/TOML defaults)A mapping of node types to parser functions. Each key is a frontmatter node type; the value is a function that accepts the raw string and returns parsed data.
  3. Use remarkMdxFrontmatter to export frontmatter

    main

    The remarkMdxFrontmatter plugin transforms MDX AST by finding frontmatter nodes (like yaml or toml) and converting them into MDX named exports using unist-util-mdx-define.

    If you use the name option, the parsed data is exported as a single object: export const name = { ... }.

    If you do not provide a name, each top-level key in your frontmatter becomes an individual export: export const key = value.

    import { unified } from 'unified'
    import remarkParse from 'remark-parse'
    import remarkMdx from 'remark-mdx'
    import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
    
    // Usage in a unified pipeline
    await unified()
      .use(remarkParse)
      .use(remarkMdxFrontmatter, { name: 'frontmatter' })
      .use(remarkMdx)
      .process('---\ntitle: Hello\n---
    # Content')