Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 10, 2018
1 parent 4a42676 commit a72bf6f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 121 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
remark-yaml-config.js
remark-yaml-config.min.js
40 changes: 20 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
'use strict';
'use strict'

var jsYAML = require('js-yaml');
var jsYAML = require('js-yaml')

module.exports = yamlConfig;
module.exports = yamlConfig

/* Modify remark to read configuration from comments. */
function yamlConfig() {
var Parser = this.Parser;
var Compiler = this.Compiler;
var parser = Parser && Parser.prototype.blockTokenizers;
var compiler = Compiler && Compiler.prototype.visitors;
var Parser = this.Parser
var Compiler = this.Compiler
var parser = Parser && Parser.prototype.blockTokenizers
var compiler = Compiler && Compiler.prototype.visitors

if (parser && parser.yamlFrontMatter) {
parser.yamlFrontMatter = factory(parser.yamlFrontMatter);
parser.yamlFrontMatter = factory(parser.yamlFrontMatter)
}

if (compiler && compiler.yaml) {
compiler.yaml = factory(compiler.yaml);
compiler.yaml = factory(compiler.yaml)
}
}

/* Wrapper factory. */
function factory(original) {
replacement.locator = original.locator;
replacement.locator = original.locator

return replacement;
return replacement

/* Replacer for tokeniser or visitor. */
function replacement(node) {
var self = this;
var result = original.apply(self, arguments);
var marker = result && result.type ? result : node;
var data;
var self = this
var result = original.apply(self, arguments)
var marker = result && result.type ? result : node
var data

try {
data = jsYAML.safeLoad(marker.value);
data = data && data.remark;
data = jsYAML.safeLoad(marker.value)
data = data && data.remark

if (data) {
self.setOptions(data);
self.setOptions(data)
}
} catch (err) {
self.file.fail(err.message, marker);
self.file.fail(err.message, marker)
}

return result;
return result
}
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"browserify": "^16.0.0",
"nyc": "^12.0.0",
"prettier": "^1.14.2",
"remark": "^9.0.0",
"remark-cli": "^5.0.0",
"remark-frontmatter": "^1.0.0",
Expand All @@ -36,23 +37,30 @@
"xo": "^0.22.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s remarkYamlConfig > remark-yaml-config.js",
"build-mangle": "browserify . -s remarkYamlConfig -p tinyify > remark-yaml-config.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.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,
"lines": 100,
"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",
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ remark:
And our script, `example.js`, looks as follows:

```javascript
var vfile = require('to-vfile');
var remark = require('remark');
var frontmatter = require('remark-frontmatter');
var yamlConfig = require('remark-yaml-config');
var vfile = require('to-vfile')
var remark = require('remark')
var frontmatter = require('remark-frontmatter')
var yamlConfig = require('remark-yaml-config')

remark()
.use(frontmatter)
.use(yamlConfig)
.process(vfile.readSync('example.md'), function (err, file) {
if (err) throw err;
console.log(String(file));
});
.process(vfile.readSync('example.md'), function(err, file) {
if (err) throw err
console.log(String(file))
})
```

Now, running `node example` yields:
Expand Down
160 changes: 73 additions & 87 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,94 @@
'use strict';
'use strict'

var test = require('tape');
var unified = require('unified');
var remark = require('remark');
var frontmatter = require('remark-frontmatter');
var html = require('remark-html');
var yamlConfig = require('.');
var test = require('tape')
var unified = require('unified')
var remark = require('remark')
var frontmatter = require('remark-frontmatter')
var html = require('remark-html')
var yamlConfig = require('.')

var processor = remark().use(frontmatter).use(yamlConfig);
var processorHTML = remark().use(frontmatter).use(yamlConfig).use(html);

test('remark-yaml-config', function (t) {
test('remark-yaml-config', function(t) {
t.equal(
processor.processSync('# Foo bar\n').toString(),
remark()
.use(frontmatter)
.use(yamlConfig)
.processSync('# Foo bar\n')
.toString(),
'# Foo bar\n',
'should not fail without yaml'
);
)

t.equal(
processor.processSync([
'---',
'remark:',
' commonmark: true',
'---',
'',
'1) Foo',
''
].join('\n')).toString(),
[
'---',
'remark:',
' commonmark: true',
'---',
'',
'1. Foo',
''
].join('\n'),
remark()
.use(frontmatter)
.use(yamlConfig)
.processSync(
['---', 'remark:', ' commonmark: true', '---', '', '1) Foo', ''].join(
'\n'
)
)
.toString(),
['---', 'remark:', ' commonmark: true', '---', '', '1. Foo', ''].join(
'\n'
),
'should set parse options'
);
)

t.equal(
processor.processSync([
'---',
'remark:',
' bullet: "*"',
'---',
'',
'- Foo',
''
].join('\n')).toString(),
[
'---',
'remark:',
' bullet: "*"',
'---',
'',
'* Foo',
''
].join('\n'),
remark()
.use(frontmatter)
.use(yamlConfig)
.processSync(
['---', 'remark:', ' bullet: "*"', '---', '', '- Foo', ''].join('\n')
)
.toString(),
['---', 'remark:', ' bullet: "*"', '---', '', '* Foo', ''].join('\n'),
'should set stringification options'
);
)

t.throws(
function () {
processor.processSync([
'---',
'remark:',
' bullet: "?"',
'---',
'',
'- Foo',
''
].join('\n')).toString();
function() {
remark()
.use(frontmatter)
.use(yamlConfig)
.processSync(
['---', 'remark:', ' bullet: "?"', '---', '', '- Foo', ''].join(
'\n'
)
)
.toString()
},
/1:1-4:4: Invalid value `\?` for setting `options\.bullet`/,
'should throw exceptions with location information'
);
)

t.doesNotThrow(
function () {
unified().use(yamlConfig).freeze();
},
'should not throw without parser / compiler'
);
t.doesNotThrow(function() {
unified()
.use(yamlConfig)
.freeze()
}, 'should not throw without parser / compiler')

t.equal(
processorHTML.processSync([
'---',
'remark:',
' commonmark: true',
' bullet: "*"',
'---',
'',
'1) Foo',
''
].join('\n')).toString(),
[
'<ol>',
'<li>Foo</li>',
'</ol>',
''
].join('\n'),
remark()
.use(frontmatter)
.use(yamlConfig)
.use(html)
.processSync(
[
'---',
'remark:',
' commonmark: true',
' bullet: "*"',
'---',
'',
'1) Foo',
''
].join('\n')
)
.toString(),
['<ol>', '<li>Foo</li>', '</ol>', ''].join('\n'),
'should ignore missing compilers'
);
)

t.end();
});
t.end()
})

0 comments on commit a72bf6f

Please sign in to comment.