matter-json
v1.0.0
Published
JSON front-matter parser and combiner. Minimal and perfect
Downloads
28
Maintainers
Readme
matter-json
Archetype: Node.js package
matter-json is a JSON front-matter parser and combiner. Minimal and perfect.
Details
- Extract front matter data and returns the meta and content separated.
- Validate front matter structure before parsing.
- Serialize front matter back into a structured document.
Install
npm install matter-json
Usage
import { parse, serialize, validate } from 'matter-json';
const text = `---
{
"title": "Hello World",
"tags": ["json", "front-matter"],
"published": true
}
---
This is the document content.`;
const result = parse(text);
console.log(result);
console.log(validate(text)); // true
const output = serialize(
{ title: 'New Title', draft: false },
'Updated content.',
);
console.log(output);
API Reference
parse(text: string): { data: object, content: string }
Parses a document with front matter.
Parameters:
| Parameter | Type | Description |
| --------- | ------ | ---------------------------------------------- |
| text
| string | The document containing front matter and text. |
Returns:
An object containing:
data
→ Parsed front matter.content
→ The remaining document content.
serialize(data: object, content: string): string
Converts an object into front matter format.
Parameters:
| Parameter | Type | Description |
| --------- | ------ | --------------------------------- |
| data
| object | The structured front matter data. |
| content
| string | The document content. |
Returns:
A string formatted as front matter with the given content.
validate(text: string): boolean
Checks if front matter is correctly formatted and parsable.
Parameters:
| Parameter | Type | Description |
| --------- | ------ | ------------------------- |
| text
| string | The document to validate. |
Returns:
true
if valid.false
if invalid.
Related Links
I provide matter parsers for YAML
and TOML
also
License
Apache-2.0 License. See LICENSE for details.