frontmatter()
Tạo plugin frontmatter cho phép parse các khối metadata YAML, TOML hoặc JSON ở đầu tài liệu Markdown/MDX.
import { frontmatter } from "@unifast/node";Chữ ký
function frontmatter(options?: FrontmatterPluginOptions): UnifastPluginTham số
options?
Cấu hình cho các định dạng frontmatter
| Thuộc tính | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
yaml | boolean | true | Bật frontmatter YAML (dấu phân cách —) |
toml | boolean | false | Bật frontmatter TOML (dấu phân cách +++) |
json | boolean | false | Bật frontmatter JSON (dấu phân cách ;;;) |
Cách dùng
import { compile, frontmatter } from "@unifast/node";
const result = compile(md, {
plugins: [
frontmatter({
yaml: true,
toml: true,
json: true,
}),
],
});
console.log(result.frontmatter);
// { title: "My Page", date: "2024-01-01", tags: ["markdown", "docs"] }Ví dụ
Frontmatter YAML
import { compile, frontmatter } from "@unifast/node";
const md = `---
title: My Page
date: 2024-01-01
tags:
- markdown
- docs
---
# Hello`;
const result = compile(md, { plugins: [frontmatter()] });
console.log(result.frontmatter);
// { title: "My Page", date: "2024-01-01", tags: ["markdown", "docs"] }Frontmatter TOML
import { compile, frontmatter } from "@unifast/node";
const md = `+++
title = "My Page"
date = 2024-01-01
+++
# Hello`;
const result = compile(md, { plugins: [frontmatter({ toml: true })] });
console.log(result.frontmatter);
// { title: "My Page", date: "2024-01-01" }Frontmatter JSON
import { compile, frontmatter } from "@unifast/node";
const md = `;;;
{"title": "My Page", "draft": false}
;;;
# Hello`;
const result = compile(md, { plugins: [frontmatter({ json: true })] });
console.log(result.frontmatter);
// { title: "My Page", draft: false }