simplematter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +18 -0
- package/README.md +124 -0
- package/lib/simplematter.js +89 -0
- package/package.json +46 -0
- package/types/simplematter.d.ts +26 -0
- package/types/simplematter.d.ts.map +1 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 Remco Haszing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
6
|
+
associated documentation files (the “Software”), to deal in the Software without restriction,
|
|
7
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
8
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
12
|
+
portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
15
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
16
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
17
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# simplematter
|
|
2
|
+
|
|
3
|
+
[](https://github.com/remcohaszing/simplematter/actions/workflows/ci.yaml)
|
|
4
|
+
[](https://codecov.io/gh/remcohaszing/simplematter)
|
|
5
|
+
[](https://www.npmjs.com/package/simplematter)
|
|
6
|
+
[](https://www.npmjs.com/package/simplematter)
|
|
7
|
+
|
|
8
|
+
A simple frontmatter parser.
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [API](#api)
|
|
15
|
+
- [`simplematter(content[, parsers])`](#simplemattercontent-parsers)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
- [Modify YAML frontmatter](#modify-yaml-frontmatter)
|
|
18
|
+
- [Get raw frontmatter](#get-raw-frontmatter)
|
|
19
|
+
- [Compatibility](#compatibility)
|
|
20
|
+
- [License](#license)
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install simplematter
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
The `simplematter` function parses frontmatter data from a string. It supports both the `---` and
|
|
31
|
+
the `+++` fences. The `---` fence is typically used for YAML data, the `+++` fence for TOML.
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { simplematter } from 'simplematter'
|
|
35
|
+
|
|
36
|
+
const [frontmatter, document] = simplematter(
|
|
37
|
+
`---
|
|
38
|
+
title: This could be the document title.
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
Rest of document
|
|
42
|
+
`
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
console.log(frontmatter)
|
|
46
|
+
console.log(document)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `simplematter(content[, parsers])`
|
|
52
|
+
|
|
53
|
+
Parse frontmatter data.
|
|
54
|
+
|
|
55
|
+
#### Arguments
|
|
56
|
+
|
|
57
|
+
- `content` (`string`) — The string from which to parse frontmatter data.
|
|
58
|
+
- `parsers` (`object`, optional) — The parsers to use. It has the following keys:
|
|
59
|
+
- `yaml` — A parser for YAML content. This is used to parse content from the `---` fence. By
|
|
60
|
+
default the [`yaml`](https://eemeli.org/yaml/) package is used.
|
|
61
|
+
- `toml` — A parser for TOML content. This is used to parse content from the `+++` fence. By
|
|
62
|
+
default the [`smol-toml`](https://github.com/squirrelchat/smol-toml) package is used.
|
|
63
|
+
|
|
64
|
+
## Examples
|
|
65
|
+
|
|
66
|
+
### Modify YAML frontmatter
|
|
67
|
+
|
|
68
|
+
You can modify YAML frontmatter data while preserving most of the original YAML using the
|
|
69
|
+
[`parseDocument`](https://eemeli.org/yaml/#parsing-documents) function exposed by `yaml`.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { simplematter } from 'simplematter'
|
|
73
|
+
import { Document, isDocument, parseDocument } from 'yaml'
|
|
74
|
+
|
|
75
|
+
const [frontmatter, content] = simplematter(
|
|
76
|
+
`---
|
|
77
|
+
# This comment is preserved.
|
|
78
|
+
title: This could be the document title.
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
Rest of the document.
|
|
82
|
+
`,
|
|
83
|
+
{ yaml: parseDocument }
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
const yamlDocument = isDocument(frontmatter) ? frontmatter : new Document()
|
|
87
|
+
yamlDocument.set('modified', new Date().toISOString().slice(0, 10))
|
|
88
|
+
|
|
89
|
+
const result = `---\n${frontmatter}---\n\n${content}`
|
|
90
|
+
|
|
91
|
+
console.log(result)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Get raw frontmatter
|
|
95
|
+
|
|
96
|
+
You can get the raw frontmatter string by returning the input from your parser function.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { simplematter } from 'simplematter'
|
|
100
|
+
|
|
101
|
+
function identity<Input>(input: Input): Input {
|
|
102
|
+
return input
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const [frontmatter] = simplematter(
|
|
106
|
+
`---
|
|
107
|
+
title: This could be the document title.
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
Rest of the document.
|
|
111
|
+
`,
|
|
112
|
+
{ toml: identity, yaml: identity }
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
console.log(frontmatter)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Compatibility
|
|
119
|
+
|
|
120
|
+
This project is compatible with Node.js 22 or greater.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { parse as toml } from 'smol-toml'
|
|
2
|
+
import { parse as yaml } from 'yaml'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef simplematter.Parsers
|
|
6
|
+
* @property {((frontmatter: string) => unknown) | undefined} [toml]
|
|
7
|
+
* A parser for TOML content. This is used to parse content from the `+++` fence. By default the
|
|
8
|
+
* [`yaml`](https://eemeli.org/yaml/) package is used.
|
|
9
|
+
* @property {((frontmatter: string) => unknown) | undefined} [yaml]
|
|
10
|
+
* A parser for YAML content. This is used to parse content from the `---` fence. By default the
|
|
11
|
+
* [`smol-toml`](https://github.com/squirrelchat/smol-toml) package is used.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const nonWhitespaceRegex = /\S/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Check whether the content from a line starting at an index is trailing space.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} content
|
|
20
|
+
* The content whose characters to check.
|
|
21
|
+
* @param {number} start
|
|
22
|
+
* The start index of the characters to check.
|
|
23
|
+
* @returns {number}
|
|
24
|
+
* The index of the newline character that ends the line whitespace, -1 otherwise.
|
|
25
|
+
*/
|
|
26
|
+
function findTrailingSpaceEnd(content, start) {
|
|
27
|
+
for (let index = start; index < content.length; index += 1) {
|
|
28
|
+
const char = content[index]
|
|
29
|
+
|
|
30
|
+
if (char === '\n') {
|
|
31
|
+
return index
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (nonWhitespaceRegex.test(char)) {
|
|
35
|
+
return -1
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The fence is the end of the document. There’s no trailing newline.
|
|
40
|
+
return content.length
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @type {Required<simplematter.Parsers>} */
|
|
44
|
+
const defaultParsers = {
|
|
45
|
+
toml,
|
|
46
|
+
yaml
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Parse frontmatter data.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} content
|
|
53
|
+
* The string from which to parse frontmatter data.
|
|
54
|
+
* @param {simplematter.Parsers | undefined} [parsers]
|
|
55
|
+
* The parsers to use.
|
|
56
|
+
* @returns {[data: unknown, document: string]}
|
|
57
|
+
* A tuple which consists of the parsed frontmatter data and the rest of the document.
|
|
58
|
+
*/
|
|
59
|
+
export function simplematter(content, parsers) {
|
|
60
|
+
const openFence = content.slice(0, 3)
|
|
61
|
+
const openSpaceEnd = findTrailingSpaceEnd(content, 3)
|
|
62
|
+
let closeFenceStart = openSpaceEnd + 1
|
|
63
|
+
|
|
64
|
+
/** @type {number} */
|
|
65
|
+
let closeSpaceEnd
|
|
66
|
+
|
|
67
|
+
if (openSpaceEnd === -1 || (openFence !== '---' && openFence !== '+++')) {
|
|
68
|
+
return [undefined, content]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
do {
|
|
72
|
+
closeFenceStart = content.indexOf(openFence, closeFenceStart + 3)
|
|
73
|
+
|
|
74
|
+
// We’ve reached the end of the content, but we didn’t find a closing fence.
|
|
75
|
+
if (closeFenceStart === -1) {
|
|
76
|
+
return [undefined, content]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
closeSpaceEnd = findTrailingSpaceEnd(content, closeFenceStart + 3)
|
|
80
|
+
} while (closeSpaceEnd === -1 || content[closeFenceStart - 1] !== '\n')
|
|
81
|
+
|
|
82
|
+
const key = openFence === '+++' ? 'toml' : 'yaml'
|
|
83
|
+
const parser = parsers?.[key] || defaultParsers[key]
|
|
84
|
+
|
|
85
|
+
return [
|
|
86
|
+
parser(content.slice(openSpaceEnd + 1, closeFenceStart)),
|
|
87
|
+
content.slice(closeSpaceEnd).trimStart()
|
|
88
|
+
]
|
|
89
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simplematter",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple frontmatter parser",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"frontmatter",
|
|
7
|
+
"front-matter",
|
|
8
|
+
"graymatter",
|
|
9
|
+
"gray-matter",
|
|
10
|
+
"greymatter",
|
|
11
|
+
"grey-matter",
|
|
12
|
+
"matter"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/remcohaszing/simplematter#readme",
|
|
15
|
+
"bugs": "https://github.com/remcohaszing/simplematter/issues",
|
|
16
|
+
"repository": "remcohaszing/simplematter",
|
|
17
|
+
"funding": "https://github.com/sponsors/remcohaszing",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Remco Haszing <remcohaszing@gmail.com>",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
"types": "./types/simplematter.d.ts",
|
|
23
|
+
"default": "./lib/simplematter.js"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"files": [
|
|
27
|
+
"lib",
|
|
28
|
+
"types"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"prepack": "tsc --build",
|
|
32
|
+
"test": "c8 node --test"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"smol-toml": "^1.0.0",
|
|
36
|
+
"yaml": "^2.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@remcohaszing/eslint": "^13.0.0",
|
|
40
|
+
"c8": "^10.0.0",
|
|
41
|
+
"prettier": "^3.0.0",
|
|
42
|
+
"remark-cli": "^12.0.0",
|
|
43
|
+
"remark-preset-remcohaszing": "^3.0.0",
|
|
44
|
+
"typescript": "^5.0.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse frontmatter data.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} content
|
|
5
|
+
* The string from which to parse frontmatter data.
|
|
6
|
+
* @param {simplematter.Parsers | undefined} [parsers]
|
|
7
|
+
* The parsers to use.
|
|
8
|
+
* @returns {[data: unknown, document: string]}
|
|
9
|
+
* A tuple which consists of the parsed frontmatter data and the rest of the document.
|
|
10
|
+
*/
|
|
11
|
+
export function simplematter(content: string, parsers?: simplematter.Parsers | undefined): [data: unknown, document: string];
|
|
12
|
+
export namespace simplematter {
|
|
13
|
+
type Parsers = {
|
|
14
|
+
/**
|
|
15
|
+
* A parser for TOML content. This is used to parse content from the `+++` fence. By default the
|
|
16
|
+
* [`yaml`](https://eemeli.org/yaml/) package is used.
|
|
17
|
+
*/
|
|
18
|
+
toml?: ((frontmatter: string) => unknown) | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* A parser for YAML content. This is used to parse content from the `---` fence. By default the
|
|
21
|
+
* [`smol-toml`](https://github.com/squirrelchat/smol-toml) package is used.
|
|
22
|
+
*/
|
|
23
|
+
yaml?: ((frontmatter: string) => unknown) | undefined;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=simplematter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simplematter.d.ts","sourceRoot":"","sources":["../lib/simplematter.js"],"names":[],"mappings":"AAgDA;;;;;;;;;GASG;AACH,sCAPW,MAAM,YAEN,YAAY,CAAC,OAAO,GAAG,SAAS,GAE9B,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAiC7C;;;;;;;eAnFa,CAAC,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,SAAS;;;;;eAG9C,CAAC,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,SAAS"}
|