Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: syntax-tree/mdast-util-to-nlcst
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5.0.2
Choose a base ref
...
head repository: syntax-tree/mdast-util-to-nlcst
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Jun 1, 2021

  1. Copy the full SHA
    402d779 View commit details

Commits on Jul 26, 2021

  1. Copy the full SHA
    e69b3a8 View commit details
  2. Refactor code-style

    wooorm committed Jul 26, 2021
    Copy the full SHA
    433309d View commit details
  3. Add strict to tsconfig.json

    wooorm committed Jul 26, 2021
    Copy the full SHA
    6d1ca1e View commit details
  4. Remove dependency

    wooorm committed Jul 26, 2021
    Copy the full SHA
    f3f7db6 View commit details
  5. Copy the full SHA
    961dbbc View commit details
  6. Update xo

    wooorm committed Jul 26, 2021
    Copy the full SHA
    b826c51 View commit details
  7. 5.1.0

    wooorm committed Jul 26, 2021
    Copy the full SHA
    a7ef62c View commit details

Commits on Aug 27, 2021

  1. Update dev-dependencies

    wooorm committed Aug 27, 2021
    Copy the full SHA
    f69ae5c View commit details
  2. Use @types/nlcst

    wooorm committed Aug 27, 2021
    Copy the full SHA
    c1462b2 View commit details
  3. 5.2.0

    wooorm committed Aug 27, 2021
    Copy the full SHA
    248fa8f View commit details

Commits on Oct 31, 2021

  1. Update dev-dependencies

    wooorm committed Oct 31, 2021
    Copy the full SHA
    dcc2a14 View commit details
  2. Copy the full SHA
    6f5656c View commit details
  3. 5.2.1

    wooorm committed Oct 31, 2021
    Copy the full SHA
    bd1f454 View commit details

Commits on May 21, 2022

  1. Update dev-dependencies

    wooorm committed May 21, 2022
    Copy the full SHA
    d4a5c20 View commit details
  2. Refactor code-style

    wooorm committed May 21, 2022
    Copy the full SHA
    c20b853 View commit details
  3. Add improved docs

    wooorm committed May 21, 2022
    Copy the full SHA
    d9ce4df View commit details
  4. Refactor to move core to lib/

    wooorm committed May 21, 2022
    Copy the full SHA
    c802109 View commit details

Commits on Jan 25, 2023

  1. Update dev-dependencies

    wooorm committed Jan 25, 2023
    Copy the full SHA
    66a1962 View commit details
  2. Add support for new parse-latin, parse-english

    This is a breaking change: these parsers were updated,
    which cleans their API a lot, and adds types.
    wooorm committed Jan 25, 2023
    Copy the full SHA
    f947fba View commit details
  3. Update Actions

    wooorm committed Jan 25, 2023
    Copy the full SHA
    d352b5d View commit details
  4. Update tsconfig.json

    wooorm committed Jan 25, 2023
    Copy the full SHA
    ed61add View commit details
  5. Refactor code-style

    *   Add more docs to JSDoc
    *   Add support for `null` in input of API types
    wooorm committed Jan 25, 2023
    Copy the full SHA
    fdd46e8 View commit details
  6. Add ignore-scripts to .npmrc

    wooorm committed Jan 25, 2023
    Copy the full SHA
    3838dee View commit details
  7. Use Node test runner

    wooorm committed Jan 25, 2023
    Copy the full SHA
    912cdfc View commit details
  8. Copy the full SHA
    8000155 View commit details
  9. Add improved docs

    wooorm committed Jan 25, 2023
    Copy the full SHA
    9be776d View commit details
  10. Fix typo

    wooorm committed Jan 25, 2023
    Copy the full SHA
    c4a547e View commit details
  11. 6.0.0

    wooorm committed Jan 25, 2023
    Copy the full SHA
    3d42964 View commit details

Commits on Jul 8, 2023

  1. Update dev-dependencies

    wooorm committed Jul 8, 2023
    Copy the full SHA
    24f97ec View commit details
  2. Copy the full SHA
    c9b0466 View commit details
  3. Refactor code-style

    wooorm committed Jul 8, 2023
    Copy the full SHA
    cce7bcc View commit details
  4. Refactor docs

    wooorm committed Jul 8, 2023
    Copy the full SHA
    c61c7aa View commit details
  5. Change to use export map

    wooorm committed Jul 8, 2023
    Copy the full SHA
    2463986 View commit details
  6. Change to require Node.js 16

    wooorm committed Jul 8, 2023
    Copy the full SHA
    8462981 View commit details
  7. 7.0.0

    wooorm committed Jul 8, 2023
    Copy the full SHA
    e15e733 View commit details

