https://github.com/alexstevovich/matter-json-node
[Node.js] JSON front-matter parser and combiner.
https://github.com/alexstevovich/matter-json-node
front-matter json nodejs parser
Last synced: 2 months ago
JSON representation
[Node.js] JSON front-matter parser and combiner.
- Host: GitHub
- URL: https://github.com/alexstevovich/matter-json-node
- Owner: alexstevovich
- License: apache-2.0
- Created: 2025-03-15T03:22:41.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-03-15T03:24:21.000Z (10 months ago)
- Last Synced: 2025-06-25T11:55:46.569Z (7 months ago)
- Topics: front-matter, json, nodejs, parser
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```js
npm install matter-json
```
## Usage
```js
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
- [https://github.com/alexstevovich/matter-yaml-node](https://github.com/alexstevovich/matter-yaml-node)
- [https://github.com/alexstevovich/matter-toml-node](https://github.com/alexstevovich/matter-toml-node)
## License
Apache-2.0 License. See [LICENSE](LICENSE) for details.