diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..4b9e4ee --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +coverage/ +remark-frontmatter.js +remark-frontmatter.min.js diff --git a/index.js b/index.js index c8b9d18..3fa9ecb 100644 --- a/index.js +++ b/index.js @@ -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) } diff --git a/lib/compile.js b/lib/compile.js index 76bc1bf..109b6ea 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -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 } } diff --git a/lib/fence.js b/lib/fence.js index e9f367b..9e80c48 100644 --- a/lib/fence.js +++ b/lib/fence.js @@ -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] } diff --git a/lib/matters.js b/lib/matters.js index e2da00e..df4a059 100644 --- a/lib/matters.js +++ b/lib/matters.js @@ -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 } diff --git a/lib/parse.js b/lib/parse.js index d431727..a78b36f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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) - }); + }) } } } diff --git a/package.json b/package.json index 85d7c56..85ade56 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "is-hidden": "^1.1.0", "not": "^0.1.0", "nyc": "^12.0.0", + "prettier": "^1.14.2", "remark": "^9.0.0", "remark-cli": "^5.0.0", "remark-preset-wooorm": "^4.0.0", @@ -40,14 +41,13 @@ "xo": "^0.22.0" }, "scripts": { - "build-md": "remark *.md -qfo", + "format": "remark *.md -qfo && prettier --write \"**/*.js\" && xo --fix", "build-bundle": "browserify . -s remarkFrontmatter > remark-frontmatter.js", "build-mangle": "browserify . -s remarkFrontmatter -p tinyify > remark-frontmatter.min.js", - "build": "npm run build-md && npm run build-bundle && npm run build-mangle", - "lint": "xo", + "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test/index.js", - "test": "npm run build && npm run lint && npm run test-coverage" + "test": "npm run format && npm run build && npm run test-coverage" }, "nyc": { "check-coverage": true, @@ -55,8 +55,16 @@ "functions": 100, "branches": 100 }, + "prettier": { + "tabWidth": 2, + "useTabs": false, + "singleQuote": true, + "bracketSpacing": false, + "semi": false, + "trailingComma": "none" + }, "xo": { - "space": true, + "prettier": true, "esnext": false, "rules": { "guard-for-in": "off" diff --git a/readme.md b/readme.md index cfa1705..560306f 100644 --- a/readme.md +++ b/readme.md @@ -25,25 +25,25 @@ title = "New Website" And our script, `example.js`, looks as follows: ```javascript -var vfile = require('to-vfile'); -var report = require('vfile-reporter'); -var unified = require('unified'); -var parse = require('remark-parse'); -var stringify = require('remark-stringify'); -var frontmatter = require('remark-frontmatter'); +var vfile = require('to-vfile') +var report = require('vfile-reporter') +var unified = require('unified') +var parse = require('remark-parse') +var stringify = require('remark-stringify') +var frontmatter = require('remark-frontmatter') unified() .use(parse) .use(stringify) .use(frontmatter, ['yaml', 'toml']) .use(logger) - .process(vfile.readSync('example.md'), function (err, file) { - console.log(String(file)); - console.error(report(err || file)); - }); + .process(vfile.readSync('example.md'), function(err, file) { + console.log(String(file)) + console.error(report(err || file)) + }) function logger() { - return console.dir; + return console.dir } ``` diff --git a/test/index.js b/test/index.js index bee1900..ce2f273 100644 --- a/test/index.js +++ b/test/index.js @@ -1,115 +1,121 @@ -'use strict'; - -var fs = require('fs'); -var path = require('path'); -var test = require('tape'); -var vfile = require('to-vfile'); -var unified = require('unified'); -var remark = require('remark'); -var not = require('not'); -var hidden = require('is-hidden'); -var frontmatter = require('..'); - -var join = path.join; -var read = fs.readFileSync; -var write = fs.writeFileSync; -var dir = fs.readdirSync; - -test('frontmatter()', function (t) { - t.equal(typeof frontmatter, 'function', 'should be a function'); - - t.doesNotThrow( - function () { - remark().use(frontmatter).freeze(); - }, - 'should not throw if not passed options' - ); - - t.doesNotThrow( - function () { - unified().use(frontmatter).freeze(); - }, - 'should not throw if without parser or compiler' - ); +'use strict' + +var fs = require('fs') +var path = require('path') +var test = require('tape') +var vfile = require('to-vfile') +var unified = require('unified') +var remark = require('remark') +var not = require('not') +var hidden = require('is-hidden') +var frontmatter = require('..') + +var join = path.join +var read = fs.readFileSync +var write = fs.writeFileSync +var dir = fs.readdirSync + +test('frontmatter()', function(t) { + t.equal(typeof frontmatter, 'function', 'should be a function') + + t.doesNotThrow(function() { + remark() + .use(frontmatter) + .freeze() + }, 'should not throw if not passed options') + + t.doesNotThrow(function() { + unified() + .use(frontmatter) + .freeze() + }, 'should not throw if without parser or compiler') t.throws( - function () { - unified().use(frontmatter, [1]).freeze(); + function() { + unified() + .use(frontmatter, [1]) + .freeze() }, /^Error: Expected matter to be an object, not `1`/, 'should throw if not given a preset or a matter' - ); + ) t.throws( - function () { - unified().use(frontmatter, ['jsonml']).freeze(); + function() { + unified() + .use(frontmatter, ['jsonml']) + .freeze() }, /^Error: Missing matter definition for `jsonml`/, 'should throw if given an unknown preset' - ); + ) t.throws( - function () { - unified().use(frontmatter, [{marker: '*'}]).freeze(); + function() { + unified() + .use(frontmatter, [{marker: '*'}]) + .freeze() }, /^Error: Missing `type` in matter `{"marker":"\*"}`/, 'should throw if given a matter without `type`' - ); + ) t.throws( - function () { - unified().use(frontmatter, [{type: 'jsonml'}]).freeze(); + function() { + unified() + .use(frontmatter, [{type: 'jsonml'}]) + .freeze() }, /^Error: Missing `marker` or `fence` in matter `{"type":"jsonml"}`/, 'should throw if given a matter without `marker`' - ); + ) - t.end(); -}); + t.end() +}) -test('fixtures', function (t) { - var base = join(__dirname, 'fixtures'); - var entries = dir(base).filter(not(hidden)); +test('fixtures', function(t) { + var base = join(__dirname, 'fixtures') + var entries = dir(base).filter(not(hidden)) - t.plan(entries.length); + t.plan(entries.length) - entries.forEach(each); + entries.forEach(each) function each(fixture) { - t.test(fixture, function (st) { - var input = vfile.readSync(join(base, fixture, 'input.md')); - var treePath = join(base, fixture, 'tree.json'); - var outputPath = join(base, fixture, 'output.md'); - var output; - var actual; - var expected; - var config; - var proc; + t.test(fixture, function(st) { + var input = vfile.readSync(join(base, fixture, 'input.md')) + var treePath = join(base, fixture, 'tree.json') + var outputPath = join(base, fixture, 'output.md') + var output + var actual + var expected + var config + var proc try { - config = JSON.parse(read(join(base, fixture, 'config.json'))); + config = JSON.parse(read(join(base, fixture, 'config.json'))) } catch (err) {} - proc = remark().use(frontmatter, config); - actual = proc.parse(input); + proc = remark().use(frontmatter, config) + actual = proc.parse(input) try { - output = read(outputPath, 'utf8'); + output = read(outputPath, 'utf8') } catch (err) { - output = String(input); + output = String(input) } try { - expected = JSON.parse(read(treePath)); + expected = JSON.parse(read(treePath)) } catch (err) { /* New fixture. */ - write(treePath, JSON.stringify(actual, 0, 2) + '\n'); - expected = actual; + write(treePath, JSON.stringify(actual, 0, 2) + '\n') + expected = actual } - st.deepEqual(actual, expected, 'tree'); - st.equal(String(proc.processSync(input)), output, 'process'); - st.end(); - }); + st.deepEqual(actual, expected, 'tree') + st.equal(String(proc.processSync(input)), output, 'process') + st.end() + }) } -}); +})