Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdast-util-to-nlcst

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-to-nlcst - npm Package Compare versions

Comparing version 5.0.2 to 5.1.0

29

index.d.ts
/**
* Transform a `tree` in mdast to nlcst.
*
* @param {Node} tree
* @param {MdastNode} tree
* @param {VFile} file

@@ -10,23 +10,26 @@ * @param {ParserInstance|ParserConstructor} Parser

export function toNlcst(
tree: Node,
tree: MdastNode,
file: VFile,
Parser: ParserInstance | ParserConstructor,
options?: Options
): import('unist').Node
export type Node = import('unist').Node
export type Parent = import('unist').Parent
options?: Options | undefined
): import('unist').Node<import('unist').Data>
export type Point = import('unist').Point
export type Content = import('mdast').Content
export type UnistLiteral = import('unist').Literal<string>
export type UnistParent = import('unist').Parent
export type UnistNode = import('unist').Node
export type MdastRoot = import('mdast').Root
export type MdastContent = import('mdast').Content
export type MdastNode = MdastRoot | MdastContent
export type VFile = import('vfile').VFile
export type Location = ReturnType<typeof location>
export type ParserInstance = {
parse(nodes: Node[]): Node
tokenizeSource(value: string): Node
tokenizeWhiteSpace(value: string): Node
tokenize(value: string): Node[]
parse(nodes: UnistNode[]): UnistNode
tokenizeSource(value: string): UnistNode
tokenizeWhiteSpace(value: string): UnistNode
tokenize(value: string): UnistNode[]
}
export type ParserConstructor = new () => ParserInstance
export type Options = {
ignore?: Array<string>
source?: Array<string>
ignore?: string[] | undefined
source?: string[] | undefined
}

@@ -33,0 +36,0 @@ export type Context = {

/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Point} Point
* @typedef {import('mdast').Content} Content
* @typedef {import('unist').Literal<string>} UnistLiteral
* @typedef {import('unist').Parent} UnistParent
* @typedef {import('unist').Node} UnistNode
* @typedef {import('mdast').Root} MdastRoot
* @typedef {import('mdast').Content} MdastContent
* @typedef {MdastRoot|MdastContent} MdastNode
* @typedef {import('vfile').VFile} VFile
* @typedef {ReturnType<import('vfile-location').location>} Location
* @typedef {{
* parse(nodes: Node[]): Node
* tokenizeSource(value: string): Node
* tokenizeWhiteSpace(value: string): Node
* tokenize(value: string): Node[]
* parse(nodes: UnistNode[]): UnistNode
* tokenizeSource(value: string): UnistNode
* tokenizeWhiteSpace(value: string): UnistNode
* tokenize(value: string): UnistNode[]
* }} ParserInstance

@@ -28,3 +31,2 @@ * @typedef {new () => ParserInstance} ParserConstructor

import repeat from 'repeat-string'
import {toString} from 'nlcst-to-string'

@@ -34,6 +36,9 @@ import {pointStart, pointEnd} from 'unist-util-position'

const defaultIgnore = ['table', 'tableRow', 'tableCell']
const defaultSource = ['inlineCode']
/**
* Transform a `tree` in mdast to nlcst.
*
* @param {Node} tree
* @param {MdastNode} tree
* @param {VFile} file

@@ -44,5 +49,2 @@ * @param {ParserInstance|ParserConstructor} Parser

export function toNlcst(tree, file, Parser, options = {}) {
/** @type {ParserInstance} */
var parser
// Crash on invalid parameters.

@@ -71,24 +73,22 @@ if (!tree || !tree.type) {

parser = 'parse' in Parser ? Parser : new Parser()
const parser = 'parse' in Parser ? Parser : new Parser()
const result = one(
{
doc: String(file),
place: location(file),
parser,
ignore: options.ignore
? defaultIgnore.concat(options.ignore)
: defaultIgnore,
source: options.source
? defaultSource.concat(options.source)
: defaultSource
},
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),
place: location(file),
parser,
ignore: [].concat(
'table',
'tableRow',
'tableCell',
options.ignore || []
),
source: [].concat('inlineCode', options.source || [])
},
// @ts-ignore assume mdast node.
tree
)
)
return parser.parse(result || [])
}

@@ -99,13 +99,10 @@

