An open API service indexing awesome lists of open source software.

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.

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.