-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
189 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
remark-frontmatter.js | ||
remark-frontmatter.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,63 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var xtend = require('xtend'); | ||
var matters = require('./lib/matters'); | ||
var parse = require('./lib/parse'); | ||
var compile = require('./lib/compile'); | ||
var xtend = require('xtend') | ||
var matters = require('./lib/matters') | ||
var parse = require('./lib/parse') | ||
var compile = require('./lib/compile') | ||
|
||
module.exports = frontmatter; | ||
module.exports = frontmatter | ||
|
||
function frontmatter(options) { | ||
var parser = this.Parser; | ||
var compiler = this.Compiler; | ||
var config = matters(options || ['yaml']); | ||
var parser = this.Parser | ||
var compiler = this.Compiler | ||
var config = matters(options || ['yaml']) | ||
|
||
if (isRemarkParser(parser)) { | ||
attachParser(parser, config); | ||
attachParser(parser, config) | ||
} | ||
|
||
if (isRemarkCompiler(compiler)) { | ||
attachCompiler(compiler, config); | ||
attachCompiler(compiler, config) | ||
} | ||
} | ||
|
||
function attachParser(parser, matters) { | ||
var proto = parser.prototype; | ||
var tokenizers = wrap(parse, matters); | ||
var names = []; | ||
var key; | ||
var proto = parser.prototype | ||
var tokenizers = wrap(parse, matters) | ||
var names = [] | ||
var key | ||
|
||
for (key in tokenizers) { | ||
names.push(key); | ||
names.push(key) | ||
} | ||
|
||
proto.blockMethods = names.concat(proto.blockMethods); | ||
proto.blockTokenizers = xtend(tokenizers, proto.blockTokenizers); | ||
proto.blockMethods = names.concat(proto.blockMethods) | ||
proto.blockTokenizers = xtend(tokenizers, proto.blockTokenizers) | ||
} | ||
|
||
function attachCompiler(compiler, matters) { | ||
var proto = compiler.prototype; | ||
proto.visitors = xtend(wrap(compile, matters), proto.visitors); | ||
var proto = compiler.prototype | ||
proto.visitors = xtend(wrap(compile, matters), proto.visitors) | ||
} | ||
|
||
function wrap(func, matters) { | ||
var result = {}; | ||
var length = matters.length; | ||
var index = -1; | ||
var tuple; | ||
var result = {} | ||
var length = matters.length | ||
var index = -1 | ||
var tuple | ||
|
||
while (++index < length) { | ||
tuple = func(matters[index]); | ||
result[tuple[0]] = tuple[1]; | ||
tuple = func(matters[index]) | ||
result[tuple[0]] = tuple[1] | ||
} | ||
|
||
return result; | ||
return result | ||
} | ||
|
||
function isRemarkParser(parser) { | ||
return Boolean( | ||
parser && | ||
parser.prototype && | ||
parser.prototype.blockTokenizers | ||
); | ||
return Boolean(parser && parser.prototype && parser.prototype.blockTokenizers) | ||
} | ||
|
||
function isRemarkCompiler(compiler) { | ||
return Boolean( | ||
compiler && | ||
compiler.prototype && | ||
compiler.prototype.visitors | ||
); | ||
return Boolean(compiler && compiler.prototype && compiler.prototype.visitors) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var fence = require('./fence'); | ||
var fence = require('./fence') | ||
|
||
module.exports = create; | ||
module.exports = create | ||
|
||
function create(matter) { | ||
var type = matter.type; | ||
var open = fence(matter, 'open'); | ||
var close = fence(matter, 'close'); | ||
var type = matter.type | ||
var open = fence(matter, 'open') | ||
var close = fence(matter, 'close') | ||
|
||
frontmatter.displayName = type + 'FrontMatter'; | ||
frontmatter.displayName = type + 'FrontMatter' | ||
|
||
return [type, frontmatter]; | ||
return [type, frontmatter] | ||
|
||
function frontmatter(node) { | ||
return open + (node.value ? '\n' + node.value : '') + '\n' + close; | ||
return open + (node.value ? '\n' + node.value : '') + '\n' + close | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = fence; | ||
module.exports = fence | ||
|
||
function fence(matter, prop) { | ||
var marker; | ||
var marker | ||
|
||
if (matter.marker) { | ||
marker = pick(matter.marker, prop); | ||
return marker + marker + marker; | ||
marker = pick(matter.marker, prop) | ||
return marker + marker + marker | ||
} | ||
|
||
return pick(matter.fence, prop); | ||
return pick(matter.fence, prop) | ||
} | ||
|
||
function pick(schema, prop) { | ||
return typeof schema === 'string' ? schema : schema[prop]; | ||
return typeof schema === 'string' ? schema : schema[prop] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var fault = require('fault'); | ||
var fault = require('fault') | ||
|
||
module.exports = matters; | ||
module.exports = matters | ||
|
||
var own = {}.hasOwnProperty; | ||
var own = {}.hasOwnProperty | ||
|
||
var markers = { | ||
yaml: '-', | ||
toml: '+' | ||
}; | ||
} | ||
|
||
function matters(options) { | ||
var results = []; | ||
var index = -1; | ||
var length; | ||
var results = [] | ||
var index = -1 | ||
var length | ||
|
||
/* One preset or matter. */ | ||
if (typeof options === 'string' || !('length' in options)) { | ||
options = [options]; | ||
options = [options] | ||
} | ||
|
||
length = options.length; | ||
length = options.length | ||
|
||
while (++index < length) { | ||
results[index] = matter(options[index]); | ||
results[index] = matter(options[index]) | ||
} | ||
|
||
return results; | ||
return results | ||
} | ||
|
||
function matter(option) { | ||
var result = option; | ||
var result = option | ||
|
||
if (typeof result === 'string') { | ||
if (!own.call(markers, result)) { | ||
throw fault('Missing matter definition for `%s`', result); | ||
throw fault('Missing matter definition for `%s`', result) | ||
} | ||
|
||
result = {type: result, marker: markers[result]}; | ||
result = {type: result, marker: markers[result]} | ||
} else if (typeof result !== 'object') { | ||
throw fault('Expected matter to be an object, not `%j`', result); | ||
throw fault('Expected matter to be an object, not `%j`', result) | ||
} | ||
|
||
if (!own.call(result, 'type')) { | ||
throw fault('Missing `type` in matter `%j`', result); | ||
throw fault('Missing `type` in matter `%j`', result) | ||
} | ||
|
||
if (!own.call(result, 'fence') && !own.call(result, 'marker')) { | ||
throw fault('Missing `marker` or `fence` in matter `%j`', result); | ||
throw fault('Missing `marker` or `fence` in matter `%j`', result) | ||
} | ||
|
||
return result; | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var fence = require('./fence'); | ||
var fence = require('./fence') | ||
|
||
module.exports = create; | ||
module.exports = create | ||
|
||
function create(matter) { | ||
var name = matter.type + 'FrontMatter'; | ||
var open = fence(matter, 'open'); | ||
var close = fence(matter, 'close'); | ||
var newline = '\n'; | ||
var name = matter.type + 'FrontMatter' | ||
var open = fence(matter, 'open') | ||
var close = fence(matter, 'close') | ||
var newline = '\n' | ||
|
||
frontmatter.displayName = name; | ||
frontmatter.onlyAtStart = true; | ||
frontmatter.displayName = name | ||
frontmatter.onlyAtStart = true | ||
|
||
return [name, frontmatter]; | ||
return [name, frontmatter] | ||
|
||
function frontmatter(eat, value, silent) { | ||
var index = open.length; | ||
var offset; | ||
var index = open.length | ||
var offset | ||
|
||
if (value.slice(0, index) !== open || value.charAt(index) !== newline) { | ||
return; | ||
return | ||
} | ||
|
||
offset = value.indexOf(close, index); | ||
offset = value.indexOf(close, index) | ||
|
||
while (offset !== -1 && value.charAt(offset - 1) !== newline) { | ||
index = offset + close.length; | ||
offset = value.indexOf(close, index); | ||
index = offset + close.length | ||
offset = value.indexOf(close, index) | ||
} | ||
|
||
if (offset !== -1) { | ||
/* istanbul ignore if - never used (yet) */ | ||
if (silent) { | ||
return true; | ||
return true | ||
} | ||
|
||
return eat(value.slice(0, offset + close.length))({ | ||
type: matter.type, | ||
value: value.slice(open.length + 1, offset - 1) | ||
}); | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.