vfile-matter

5.0.1 · active · verified Sun Apr 19

vfile-matter is a utility package for the vfile ecosystem, designed to parse YAML front matter found at the beginning of a virtual file. It's currently stable at version 5.0.1, with minor updates and patch releases occurring as needed, and major versions released periodically to align with ecosystem updates and Node.js LTS cycles. This package populates the parsed data into `file.data.matter` and can optionally strip the front matter content from the file's `value`. A key differentiator is its tight integration with `vfile`, making it suitable for direct file manipulation when not using a higher-level parsing system like remark. Since version 4.0.0, it uses the more modern `yaml` package for parsing, replacing `js-yaml`. It's important to note that this package is distinct from `remark-frontmatter`, which should be used when processing Markdown with `remark` rather than directly manipulating `vfile` objects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing YAML front matter from a virtual file and stripping it from the content using `vfile-matter`.

import { read } from 'to-vfile';
import { matter } from 'vfile-matter';

const exampleFileContent = `---\nlayout: solar-system\ntitle: Jupiter\n---\n<h1>Jupiter</h1>\n`;

async function processFile() {
  // In a real scenario, you'd read from a file path
  // For this example, we create a VFile directly
  const file = await read({ path: 'example.html', value: exampleFileContent });

  // Parse the YAML front matter and optionally strip it
  matter(file, { strip: true });

  console.log('Parsed Matter:', file.data.matter);
  console.log('File Content (after stripping):', String(file));
}

processFile().catch(console.error);

/*
To run this example, ensure you have 'vfile-matter' and 'to-vfile' installed:
npm install vfile-matter to-vfile
*/

view raw JSON →