Skip to content

Commit 1bd1da2

Browse files
committed
Replace js-yaml with yaml
1 parent 437df2f commit 1bd1da2

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

index.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
/**
22
* @typedef {import('vfile').VFile} VFile
3-
* @typedef {import('js-yaml').LoadOptions} LoadOptions
3+
* @typedef {import('yaml').ParseOptions} ParseOptions
4+
* @typedef {import('yaml').DocumentOptions} DocumentOptions
5+
* @typedef {import('yaml').SchemaOptions} SchemaOptions
6+
* @typedef {import('yaml').ToJSOptions} ToJsOptions
7+
* @typedef {ParseOptions & DocumentOptions & SchemaOptions & ToJsOptions} YamlOptions
48
*
5-
* @typedef Options VFile matter options
6-
* @property {boolean} [strip=false] Remove the YAML front matter from the file
7-
* @property {Omit<LoadOptions, 'filename'>} [yaml] Options for the YAML parser
9+
* @typedef Options
10+
* Configuration (optional).
11+
* @property {boolean} [strip=false]
12+
* Remove the YAML front matter from the file.
13+
* @property {YamlOptions} [yaml]
14+
* Options for the YAML parser.
15+
* These are passed as `x` in `yaml.parse('', x)`, which is equivalent to
16+
* `ParseOptions & DocumentOptions & SchemaOptions & ToJsOptions`.
817
*/
918

1019
import buffer from 'is-buffer'
11-
import {load} from 'js-yaml'
20+
import yaml from 'yaml'
1221

1322
/**
14-
* Parse the YAML front matter in a [`vfile`](https://github.com/vfile/vfile), and add it as `file.data.matter`.
23+
* Parse the YAML front matter in a `vfile`, and add it as `file.data.matter`.
1524
*
16-
* If no matter is found in the file, nothing happens, except that `file.data.matter` is set to an empty object (`{}`).
25+
* If no matter is found in the file, nothing happens, except that
26+
* `file.data.matter` is set to an empty object (`{}`).
1727
*
1828
* @template {VFile} File
1929
* @param {File} file
@@ -31,10 +41,7 @@ export function matter(file, options = {}) {
3141
/^---(?:\r?\n|\r)(?:([\s\S]*?)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(doc)
3242

3343
if (match) {
34-
file.data.matter = load(
35-
match[1],
36-
Object.assign({}, yamlOptions, {filename: file.path})
37-
)
44+
file.data.matter = yaml.parse(match[1], yamlOptions)
3845

3946
if (strip) {
4047
doc = doc.slice(match[0].length)

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
"index.js"
3434
],
3535
"dependencies": {
36-
"@types/js-yaml": "^4.0.0",
3736
"is-buffer": "^2.0.0",
38-
"js-yaml": "^4.0.0"
37+
"yaml": "^2.0.0"
3938
},
4039
"devDependencies": {
4140
"@types/tape": "^4.0.0",

test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Buffer} from 'node:buffer'
22
import test from 'tape'
33
import buffer from 'is-buffer'
4-
import {CORE_SCHEMA} from 'js-yaml'
54
import {toVFile as vfile} from 'to-vfile'
65
import {matter} from './index.js'
76

@@ -42,13 +41,17 @@ test('vfile-matter', function (t) {
4241
file = matter(vfile(), {strip: true})
4342
t.ok(file.value === undefined, 'should supporting empties')
4443

45-
file = matter(vfile({value: '---\ndate: 2021-01-01\n---\n'}), {
46-
yaml: {schema: CORE_SCHEMA}
44+
file = matter(vfile({value: '---\nyes: no\n---\n'}), {
45+
yaml: {version: '1.2'}
46+
})
47+
t.deepEqual(file.data, {matter: {yes: 'no'}}, 'should pass yaml options (1)')
48+
file = matter(vfile({value: '---\nyes: no\n---\n'}), {
49+
yaml: {version: '1.1'}
4750
})
4851
t.deepEqual(
4952
file.data,
50-
{matter: {date: '2021-01-01'}},
51-
'should pass yaml options'
53+
{matter: {true: false}},
54+
'should pass yaml options (2)'
5255
)
5356

5457
t.end()

0 commit comments

Comments
 (0)