site/node_modules/remark-frontmatter/lib/index.js
2024-10-14 08:09:33 +02:00

49 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// <reference types="remark-parse" />
/// <reference types="remark-stringify" />
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-frontmatter').Options} Options
* @typedef {import('unified').Processor<Root>} Processor
*/
import {
frontmatterFromMarkdown,
frontmatterToMarkdown
} from 'mdast-util-frontmatter'
import {frontmatter} from 'micromark-extension-frontmatter'
/** @type {Options} */
const emptyOptions = 'yaml'
/**
* Add support for frontmatter.
*
* ###### Notes
*
* Doesnt parse the data inside them: create your own plugin to do that.
*
* @param {Options | null | undefined} [options='yaml']
* Configuration (default: `'yaml'`).
* @returns {undefined}
* Nothing.
*/
export default function remarkFrontmatter(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const settings = options || emptyOptions
const data = self.data()
const micromarkExtensions =
data.micromarkExtensions || (data.micromarkExtensions = [])
const fromMarkdownExtensions =
data.fromMarkdownExtensions || (data.fromMarkdownExtensions = [])
const toMarkdownExtensions =
data.toMarkdownExtensions || (data.toMarkdownExtensions = [])
micromarkExtensions.push(frontmatter(settings))
fromMarkdownExtensions.push(frontmatterFromMarkdown(settings))
toMarkdownExtensions.push(frontmatterToMarkdown(settings))
}