Skip to content

Commit

Permalink
Fix to match GitHub
Browse files Browse the repository at this point in the history
* Frontmatter must now start on the first line.
  Previously, delaying with blank lines first worked.
  That behavior does not match how GitHub parses YAML, and is thus
  removed.
  • Loading branch information
wooorm committed Dec 7, 2020
1 parent 7de4a64 commit 2904467
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
34 changes: 8 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,26 @@

var buffer = require('is-buffer')
var yamlParse = require('js-yaml').safeLoad
var parse = require('remark-frontmatter/lib/parse')
var matters = require('remark-frontmatter/lib/matters')

module.exports = matter

var matterParse = parse(matters('yaml')[0])[1]

function matter(file, options) {
var strip = (options || {}).strip
var data = file.data
var doc = String(file)
var result = matterParse(mockEat, doc)
var offset

data.matter = {}
var match = /^---(?:\r?\n|\r)(?:([\s\S]*)(?:\r?\n|\r))?---(?:\r?\n|\r|$)/.exec(
doc
)

if (result) {
data.matter = yamlParse(result.value, {filename: file.path})
if (match) {
file.data.matter = yamlParse(match[1], {filename: file.path})

if (strip) {
offset = result.length

// \n
if (doc.charCodeAt(offset) === 10) {
offset++
}

doc = doc.slice(offset)
doc = doc.slice(match[0].length)
file.contents = buffer(file.contents) ? Buffer.from(doc) : doc
}
} else {
file.data.matter = {}
}

return file
}

function mockEat(doc) {
return add
function add(node) {
return {length: doc.length, value: node.value}
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
],
"dependencies": {
"is-buffer": "^2.0.0",
"js-yaml": "^3.0.0",
"remark-frontmatter": "^1.3.0"
"js-yaml": "^3.0.0"
},
"devDependencies": {
"nyc": "^15.0.0",
Expand Down

0 comments on commit 2904467

Please # to comment.