mdast-util-to-nlcst
Advanced tools
Comparing version 3.2.0 to 3.2.1
184
index.js
@@ -1,39 +0,33 @@ | ||
'use strict'; | ||
'use strict' | ||
var repeat = require('repeat-string'); | ||
var vfileLocation = require('vfile-location'); | ||
var position = require('unist-util-position'); | ||
var toString = require('nlcst-to-string'); | ||
var repeat = require('repeat-string') | ||
var vfileLocation = require('vfile-location') | ||
var position = require('unist-util-position') | ||
var toString = require('nlcst-to-string') | ||
module.exports = toNLCST; | ||
module.exports = toNLCST | ||
var ignore = [ | ||
'table', | ||
'tableRow', | ||
'tableCell' | ||
]; | ||
var ignore = ['table', 'tableRow', 'tableCell'] | ||
var source = [ | ||
'inlineCode' | ||
]; | ||
var source = ['inlineCode'] | ||
var newline = '\n'; | ||
var newline = '\n' | ||
/* Transform `tree` into `nlcst`. */ | ||
// Transform `tree` into `nlcst`. | ||
function toNLCST(tree, file, Parser, options) { | ||
var settings = options || {}; | ||
var parser; | ||
var settings = options || {} | ||
var parser | ||
/* Warn for invalid parameters. */ | ||
// Warn for invalid parameters. | ||
if (!tree || !tree.type) { | ||
throw new Error('mdast-util-to-nlcst expected node'); | ||
throw new Error('mdast-util-to-nlcst expected node') | ||
} | ||
if (!file || !file.messages) { | ||
throw new Error('mdast-util-to-nlcst expected file'); | ||
throw new Error('mdast-util-to-nlcst expected file') | ||
} | ||
/* Construct parser. */ | ||
// Construct parser. | ||
if (!Parser) { | ||
throw new Error('mdast-util-to-nlcst expected parser'); | ||
throw new Error('mdast-util-to-nlcst expected parser') | ||
} | ||
@@ -47,127 +41,133 @@ | ||
) { | ||
throw new Error('mdast-util-to-nlcst expected position on nodes'); | ||
throw new Error('mdast-util-to-nlcst expected position on nodes') | ||
} | ||
parser = 'parse' in Parser ? Parser : new Parser(); | ||
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({ | ||
doc: String(file), | ||
location: vfileLocation(file), | ||
parser: parser, | ||
ignore: ignore.concat(settings.ignore || []), | ||
source: source.concat(settings.source || []) | ||
}, tree)); | ||
// Transform mdast into NLCST tokens, and pass these into `parser.parse` to | ||
// insert sentences, paragraphs where needed. | ||
return parser.parse( | ||
one( | ||
{ | ||
doc: String(file), | ||
location: vfileLocation(file), | ||
parser: parser, | ||
ignore: ignore.concat(settings.ignore || []), | ||
source: source.concat(settings.source || []) | ||
}, | ||
tree | ||
) | ||
) | ||
} | ||
/* Convert `node` into NLCST. */ | ||
// Convert `node` into NLCST. | ||
function one(config, node) { | ||
var offset = config.location.toOffset; | ||
var parser = config.parser; | ||
var doc = config.doc; | ||
var type = node.type; | ||
var start = offset(position.start(node)); | ||
var end = offset(position.end(node)); | ||
var offset = config.location.toOffset | ||
var parser = config.parser | ||
var doc = config.doc | ||
var type = node.type | ||
var start = offset(position.start(node)) | ||
var end = offset(position.end(node)) | ||
if (config.ignore.indexOf(type) === -1) { | ||
if (config.source.indexOf(type) !== -1) { | ||
return patch(config, [parser.tokenizeSource(doc.slice(start, end))], start); | ||
return patch( | ||
config, | ||
[parser.tokenizeSource(doc.slice(start, end))], | ||
start | ||
) | ||
} | ||
if (node.children) { | ||
return all(config, node); | ||
return all(config, node) | ||
} | ||
if (type === 'image' || type === 'imageReference') { | ||
return patch(config, parser.tokenize(node.alt), start + 2); | ||
return patch(config, parser.tokenize(node.alt), start + 2) | ||
} | ||
if (type === 'text' || type === 'escape') { | ||
return patch(config, parser.tokenize(node.value), start); | ||
return patch(config, parser.tokenize(node.value), start) | ||
} | ||
if (node.type === 'break') { | ||
return patch(config, [parser.tokenizeWhiteSpace('\n')], start); | ||
return patch(config, [parser.tokenizeWhiteSpace('\n')], start) | ||
} | ||
} | ||
return null; | ||
return null | ||
} | ||
/* Convert all nodes in `parent` (mdast) into NLCST. */ | ||
// Convert all nodes in `parent` (mdast) into NLCST. | ||
function all(config, parent) { | ||
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; | ||
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 | ||
while (++index < length) { | ||
node = children[index]; | ||
pos = node.position; | ||
endLine = position.start(node).line; | ||
node = children[index] | ||
pos = node.position | ||
endLine = position.start(node).line | ||
if (prevEndLine && endLine !== prevEndLine) { | ||
child = config.parser.tokenizeWhiteSpace(repeat(newline, endLine - prevEndLine)); | ||
patch(config, [child], prevOffset); | ||
child = config.parser.tokenizeWhiteSpace( | ||
repeat(newline, endLine - prevEndLine) | ||
) | ||
patch(config, [child], prevOffset) | ||
if (child.value.length < 2) { | ||
child.value = repeat(newline, 2); | ||
child.value = repeat(newline, 2) | ||
} | ||
result.push(child); | ||
result.push(child) | ||
} | ||
child = one(config, node); | ||
child = one(config, node) | ||
if (child) { | ||
result = result.concat(child); | ||
result = result.concat(child) | ||
} | ||
pos = position.end(node); | ||
prevEndLine = pos.line; | ||
prevOffset = pos.offset; | ||
pos = position.end(node) | ||
prevEndLine = pos.line | ||
prevOffset = pos.offset | ||
} | ||
return result; | ||
return result | ||
} | ||
/* Patch a position on each node in `nodes`. | ||
* `offset` is the offset in `file` this run of content | ||
* starts at. */ | ||
// Patch a position on each node in `nodes`. | ||
// `offset` is the offset in `file` this run of content starts at. | ||
function patch(config, nodes, offset) { | ||
var position = config.location.toPosition; | ||
var length = nodes.length; | ||
var index = -1; | ||
var start = offset; | ||
var children; | ||
var node; | ||
var end; | ||
var position = config.location.toPosition | ||
var length = nodes.length | ||
var index = -1 | ||
var start = offset | ||
var children | ||
var node | ||
var end | ||
while (++index < length) { | ||
node = nodes[index]; | ||
children = node.children; | ||
node = nodes[index] | ||
children = node.children | ||
if (children) { | ||
patch(config, children, start); | ||
patch(config, children, start) | ||
} | ||
end = start + toString(node).length; | ||
end = start + toString(node).length | ||
node.position = { | ||
start: position(start), | ||
end: position(end) | ||
}; | ||
node.position = {start: position(start), end: position(end)} | ||
start = end; | ||
start = end | ||
} | ||
return nodes; | ||
return nodes | ||
} |
{ | ||
"name": "mdast-util-to-nlcst", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Transform MDAST to NLCST", | ||
@@ -17,5 +17,5 @@ "license": "MIT", | ||
"bugs": "https://github.com/syntax-tree/mdast-util-to-nlcst/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)" | ||
], | ||
@@ -32,27 +32,27 @@ "files": [ | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"esmangle": "^1.0.1", | ||
"browserify": "^16.0.0", | ||
"is-hidden": "^1.0.1", | ||
"negate": "^1.0.0", | ||
"nyc": "^11.0.0", | ||
"nyc": "^13.0.0", | ||
"parse-dutch": "^4.0.0", | ||
"parse-english": "^4.0.0", | ||
"parse-latin": "^4.0.0", | ||
"remark": "^8.0.0", | ||
"remark-cli": "^4.0.0", | ||
"prettier": "^1.14.3", | ||
"remark": "^10.0.0", | ||
"remark-cli": "^6.0.0", | ||
"remark-frontmatter": "^1.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.0.0", | ||
"vfile": "^2.0.0", | ||
"xo": "^0.18.0" | ||
"tinyify": "^2.4.3", | ||
"vfile": "^3.0.0", | ||
"xo": "^0.23.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark *.md -qfo", | ||
"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": "xo", | ||
"format": "remark *.md -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify . -s mdastUtilToNLCST > mdast-util-to-nlcst.js", | ||
"build-mangle": "browserify . -s mdastUtilToNLCST -p tinyify > mdast-util-to-nlcst.min.js", | ||
"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" | ||
}, | ||
@@ -65,4 +65,12 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
@@ -69,0 +77,0 @@ "rules": { |
@@ -5,3 +5,3 @@ # mdast-util-to-nlcst [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
> **Note** You probably want to use [`remark-retext`][remark-retext]. | ||
> **Note**: You probably want to use [`remark-retext`][remark-retext]. | ||
@@ -19,14 +19,14 @@ ## Installation | ||
```javascript | ||
var toNLCST = require('mdast-util-to-nlcst'); | ||
var inspect = require('unist-util-inspect'); | ||
var English = require('parse-english'); | ||
var remark = require('remark'); | ||
var vfile = require('vfile'); | ||
var toNLCST = require('mdast-util-to-nlcst') | ||
var inspect = require('unist-util-inspect') | ||
var English = require('parse-english') | ||
var remark = require('remark') | ||
var vfile = require('vfile') | ||
var file = vfile('Some *foo*sball.'); | ||
var tree = remark().parse(file); | ||
var file = vfile('Some *foo*sball.') | ||
var tree = remark().parse(file) | ||
var nlcst = toNLCST(tree, file, English); | ||
var nlcst = toNLCST(tree, file, English) | ||
console.log(inspect(nlcst)); | ||
console.log(inspect(nlcst)) | ||
``` | ||
@@ -82,3 +82,3 @@ | ||
`'inlineCode'` is always ignored. | ||
`'inlineCode'` is always marked as source. | ||
@@ -89,2 +89,57 @@ ##### Returns | ||
##### Examples | ||
###### `ignore` | ||
Say we have the following file `example.md`: | ||
```markdown | ||
A paragraph. | ||
> A paragraph in a block quote. | ||
``` | ||
…and if we now transform with `ignore: ['blockquote']`, we get: | ||
```txt | ||
RootNode[2] (1:1-3:1, 0-14) | ||
├─ ParagraphNode[1] (1:1-1:13, 0-12) | ||
│ └─ SentenceNode[4] (1:1-1:13, 0-12) | ||
│ ├─ WordNode[1] (1:1-1:2, 0-1) | ||
│ │ └─ TextNode: "A" (1:1-1:2, 0-1) | ||
│ ├─ WhiteSpaceNode: " " (1:2-1:3, 1-2) | ||
│ ├─ WordNode[1] (1:3-1:12, 2-11) | ||
│ │ └─ TextNode: "paragraph" (1:3-1:12, 2-11) | ||
│ └─ PunctuationNode: "." (1:12-1:13, 11-12) | ||
└─ WhiteSpaceNode: "\n\n" (1:13-3:1, 12-14) | ||
``` | ||
###### `source` | ||
Say we have the following file `example.md`: | ||
```markdown | ||
A paragraph. | ||
> A paragraph in a block quote. | ||
``` | ||
…and if we now transform with `source: ['blockquote']`, we get: | ||
```txt | ||
RootNode[3] (1:1-3:32, 0-45) | ||
├─ ParagraphNode[1] (1:1-1:13, 0-12) | ||
│ └─ SentenceNode[4] (1:1-1:13, 0-12) | ||
│ ├─ WordNode[1] (1:1-1:2, 0-1) | ||
│ │ └─ TextNode: "A" (1:1-1:2, 0-1) | ||
│ ├─ WhiteSpaceNode: " " (1:2-1:3, 1-2) | ||
│ ├─ WordNode[1] (1:3-1:12, 2-11) | ||
│ │ └─ TextNode: "paragraph" (1:3-1:12, 2-11) | ||
│ └─ PunctuationNode: "." (1:12-1:13, 11-12) | ||
├─ WhiteSpaceNode: "\n\n" (1:13-3:1, 12-14) | ||
└─ ParagraphNode[1] (3:1-3:32, 14-45) | ||
└─ SentenceNode[1] (3:1-3:32, 14-45) | ||
└─ SourceNode: "> A paragraph in a block quote." (3:1-3:32, 14-45) | ||
``` | ||
## Related | ||
@@ -101,2 +156,10 @@ | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/mdast`][contributing] for ways to get | ||
started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
@@ -118,5 +181,5 @@ | ||
[license]: LICENSE | ||
[license]: license | ||
[author]: http://wooorm.com | ||
[author]: https://wooorm.com | ||
@@ -127,3 +190,3 @@ [mdast]: https://github.com/syntax-tree/mdast | ||
[remark-retext]: https://github.com/wooorm/remark-retext | ||
[remark-retext]: https://github.com/remarkjs/remark-retext | ||
@@ -141,1 +204,5 @@ [vfile]: https://github.com/vfile/vfile | ||
[source]: https://github.com/syntax-tree/nlcst#source | ||
[contributing]: https://github.com/syntax-tree/mdast/blob/master/contributing.md | ||
[coc]: https://github.com/syntax-tree/mdast/blob/master/code-of-conduct.md |
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
12267
201
16
1