* @param {Context} config
* @param {Content} node
* @returns {Array.<Node>|undefined}
* @param {MdastNode} node
* @returns {Array.<UnistNode>|undefined}
*/
function one(config, node) {
/** @type {number} */
var start
const start = node.position ? node.position.start.offset : undefined
if (!config.ignore.includes(node.type)) {
start = node.position.start.offset
if (config.source.includes(node.type)) {
if (config.source.includes(node.type) && start && node.position) {
return patch(

@@ -123,8 +120,11 @@ config,

if ('children' in node) {
// @ts-ignore looks like a parent.
return all(config, node)
}
if (node.type === 'image' || node.type === 'imageReference') {
return patch(config, config.parser.tokenize(node.alt), start + 2)
if ((node.type === 'image' || node.type === 'imageReference') && node.alt) {
return patch(
config,
config.parser.tokenize(node.alt),
typeof start === 'number' ? start + 2 : undefined
)
}

@@ -145,45 +145,37 @@

* @param {Context} config
* @param {Parent} parent
* @returns {Array.<Node>}
* @param {UnistParent} parent
* @returns {Array.<UnistNode>}
*/
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
let index = -1
/** @type {Array.<UnistNode>} */
const results = []
/** @type {Point|undefined} */
let end
while (++index < parent.children.length) {
// @ts-ignore Assume `parent` is an mdast parent.
child = parent.children[index]
start = pointStart(child)
/** @type {MdastContent} */
// @ts-expect-error Assume `parent` is an mdast parent.
const child = parent.children[index]
const start = pointStart(child)
if (end && start.line !== end.line) {
lineEnding = config.parser.tokenizeWhiteSpace(
repeat('\n', start.line - end.line)
const lineEnding = config.parser.tokenizeWhiteSpace(
'\n'.repeat(start.line - end.line)
)
patch(config, [lineEnding], end.offset)
if (
'value' in lineEnding &&
typeof lineEnding.value === 'string' &&
lineEnding.value.length < 2
) {
if (literal(lineEnding) && lineEnding.value.length < 2) {
lineEnding.value = '\n\n'
}
result.push(lineEnding)
results.push(lineEnding)
}
result = result.concat(one(config, child) || [])
const result = one(config, child)
if (result) results.push(...result)
end = pointEnd(child)
}
return result
return results
}

@@ -195,30 +187,29 @@

*
* @template {Array.<Node>} T
* @template {Array.<UnistNode>} T
* @param {Context} config
* @param {T} nodes
* @param {number} offset
* @param {number|undefined} offset
* @returns {T}
*/
function patch(config, nodes, offset) {
var index = -1
var start = offset
/** @type {number} */
var end
/** @type {Node} */
var node
let index = -1
let start = offset
while (++index < nodes.length) {
node = nodes[index]
const node = nodes[index]
if ('children' in node) {
// @ts-ignore looks like a parent.
if (parent(node)) {
patch(config, node.children, start)
}
end = start + toString(node).length
const end =
typeof start === 'number' ? start + toString(node).length : undefined
node.position = {
start: config.place.toPoint(start),
end: config.place.toPoint(end)
}
node.position =
start !== undefined && end !== undefined
? {
start: config.place.toPoint(start),
end: config.place.toPoint(end)
}
: undefined

@@ -230,1 +221,17 @@ start = end

}
/**
* @param {UnistNode} node
* @returns {node is UnistLiteral}
*/
function literal(node) {
return 'value' in node
}
/**
* @param {UnistNode} node
* @returns {node is UnistParent}
*/
function parent(node) {
return 'children' in node
}
{
"name": "mdast-util-to-nlcst",
"version": "5.0.2",
"version": "5.1.0",
"description": "mdast utility to transform to nlcst",

@@ -38,6 +38,4 @@ "license": "MIT",

"@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": "^4.0.0",

@@ -65,3 +63,3 @@ "vfile": "^5.0.0",

"typescript": "^4.0.0",
"xo": "^0.39.0"
"xo": "^0.42.0"
},

@@ -85,7 +83,3 @@ "scripts": {

"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},

@@ -92,0 +86,0 @@ "remarkConfig": {

@@ -35,5 +35,5 @@ # mdast-util-to-nlcst

var file = new VFile('Some *foo*sball.')
var mdast = fromMarkdown(file)
var nlcst = toNlcst(mdast, file, ParseEnglish)
const file = new VFile('Some *foo*sball.')
const mdast = fromMarkdown(file)
const nlcst = toNlcst(mdast, file, ParseEnglish)

@@ -40,0 +40,0 @@ console.log(inspect(nlcst))

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc