360 Secure SkillHub
360 安全技能中心

Parse YAML/JSON/TOML front-matter from strings or files using the gray-matter library

核心功能涵盖解析提取YAML/JSON/TOML等格式的前置元数据、剥离元数据保留内容、校验文件是否含元数据、生成元数据。支持处理文件或标准输入,可通过命令行脚本完成常规操作,也可编程调用API实现批量等自定义逻辑,适合处理带元数据的markdown文件等场景。

openlark
openlark
热度 28.89
最近更新 2026-05-11 17:48:05
分类:效率依赖:代码执行 / 读写文件应用场景:数据处理 / 文件转换

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add gray-matter
Metadata
name
gray-matter
description
Parse YAML/JSON/TOML front-matter from strings or files using the gray-matter library.

Gray Matter

Parse YAML/JSON/TOML/CSON/CoffeeScript front-matter from strings or files. Use whenever working with markdown files that have front-matter (YAML/JSON/TOML headers), extracting metadata from documents with ---- delimited headers, stringifying data back to front-matter format, or any task involving front-matter parsing/stringification.

Triggers

"front matter", "frontmatter", "gray-matter", "YAML header","parse markdown metadata", "extract front matter", "add front matter", "stringify front matter".

Quick Start

Use scripts/parse.js for all front-matter operations:

# Parse front-matter from a file (outputs JSON)
node scripts/parse.js path/to/file.md

# Parse from piped stdin
type file.md | node scripts/parse.js --stdin

# Output only the front-matter data (YAML format)
node scripts/parse.js file.md --data-only

# Output only the content (no front-matter)
node scripts/parse.js file.md --content-only

# Include excerpt in output
node scripts/parse.js file.md --excerpt

# Force a specific language
node scripts/parse.js file.md --lang json

# Custom delimiters
node scripts/parse.js file.md --delims "~~~"

# Test if a file has front-matter
node scripts/parse.js file.md --test

# Stringify: write JSON data to a temp file, then:
node scripts/parse.js data.json --stringify
# Outputs front-matter delimiters wrapping YAML-serialized data

Common Operations

1. Extract metadata from a markdown file

node scripts/parse.js path/to/post.md --data-only

2. Strip front-matter, keep only content

node scripts/parse.js path/to/post.md --content-only

3. Check if a file has front-matter

node scripts/parse.js path/to/file.md --test
# prints: true or false

4. Add front-matter to content

Write JSON data to a temp file, then stringify:

node scripts/parse.js _data.json --stringify
# Prepends YAML front-matter (with --- delimiters) to empty content

5. Parse TOML front-matter

gray-matter does not include a TOML engine by default. First install the toml package:

cd <skill-dir>/scripts && npm install toml

Then use inline require('gray-matter') with a custom engine:

const toml = require('toml');
const matter = require('gray-matter');
const result = matter(str, {
  engines: { toml: toml.parse.bind(toml) },
  language: 'toml'
});

Alternatively, gray-matter auto-detects the language when specified in the delimiter line:

---toml
title = "My Post"
---

When to Use the Script vs Inline Code

  • Use the script (above) for most operations -- it handles edge cases reliably
  • Use inline require('gray-matter') only when doing programmatic transformations (iterating over many files, conditional logic, custom engine integration). In that case, see references/api.md for the full API.

Script Installation

The script auto-installs gray-matter via npm install gray-matter on first run. If auto-install fails, run manually:

cd <skill-dir>/scripts && npm install gray-matter

Reference

See references/api.md for the complete gray-matter API documentation (main function, static methods, all options, engines, deprecated options).