-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
5 changed files
with
118 additions
and
121 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-yaml-config.js | ||
remark-yaml-config.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,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 | ||
} | ||
} |
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
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,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() | ||
}) |