Skip to content

Commit

Permalink
Update for remark 13
Browse files Browse the repository at this point in the history
* Update to a micromark parser, defer work to
  <https://github.com/micromark/micromark-extension-frontmatter>,
  <https://github.com/syntax-tree/mdast-util-frontmatter>
* 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 Oct 4, 2020
1 parent 9df76a0 commit fb1357b
Show file tree
Hide file tree
Showing 39 changed files with 181 additions and 587 deletions.
65 changes: 11 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,19 @@
'use strict'

var matters = require('./lib/matters')
var parse = require('./lib/parse')
var compile = require('./lib/compile')
var syntax = require('micromark-extension-frontmatter')
var fromMarkdown = require('mdast-util-frontmatter/from-markdown')
var toMarkdown = require('mdast-util-frontmatter/to-markdown')

module.exports = frontmatter

function frontmatter(options) {
var parser = this.Parser
var compiler = this.Compiler
var config = matters(options || ['yaml'])

if (isRemarkParser(parser)) {
attachParser(parser, config)
}

if (isRemarkCompiler(compiler)) {
attachCompiler(compiler, config)
}
}

function attachParser(parser, matters) {
var proto = parser.prototype
var tokenizers = wrap(parse, matters)
var names = []
var key

for (key in tokenizers) {
names.push(key)
var data = this.data()
add('micromarkExtensions', syntax(options))
add('fromMarkdownExtensions', fromMarkdown(options))
add('toMarkdownExtensions', toMarkdown(options))
function add(field, value) {
/* istanbul ignore if - other extensions. */
if (data[field]) data[field].push(value)
else data[field] = [value]
}

proto.blockMethods = names.concat(proto.blockMethods)
proto.blockTokenizers = Object.assign({}, tokenizers, proto.blockTokenizers)
}

function attachCompiler(compiler, matters) {
var proto = compiler.prototype
proto.visitors = Object.assign({}, wrap(compile, matters), proto.visitors)
}

function wrap(func, matters) {
var result = {}
var length = matters.length
var index = -1
var tuple

while (++index < length) {
tuple = func(matters[index])
result[tuple[0]] = tuple[1]
}

return result
}

function isRemarkParser(parser) {
return Boolean(parser && parser.prototype && parser.prototype.blockTokenizers)
}

function isRemarkCompiler(compiler) {
return Boolean(compiler && compiler.prototype && compiler.prototype.visitors)
}
19 changes: 0 additions & 19 deletions lib/compile.js

This file was deleted.

18 changes: 0 additions & 18 deletions lib/fence.js

This file was deleted.

55 changes: 0 additions & 55 deletions lib/matters.js

This file was deleted.

46 changes: 0 additions & 46 deletions lib/parse.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
],
"files": [
"index.js",
"lib",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"fault": "^1.0.1"
"mdast-util-frontmatter": "^0.2.0",
"micromark-extension-frontmatter": "^0.2.0"
},
"devDependencies": {
"browserify": "^16.0.0",
Expand All @@ -42,7 +42,7 @@
"not": "^0.1.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark": "^12.0.0",
"remark": "^13.0.0-alpha.1",
"remark-cli": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"tape": "^5.0.0",
Expand Down
Loading

0 comments on commit fb1357b

Please sign in to comment.