Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdast-util-frontmatter

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-frontmatter

mdast extension to parse and serialize frontmatter (YAML, TOML, etc)

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2M
decreased by-9.86%
Maintainers
2
Weekly downloads
 
Created

What is mdast-util-frontmatter?

The `mdast-util-frontmatter` package is a utility for working with frontmatter in Markdown Abstract Syntax Trees (MDAST). It allows you to parse, transform, and stringify frontmatter in Markdown documents, making it easier to handle metadata in Markdown files.

What are mdast-util-frontmatter's main functionalities?

Parse Frontmatter

This feature allows you to parse frontmatter from a Markdown string into an MDAST. The example demonstrates how to parse YAML frontmatter from a Markdown document.

const { fromMarkdown } = require('mdast-util-from-markdown');
const { frontmatter } = require('mdast-util-frontmatter');
const { frontmatterFromMarkdown } = require('mdast-util-frontmatter/from-markdown');

const md = '---\ntitle: Hello World\n---\n\n# Heading\n';
const tree = fromMarkdown(md, {
  extensions: [frontmatter(['yaml'])],
  mdastExtensions: [frontmatterFromMarkdown(['yaml'])]
});

console.log(tree);

Stringify Frontmatter

This feature allows you to stringify an MDAST with frontmatter back into a Markdown string. The example demonstrates how to convert an MDAST with YAML frontmatter into a Markdown document.

const { toMarkdown } = require('mdast-util-to-markdown');
const { frontmatter } = require('mdast-util-frontmatter');
const { frontmatterToMarkdown } = require('mdast-util-frontmatter/to-markdown');

const tree = {
  type: 'root',
  children: [
    {
      type: 'yaml',
      value: 'title: Hello World'
    },
    {
      type: 'heading',
      depth: 1,
      children: [{ type: 'text', value: 'Heading' }]
    }
  ]
};

const md = toMarkdown(tree, {
  extensions: [frontmatterToMarkdown(['yaml'])]
});

console.log(md);

Transform Frontmatter

This feature allows you to transform frontmatter within an MDAST. The example demonstrates how to visit and modify YAML frontmatter in a Markdown document.

const { visit } = require('unist-util-visit');
const { fromMarkdown } = require('mdast-util-from-markdown');
const { frontmatter } = require('mdast-util-frontmatter');
const { frontmatterFromMarkdown } = require('mdast-util-frontmatter/from-markdown');

const md = '---\ntitle: Hello World\n---\n\n# Heading\n';
const tree = fromMarkdown(md, {
  extensions: [frontmatter(['yaml'])],
  mdastExtensions: [frontmatterFromMarkdown(['yaml'])]
});

visit(tree, 'yaml', node => {
  node.value = 'title: Updated Title';
});

console.log(tree);

Other packages similar to mdast-util-frontmatter

Keywords

FAQs

Package last updated on 14 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc