mdast-util-to-nlcst
Advanced tools
Comparing version 4.0.1 to 5.0.0
129
index.js
@@ -1,16 +0,46 @@ | ||
'use strict' | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('mdast').Content} Content | ||
* @typedef {import('vfile').VFile} VFile | ||
* @typedef {import('vfile-location').Location} LocationInterface | ||
* @typedef {{ | ||
* parse(nodes: Node[]): Node | ||
* tokenizeSource(value: string): Node | ||
* tokenizeWhiteSpace(value: string): Node | ||
* tokenize(value: string): Node[] | ||
* }} ParserInstance | ||
* @typedef {new () => ParserInstance} ParserConstructor | ||
* | ||
* @typedef Options | ||
* @property {Array.<string>} [ignore] | ||
* @property {Array.<string>} [source] | ||
* | ||
* @typedef Context | ||
* @property {string} doc | ||
* @property {LocationInterface} location | ||
* @property {ParserInstance} parser | ||
* @property {Array.<string>} ignore | ||
* @property {Array.<string>} source | ||
*/ | ||
var toString = require('nlcst-to-string') | ||
var repeat = require('repeat-string') | ||
var position = require('unist-util-position') | ||
var vfileLocation = require('vfile-location') | ||
import repeat from 'repeat-string' | ||
import {toString} from 'nlcst-to-string' | ||
import {pointStart, pointEnd} from 'unist-util-position' | ||
import vfileLocation from 'vfile-location' | ||
module.exports = toNlcst | ||
// Transform a `tree` in mdast to nlcst. | ||
function toNlcst(tree, file, Parser, options) { | ||
var settings = options || {} | ||
/** | ||
* Transform a `tree` in mdast to nlcst. | ||
* | ||
* @param {Node} tree | ||
* @param {VFile} file | ||
* @param {ParserInstance|ParserConstructor} Parser | ||
* @param {Options} [options] | ||
*/ | ||
export function toNlcst(tree, file, Parser, options = {}) { | ||
/** @type {ParserInstance} */ | ||
var parser | ||
// Warn for invalid parameters. | ||
// Crash on invalid parameters. | ||
if (!tree || !tree.type) { | ||
@@ -47,3 +77,3 @@ throw new Error('mdast-util-to-nlcst expected node') | ||
location: vfileLocation(file), | ||
parser: parser, | ||
parser, | ||
ignore: [].concat( | ||
@@ -53,6 +83,7 @@ 'table', | ||
'tableCell', | ||
settings.ignore || [] | ||
options.ignore || [] | ||
), | ||
source: [].concat('inlineCode', settings.source || []) | ||
source: [].concat('inlineCode', options.source || []) | ||
}, | ||
// @ts-ignore assume mdast node. | ||
tree | ||
@@ -63,17 +94,23 @@ ) | ||
// Transform a single node. | ||
/** | ||
* Transform a single node. | ||
* @param {Context} config | ||
* @param {Content} node | ||
* @returns {Array.<Node>|undefined} | ||
*/ | ||
function one(config, node) { | ||
/** @type {number} */ | ||
var start | ||
var end | ||
if (config.ignore.indexOf(node.type) < 0) { | ||
// To do: next major — nodes should have offsets, so | ||
// `config.location.toOffset` should be superfluous. | ||
start = config.location.toOffset(position.start(node)) | ||
end = config.location.toOffset(position.end(node)) | ||
if (!config.ignore.includes(node.type)) { | ||
start = node.position.start.offset | ||
if (config.source.indexOf(node.type) > -1) { | ||
if (config.source.includes(node.type)) { | ||
return patch( | ||
config, | ||
[config.parser.tokenizeSource(config.doc.slice(start, end))], | ||
[ | ||
config.parser.tokenizeSource( | ||
config.doc.slice(start, node.position.end.offset) | ||
) | ||
], | ||
start | ||
@@ -83,3 +120,4 @@ ) | ||
if (node.children) { | ||
if ('children' in node) { | ||
// @ts-ignore looks like a parent. | ||
return all(config, node) | ||
@@ -96,4 +134,3 @@ } | ||
// To do: next major — remove `escape`. | ||
if (node.type === 'text' || node.type === 'escape') { | ||
if (node.type === 'text') { | ||
return patch(config, config.parser.tokenize(node.value), start) | ||
@@ -104,14 +141,25 @@ } | ||
// Transform all nodes in `parent`. | ||
/** | ||
* Transform all nodes in `parent`. | ||
* @param {Context} config | ||
* @param {Parent} parent | ||
* @returns {Array.<Node>} | ||
*/ | ||
function all(config, parent) { | ||
/** @type {Array.<Node>} */ | ||
var result = [] | ||
var index = -1 | ||
/** @type {Node} */ | ||
var lineEnding | ||
/** @type {Content} */ | ||
var child | ||
/** @type {Point} */ | ||
var end | ||
/** @type {Point} */ | ||
var start | ||
while (++index < parent.children.length) { | ||
// @ts-ignore Assume `parent` is an mdast parent. | ||
child = parent.children[index] | ||
start = position.start(child) | ||
start = pointStart(child) | ||
@@ -124,3 +172,7 @@ if (end && start.line !== end.line) { | ||
if (lineEnding.value.length < 2) { | ||
if ( | ||
'value' in lineEnding && | ||
typeof lineEnding.value === 'string' && | ||
lineEnding.value.length < 2 | ||
) { | ||
lineEnding.value = '\n\n' | ||
@@ -133,3 +185,3 @@ } | ||
result = result.concat(one(config, child) || []) | ||
end = position.end(child) | ||
end = pointEnd(child) | ||
} | ||
@@ -140,8 +192,18 @@ | ||
// 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. | ||
* | ||
* @template {Array.<Node>} T | ||
* @param {Context} config | ||
* @param {T} nodes | ||
* @param {number} offset | ||
* @returns {T} | ||
*/ | ||
function patch(config, nodes, offset) { | ||
var index = -1 | ||
var start = offset | ||
/** @type {number} */ | ||
var end | ||
/** @type {Node} */ | ||
var node | ||
@@ -152,3 +214,4 @@ | ||
if (node.children) { | ||
if ('children' in node) { | ||
// @ts-ignore looks like a parent. | ||
patch(config, node.children, start) | ||
@@ -155,0 +218,0 @@ } |
{ | ||
"name": "mdast-util-to-nlcst", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "mdast utility to transform to nlcst", | ||
@@ -28,19 +28,26 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"nlcst-to-string": "^2.0.0", | ||
"@types/mdast": "^3.0.0", | ||
"@types/repeat-string": "^1.0.0", | ||
"@types/unist": "^2.0.0", | ||
"nlcst-to-string": "^3.0.0", | ||
"repeat-string": "^1.0.0", | ||
"unist-util-position": "^3.0.0", | ||
"unist-util-position": "^4.0.0", | ||
"vfile-location": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^17.0.0", | ||
"is-hidden": "^1.0.0", | ||
"negate": "^1.0.0", | ||
"nyc": "^15.0.0", | ||
"parse-dutch": "^4.0.0", | ||
"parse-english": "^4.0.0", | ||
"parse-latin": "^4.0.0", | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"is-hidden": "^2.0.0", | ||
"parse-dutch": "^5.0.0", | ||
"parse-english": "^5.0.0", | ||
"parse-latin": "^5.0.0", | ||
"prettier": "^2.0.0", | ||
@@ -52,22 +59,17 @@ "remark": "^13.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"to-vfile": "^6.1.0", | ||
"xo": "^0.34.0" | ||
"to-vfile": "^6.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
}, | ||
"scripts": { | ||
"format": "remark *.md -qfo && prettier . --write && 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 format && npm run build && npm run test-coverage" | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test/index.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -83,11 +85,6 @@ "tabWidth": 2, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"unicorn/no-fn-reference-in-iterator": "off", | ||
"unicorn/prefer-includes": "off", | ||
"unicorn/prefer-optional-catch-binding": "off" | ||
}, | ||
"ignores": [ | ||
"mdast-util-to-nlcst.js" | ||
] | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -98,3 +95,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -17,2 +17,5 @@ # mdast-util-to-nlcst | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -27,13 +30,12 @@ | ||
```js | ||
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') | ||
import vfile from 'vfile' | ||
import {ParseEnglish} from 'parse-english' | ||
import {inspect} from 'unist-util-inspect' | ||
import fromMarkdown from 'mdast-util-from-markdown' | ||
import {toNlcst} from 'mdast-util-to-nlcst' | ||
var file = vfile('Some *foo*sball.') | ||
var mdast = remark().parse(file) | ||
var mdast = fromMarkdown(file) | ||
var nlcst = toNlcst(mdast, file, ParseEnglish) | ||
var nlcst = toNlcst(mdast, file, English) | ||
console.log(inspect(nlcst)) | ||
@@ -59,2 +61,5 @@ ``` | ||
This package exports the following identifiers: `toNlcst`. | ||
There is no default export. | ||
### `toNlcst(tree, file, Parser[, options])` | ||
@@ -186,5 +191,5 @@ | ||
[build-badge]: https://img.shields.io/travis/syntax-tree/mdast-util-to-nlcst.svg | ||
[build-badge]: https://github.com/syntax-tree/mdast-util-to-nlcst/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/syntax-tree/mdast-util-to-nlcst | ||
[build]: https://github.com/syntax-tree/mdast-util-to-nlcst/actions | ||
@@ -191,0 +196,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-to-nlcst.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16909
5
230
255
Yes
7
18
1
+ Added@types/mdast@^3.0.0
+ Added@types/repeat-string@^1.0.0
+ Added@types/unist@^2.0.0
+ Added@types/mdast@3.0.15(transitive)
+ Added@types/nlcst@1.0.4(transitive)
+ Added@types/repeat-string@1.6.5(transitive)
+ Added@types/unist@2.0.11(transitive)
+ Addednlcst-to-string@3.1.1(transitive)
+ Addedunist-util-position@4.0.4(transitive)
- Removednlcst-to-string@2.0.4(transitive)
- Removedunist-util-position@3.1.0(transitive)
Updatednlcst-to-string@^3.0.0
Updatedunist-util-position@^4.0.0