-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
15 changed files
with
390 additions
and
333 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 | ||
rehype.js | ||
rehype.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
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,80 +1,93 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var fromParse5 = require('hast-util-from-parse5'); | ||
var Parser5 = require('parse5/lib/parser'); | ||
var xtend = require('xtend'); | ||
var errors = require('./errors.json'); | ||
var fromParse5 = require('hast-util-from-parse5') | ||
var Parser5 = require('parse5/lib/parser') | ||
var xtend = require('xtend') | ||
var errors = require('./errors.json') | ||
|
||
var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'; | ||
var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-' | ||
|
||
var fatalities = {2: true, 1: false, 0: null}; | ||
var fatalities = {2: true, 1: false, 0: null} | ||
|
||
module.exports = parse; | ||
module.exports = parse | ||
|
||
function parse(options) { | ||
var settings = xtend(options, this.data('settings')); | ||
var position = typeof settings.position === 'boolean' ? settings.position : true; | ||
var settings = xtend(options, this.data('settings')) | ||
var position = settings.position | ||
|
||
this.Parser = parser; | ||
position = typeof position === 'boolean' ? position : true | ||
|
||
this.Parser = parser | ||
|
||
function parser(doc, file) { | ||
var fn = settings.fragment ? 'parseFragment' : 'parse'; | ||
var onParseError = settings.emitParseErrors ? onerror : null; | ||
var fn = settings.fragment ? 'parseFragment' : 'parse' | ||
var onParseError = settings.emitParseErrors ? onerror : null | ||
var parse5 = new Parser5({ | ||
sourceCodeLocationInfo: position, | ||
onParseError: onParseError, | ||
scriptingEnabled: false | ||
}); | ||
}) | ||
|
||
return fromParse5(parse5[fn](doc), { | ||
file: file, | ||
verbose: settings.verbose | ||
}); | ||
}) | ||
|
||
function onerror(err) { | ||
var code = err.code; | ||
var name = camelcase(code); | ||
var setting = settings[name]; | ||
var config = setting === undefined || setting === null ? true : setting; | ||
var level = typeof config === 'number' ? config : config ? 1 : 0; | ||
var start = {line: err.startLine, column: err.startCol, offset: err.startOffset}; | ||
var end = {line: err.endLine, column: err.endCol, offset: err.endOffset}; | ||
var info; | ||
var message; | ||
var code = err.code | ||
var name = camelcase(code) | ||
var setting = settings[name] | ||
var config = setting === undefined || setting === null ? true : setting | ||
var level = typeof config === 'number' ? config : config ? 1 : 0 | ||
var start = { | ||
line: err.startLine, | ||
column: err.startCol, | ||
offset: err.startOffset | ||
} | ||
var end = {line: err.endLine, column: err.endCol, offset: err.endOffset} | ||
var info | ||
var message | ||
|
||
if (level) { | ||
info = errors[name] || /* istanbul ignore next */ {reason: '', description: ''}; | ||
message = file.message(format(info.reason), {start: start, end: end}); | ||
message.source = 'parse-error'; | ||
message.ruleId = code; | ||
message.fatal = fatalities[level]; | ||
message.note = format(info.description); | ||
message.url = info.url === false ? null : base + code; | ||
info = errors[name] || /* istanbul ignore next */ { | ||
reason: '', | ||
description: '' | ||
} | ||
|
||
message = file.message(format(info.reason), {start: start, end: end}) | ||
message.source = 'parse-error' | ||
message.ruleId = code | ||
message.fatal = fatalities[level] | ||
message.note = format(info.description) | ||
message.url = info.url === false ? null : base + code | ||
} | ||
|
||
function format(value) { | ||
return value | ||
.replace(/%c(?:-(\d+))?/g, char) | ||
.replace(/%x/g, encodedChar); | ||
return value.replace(/%c(?:-(\d+))?/g, char).replace(/%x/g, encodedChar) | ||
} | ||
|
||
function char($0, $1) { | ||
var offset = $1 ? -parseInt($1, 10) : 0; | ||
var char = doc.charAt(err.startOffset + offset); | ||
return char === '`' ? '` ` `' : char; | ||
var offset = $1 ? -parseInt($1, 10) : 0 | ||
var char = doc.charAt(err.startOffset + offset) | ||
return char === '`' ? '` ` `' : char | ||
} | ||
|
||
function encodedChar() { | ||
return '0x' + doc.charCodeAt(err.startOffset).toString(16).toUpperCase(); | ||
var char = doc | ||
.charCodeAt(err.startOffset) | ||
.toString(16) | ||
.toUpperCase() | ||
|
||
return '0x' + char | ||
} | ||
} | ||
} | ||
} | ||
|
||
function camelcase(value) { | ||
return value.replace(/-[a-z]/g, replacer); | ||
return value.replace(/-[a-z]/g, replacer) | ||
} | ||
|
||
function replacer($0) { | ||
return $0.charAt(1).toUpperCase(); | ||
return $0.charAt(1).toUpperCase() | ||
} |
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,16 +1,16 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var xtend = require('xtend'); | ||
var toHTML = require('hast-util-to-html'); | ||
var xtend = require('xtend') | ||
var toHTML = require('hast-util-to-html') | ||
|
||
module.exports = stringify; | ||
module.exports = stringify | ||
|
||
function stringify(config) { | ||
var settings = xtend(config, this.data('settings')); | ||
var settings = xtend(config, this.data('settings')) | ||
|
||
this.Compiler = compiler; | ||
this.Compiler = compiler | ||
|
||
function compiler(tree) { | ||
return toHTML(tree, settings); | ||
return toHTML(tree, settings) | ||
} | ||
} |
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,10 +1,10 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var unified = require('unified'); | ||
var parse = require('rehype-parse'); | ||
var stringify = require('rehype-stringify'); | ||
var unified = require('unified') | ||
var parse = require('rehype-parse') | ||
var stringify = require('rehype-stringify') | ||
|
||
module.exports = unified() | ||
.use(parse) | ||
.use(stringify) | ||
.freeze(); | ||
.freeze() |
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.