Skip to content

Commit

Permalink
Add support for passing options to yaml parser
Browse files Browse the repository at this point in the history
Closes GH-2.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
stefanprobst authored Apr 12, 2021
1 parent 4c97d06 commit 107d6d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ var yamlParse = require('js-yaml').load
module.exports = matter

function matter(file, options) {
var strip = (options || {}).strip
var settings = options || {}
var strip = settings.strip
var yamlOptions = settings.yaml || {}
var doc = String(file)
var match = /^---(?:\r?\n|\r)(?:([\s\S]*)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(
doc
)

if (match) {
file.data.matter = yamlParse(match[1], {filename: file.path})
file.data.matter = yamlParse(
match[1],
Object.assign({}, yamlOptions, {filename: file.path})
)

if (strip) {
doc = doc.slice(match[0].length)
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"js-yaml": "^4.0.0"
},
"devDependencies": {
"@types/js-yaml": "^4.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
Expand Down Expand Up @@ -72,7 +73,10 @@
"rules": {
"guard-for-in": "off",
"unicorn/prefer-includes": "off"
}
},
"ignores": [
"types/"
]
},
"remarkConfig": {
"plugins": [
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var test = require('tape')
var buffer = require('is-buffer')
var vfile = require('to-vfile')
var matter = require('.')
var CORE_SCHEMA = require('js-yaml').CORE_SCHEMA

var yaml = '---\nkey: value\nlist:\n - 1\n - 2\n---'
var doc = 'Here is a document\nMore of the document\nOther lines\n'
Expand Down Expand Up @@ -38,5 +39,14 @@ test('vfile-matter', function (t) {
file = matter(vfile(), {strip: true})
t.ok(file.contents === undefined, 'should supporting empties')

file = matter(vfile({contents: '---\ndate: 2021-01-01\n---\n'}), {
yaml: {schema: CORE_SCHEMA}
})
t.deepEqual(
file.data,
{matter: {date: '2021-01-01'}},
'should pass yaml options'
)

t.end()
})
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TypeScript Version: 3.7

import {VFile, VFileCompatible} from 'vfile'
import {LoadOptions} from 'js-yaml'

export = matter

Expand All @@ -23,5 +24,9 @@ declare namespace matter {
* Remove the YAML front matter from the file
*/
strip?: boolean
/**
* Options for the YAML parser.
*/
yaml?: Omit<LoadOptions, 'filename'>
}
}
3 changes: 3 additions & 0 deletions types/vfile-matter-tests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import vfile = require('vfile')
import matter = require('vfile-matter')
import {CORE_SCHEMA} from 'js-yaml'

const file = vfile()

matter(file) // $ExpectType VFile
matter(file, {strip: true}) // $ExpectType VFile
matter(file, {yaml: {schema: CORE_SCHEMA}}) // $ExpectType VFile
matter(file, {}) // $ExpectType VFile
matter(1) // $ExpectError
matter(file, {strip: 'string'}) // $ExpectError
matter(file, {yaml: 'string'}) // $ExpectError

0 comments on commit 107d6d8

Please # to comment.