mdast-util-to-nlcst
Advanced tools
Comparing version 3.0.0 to 3.0.1
312
index.js
@@ -11,4 +11,2 @@ /** | ||
/* eslint-env commonjs */ | ||
/* Dependencies. */ | ||
@@ -19,9 +17,12 @@ var repeat = require('repeat-string'); | ||
/* Expose. */ | ||
module.exports = toNLCST; | ||
/* Map of ignored mdast nodes: nodes which have no (simple) | ||
* representation in NLCST. */ | ||
var IGNORE = { | ||
horizontalRule: true, | ||
table: true, | ||
tableRow: true, | ||
tableCell: true | ||
horizontalRule: true, | ||
table: true, | ||
tableRow: true, | ||
tableCell: true | ||
}; | ||
@@ -33,197 +34,194 @@ | ||
/** | ||
* Patch a position on each node in `nodes`. | ||
* `offset` is the offset in `file` this run of content | ||
* starts at. | ||
* Transform `ast` into `nlcst`. | ||
* | ||
* Note that NLCST nodes are concrete, meaning that their | ||
* starting and ending positions can be inferred from their | ||
* content. | ||
* | ||
* @param {Array.<NLCSTNode>} nodes - NLCST nodes. | ||
* @param {Object} location - Bound location info. | ||
* @param {number} offset - Starting offset for `nodes`. | ||
* @return {Array.<NLCSTNode>} - `nodes`. | ||
* @param {Node} tree - MDAST node. | ||
* @param {File} file - Virtual file. | ||
* @param {Parser|Function} Parser - (Instance of) NLCST | ||
* parser. | ||
* @return {NLCSTNode} - NLCST. | ||
*/ | ||
function patch(nodes, location, offset) { | ||
var length = nodes.length; | ||
var index = -1; | ||
var start = offset; | ||
var children; | ||
var node; | ||
var end; | ||
function toNLCST(tree, file, Parser) { | ||
var parser; | ||
var location; | ||
while (++index < length) { | ||
node = nodes[index]; | ||
children = node.children; | ||
/* Warn for invalid parameters. */ | ||
if (!tree || !tree.type) { | ||
throw new Error('mdast-util-to-nlcst expected node'); | ||
} | ||
if (children) { | ||
patch(children, location, start); | ||
} | ||
if (!file || !file.messages) { | ||
throw new Error('mdast-util-to-nlcst expected file'); | ||
} | ||
end = start + toString(node).length; | ||
/* Construct parser. */ | ||
if (!Parser) { | ||
throw new Error('mdast-util-to-nlcst expected parser'); | ||
} | ||
node.position = { | ||
start: location.toPosition(start), | ||
end: location.toPosition(end) | ||
} | ||
location = vfileLocation(file); | ||
start = end; | ||
} | ||
if ( | ||
!tree.position || | ||
!tree.position.start || | ||
!tree.position.start.column || | ||
!tree.position.start.line | ||
) { | ||
throw new Error('mdast-util-to-nlcst expected position on nodes'); | ||
} | ||
return nodes; | ||
parser = 'parse' in Parser ? Parser : new Parser(); | ||
/* Transform mdast into NLCST tokens, and pass these | ||
* into `parser.parse` to insert sentences, paragraphs | ||
* where needed. */ | ||
return parser.parse(one(tree, null, null, file, location, parser)); | ||
} | ||
/** | ||
* Convert all nodes in `parent` (mdast) into NLCST. | ||
* Convert `node` into NLCST. | ||
* | ||
* @param {MDASTNode} parent - Parent node. | ||
* @param {MDASTNode} node - Node. | ||
* @param {number?} index - Position of `node` in `parent`. | ||
* @param {MDASTNode?} parent - Parent node of `node`. | ||
* @param {File} file - Virtual file. | ||
* @param {Object} location - Bound location info. | ||
* @param {Parser} parser - NLCST parser. | ||
* @return {Array.<NLCSTNode>} - Concatenation of calling | ||
* `one` on each MDASTNode in `parent`. | ||
* @return {Array.<NLCSTNode>?} - A list of NLCST nodes, if | ||
* `node` could be converted. | ||
*/ | ||
function all(parent, file, location, parser) { | ||
var children = parent.children; | ||
var length = children && children.length; | ||
var index = -1; | ||
var result = []; | ||
var child; | ||
var node; | ||
var pos; | ||
var prevEndLine; | ||
var prevOffset; | ||
var endLine; | ||
function one(node, index, parent, file, location, parser) { | ||
var type = node.type; | ||
var pos = node.position; | ||
var start = location.toOffset(pos.start); | ||
var end = location.toOffset(pos.end); | ||
var replacement; | ||
while (++index < length) { | ||
node = children[index]; | ||
pos = node.position; | ||
endLine = pos.start.line; | ||
if (type in IGNORE) { | ||
return null; | ||
} | ||
if (prevEndLine && endLine !== prevEndLine) { | ||
child = parser.tokenizeWhiteSpace( | ||
repeat(C_NEWLINE, endLine - prevEndLine) | ||
); | ||
if (node.children) { | ||
replacement = all(node, file, location, parser); | ||
} else if ( | ||
type === 'image' || | ||
type === 'imageReference' | ||
) { | ||
replacement = patch( | ||
parser.tokenize(node.alt), location, start + 2 | ||
); | ||
} else if ( | ||
type === 'text' || | ||
type === 'escape' | ||
) { | ||
replacement = patch( | ||
parser.tokenize(node.value), location, start | ||
); | ||
} else if (node.type === 'break') { | ||
replacement = patch([ | ||
parser.tokenizeWhiteSpace('\n') | ||
], location, start); | ||
} else if (node.type === 'inlineCode') { | ||
replacement = patch([parser.tokenizeSource( | ||
file.toString().slice(start, end) | ||
)], location, start); | ||
} | ||
patch([child], location, prevOffset); | ||
if (child.value.length < 2) { | ||
child.value = repeat(C_NEWLINE, 2); | ||
} | ||
result.push(child); | ||
} | ||
child = one(node, index, parent, file, location, parser); | ||
if (child) { | ||
result = result.concat(child); | ||
} | ||
prevEndLine = pos.end.line; | ||
prevOffset = pos.end.offset; | ||
} | ||
return result; | ||
return replacement || null; | ||
} | ||
/** | ||
* Convert `node` into NLCST. | ||
* Convert all nodes in `parent` (mdast) into NLCST. | ||
* | ||
* @param {MDASTNode} node - Node. | ||
* @param {number?} index - Position of `node` in `parent`. | ||
* @param {MDASTNode?} parent - Parent node of `node`. | ||
* @param {MDASTNode} parent - Parent node. | ||
* @param {File} file - Virtual file. | ||
* @param {Object} location - Bound location info. | ||
* @param {Parser} parser - NLCST parser. | ||
* @return {Array.<NLCSTNode>?} - A list of NLCST nodes, if | ||
* `node` could be converted. | ||
* @return {Array.<NLCSTNode>} - Concatenation of calling | ||
* `one` on each MDASTNode in `parent`. | ||
*/ | ||
function one(node, index, parent, file, location, parser) { | ||
var type = node.type; | ||
var pos = node.position; | ||
var start = location.toOffset(pos.start); | ||
var end = location.toOffset(pos.end); | ||
var replacement; | ||
function all(parent, file, location, parser) { | ||
var children = parent.children; | ||
var length = children && children.length; | ||
var index = -1; | ||
var result = []; | ||
var child; | ||
var node; | ||
var pos; | ||
var prevEndLine; | ||
var prevOffset; | ||
var endLine; | ||
if (type in IGNORE) { | ||
return null; | ||
while (++index < length) { | ||
node = children[index]; | ||
pos = node.position; | ||
endLine = pos.start.line; | ||
if (prevEndLine && endLine !== prevEndLine) { | ||
child = parser.tokenizeWhiteSpace( | ||
repeat(C_NEWLINE, endLine - prevEndLine) | ||
); | ||
patch([child], location, prevOffset); | ||
if (child.value.length < 2) { | ||
child.value = repeat(C_NEWLINE, 2); | ||
} | ||
result.push(child); | ||
} | ||
if (node.children) { | ||
replacement = all(node, file, location, parser); | ||
} else if ( | ||
type === 'image' || | ||
type === 'imageReference' | ||
) { | ||
replacement = patch( | ||
parser.tokenize(node.alt), location, start + 2 | ||
); | ||
} else if ( | ||
type === 'text' || | ||
type === 'escape' | ||
) { | ||
replacement = patch( | ||
parser.tokenize(node.value), location, start | ||
); | ||
} else if (node.type === 'break') { | ||
replacement = patch([ | ||
parser.tokenizeWhiteSpace('\n') | ||
], location, start); | ||
} else if (node.type === 'inlineCode') { | ||
replacement = patch([parser.tokenizeSource( | ||
file.toString().slice(start, end) | ||
)], location, start); | ||
child = one(node, index, parent, file, location, parser); | ||
if (child) { | ||
result = result.concat(child); | ||
} | ||
return replacement || null; | ||
prevEndLine = pos.end.line; | ||
prevOffset = pos.end.offset; | ||
} | ||
return result; | ||
} | ||
/** | ||
* Transform `ast` into `nlcst`. | ||
* Patch a position on each node in `nodes`. | ||
* `offset` is the offset in `file` this run of content | ||
* starts at. | ||
* | ||
* @param {Node} tree - MDAST node. | ||
* @param {File} file - Virtual file. | ||
* @param {Parser|Function} Parser - (Instance of) NLCST | ||
* parser. | ||
* @return {NLCSTNode} - NLCST. | ||
* Note that NLCST nodes are concrete, meaning that their | ||
* starting and ending positions can be inferred from their | ||
* content. | ||
* | ||
* @param {Array.<NLCSTNode>} nodes - NLCST nodes. | ||
* @param {Object} location - Bound location info. | ||
* @param {number} offset - Starting offset for `nodes`. | ||
* @return {Array.<NLCSTNode>} - `nodes`. | ||
*/ | ||
function toNLCST(tree, file, Parser) { | ||
var parser; | ||
var location; | ||
function patch(nodes, location, offset) { | ||
var length = nodes.length; | ||
var index = -1; | ||
var start = offset; | ||
var children; | ||
var node; | ||
var end; | ||
/* Warn for invalid parameters. */ | ||
if (!tree || !tree.type) { | ||
throw new Error('mdast-util-to-nlcst expected node'); | ||
} | ||
while (++index < length) { | ||
node = nodes[index]; | ||
children = node.children; | ||
if (!file || !file.messages) { | ||
throw new Error('mdast-util-to-nlcst expected file'); | ||
if (children) { | ||
patch(children, location, start); | ||
} | ||
/* Construct parser. */ | ||
if (!Parser) { | ||
throw new Error('mdast-util-to-nlcst expected parser'); | ||
} | ||
end = start + toString(node).length; | ||
location = vfileLocation(file); | ||
node.position = { | ||
start: location.toPosition(start), | ||
end: location.toPosition(end) | ||
}; | ||
if ( | ||
!tree.position || | ||
!tree.position.start || | ||
!tree.position.start.column || | ||
!tree.position.start.line | ||
) { | ||
throw new Error('mdast-util-to-nlcst expected position on nodes'); | ||
} | ||
start = end; | ||
} | ||
parser = 'parse' in Parser ? Parser : new Parser(); | ||
/* Transform mdast into NLCST tokens, and pass these | ||
* into `parser.parse` to insert sentences, paragraphs | ||
* where needed. */ | ||
return parser.parse(one(tree, null, null, file, location, parser)); | ||
return nodes; | ||
} | ||
/* Expose. */ | ||
module.exports = toNLCST; |
{ | ||
"name": "mdast-util-to-nlcst", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Transform MDAST to NLCST", | ||
@@ -15,9 +15,3 @@ "license": "MIT", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/mdast-util-to-nlcst.git" | ||
}, | ||
"repository": "https://github.com/wooorm/mdast-util-to-nlcst", | ||
"bugs": "https://github.com/wooorm/mdast-util-to-nlcst/issues", | ||
@@ -28,2 +22,5 @@ "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
@@ -36,33 +33,45 @@ "nlcst-to-string": "^2.0.0", | ||
"browserify": "^13.0.1", | ||
"eslint": "^2.0.0", | ||
"esmangle": "^1.0.1", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^3.0.0", | ||
"jscs-jsdoc": "^2.0.0", | ||
"is-hidden": "^1.0.1", | ||
"negate": "^1.0.0", | ||
"nyc": "^8.3.0", | ||
"parse-dutch": "^3.0.0", | ||
"parse-english": "^3.0.0", | ||
"parse-latin": "^3.1.0", | ||
"remark": "^5.0.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-usage": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"parse-latin": "^3.1.1", | ||
"remark": "^6.0.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.0.0", | ||
"unist-util-inspect": "^4.0.0", | ||
"vfile": "^1.4.0" | ||
"vfile": "^2.0.0", | ||
"xo": "^0.16.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build-md": "remark *.md --quiet --frail", | ||
"build-bundle": "browserify index.js --bare -s mdastUtilToNLCST > mdast-util-to-nlcst.js", | ||
"build-mangle": "esmangle mdast-util-to-nlcst.js > mdast-util-to-nlcst.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"lint": "xo", | ||
"test-api": "node test/index.js", | ||
"test-coverage": "istanbul cover test/index.js", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"rules": { | ||
"max-params": "off" | ||
}, | ||
"ignores": [ | ||
"mdast-util-to-nlcst.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"presets": "wooorm" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14
10815
4
190
1