Commits on Apr 30, 2024

  1. Update dev-dependencies

    wooorm committed Apr 30, 2024
    Copy the full SHA
    12de72b View commit details
  2. Copy the full SHA
    ed16a50 View commit details
  3. Add declaration maps

    wooorm committed Apr 30, 2024
    Copy the full SHA
    7531065 View commit details
  4. 7.0.1

    wooorm committed Apr 30, 2024
    Copy the full SHA
    e05a1ed View commit details
Showing with 863 additions and 468 deletions.
  1. +1 −1 .github/workflows/bb.yml
  2. +5 −5 .github/workflows/main.yml
  3. +1 −0 .gitignore
  4. +1 −0 .npmrc
  5. +4 −218 index.js
  6. +369 −0 lib/index.js
  7. +47 −35 package.json
  8. +185 −68 readme.md
  9. +238 −132 test/index.js
  10. +12 −9 tsconfig.json
2 changes: 1 addition & 1 deletion .github/workflows/bb.yml
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ name: bb
on:
issues:
types: [opened, reopened, edited, closed, labeled, unlabeled]
pull_request:
pull_request_target:
types: [opened, reopened, edited, closed, labeled, unlabeled]
jobs:
main:
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -7,15 +7,15 @@ jobs:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dcodeIO/setup-node-nvm@master
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v4
strategy:
matrix:
node:
- lts/erbium
- node
- lts/gallium
- lts/iron
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts.map
*.d.ts
*.log
coverage/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
ignore-scripts=true
222 changes: 4 additions & 218 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,221 +1,7 @@
/**
* @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 {ReturnType<import('vfile-location').location>} Location
* @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 {Location} place
* @property {ParserInstance} parser
* @property {Array.<string>} ignore
* @property {Array.<string>} source
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').ParserInstance} ParserInstance
* @typedef {import('./lib/index.js').ParserConstructor} ParserConstructor
*/

import repeat from 'repeat-string'
import {toString} from 'nlcst-to-string'
import {pointStart, pointEnd} from 'unist-util-position'
import {location} from 'vfile-location'

/**
* 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

// Crash on invalid parameters.
if (!tree || !tree.type) {
throw new Error('mdast-util-to-nlcst expected node')
}

if (!file || !file.messages) {
throw new Error('mdast-util-to-nlcst expected file')
}

// Construct parser.
if (!Parser) {
throw new Error('mdast-util-to-nlcst expected parser')
}

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')
}

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),
place: location(file),
parser,
ignore: [].concat(
'table',
'tableRow',
'tableCell',
options.ignore || []
),
source: [].concat('inlineCode', options.source || [])
},
// @ts-ignore assume mdast node.
tree
)
)
}

/**
* Transform a single node.
* @param {Context} config
* @param {Content} node
* @returns {Array.<Node>|undefined}
*/
function one(config, node) {
/** @type {number} */
var start

if (!config.ignore.includes(node.type)) {
start = node.position.start.offset

if (config.source.includes(node.type)) {
return patch(
config,
[
config.parser.tokenizeSource(
config.doc.slice(start, node.position.end.offset)
)
],
start
)
}

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 === 'break') {
return patch(config, [config.parser.tokenizeWhiteSpace('\n')], start)
}

if (node.type === 'text') {
return patch(config, config.parser.tokenize(node.value), start)
}
}
}

/**
* 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 = pointStart(child)

if (end && start.line !== end.line) {
lineEnding = config.parser.tokenizeWhiteSpace(
repeat('\n', start.line - end.line)
)
patch(config, [lineEnding], end.offset)

if (
'value' in lineEnding &&
typeof lineEnding.value === 'string' &&
lineEnding.value.length < 2
) {
lineEnding.value = '\n\n'
}

result.push(lineEnding)
}

result = result.concat(one(config, child) || [])
end = pointEnd(child)
}

return result
}

/**
* 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

while (++index < nodes.length) {
node = nodes[index]

if ('children' in node) {
// @ts-ignore looks like a parent.
patch(config, node.children, start)
}

end = start + toString(node).length

node.position = {
start: config.place.toPoint(start),
end: config.place.toPoint(end)
}

start = end
}

return nodes
}
export {toNlcst} from './lib/index.js'
Loading