NodeArray

Reference

This is a utility class to help manage the nodes outputted by the parser

Extends

You can create an extended version of this class to help you manipulate better your nodes.

Check the markdown language example below.

// packages/markdown/src/MarkdownNodeArray.ts
import { NodeArray } from '@language-kit/core'
import { MarkdownNode } from './MarkdownNode'

export class MarkdownNodeArray extends NodeArray<MarkdownNode> {
    public toHtml() {
        return this.map((node) => node.toHtml()).join('')
    }
}
// packages/markdown/src/MarkdownParser.ts
...
import { MarkdownNodeArray } from './MarkdownNodeArray'

export class MarkdownParser extends BaseParser<MarkdownNode> {
    ...

    public toNodes(payload: string, options?: ToNodeOptions) {
        const nodes = super.toNodes(payload, options)

        return new MarkdownNodeArray(...nodes)
    }
}

It adds an toHtml() to the array of nodes an overwrite the output of the main parser.

This way we have a very useful and easy way to convert all nodes in pure html, and also very customizable by individual nodes too.

const nodes = parser.toNodes("# Heading\n **bold text**")

const html = nodes.toHtml() // <h1>Heading</h1><strong>bold text<strong>

Methods

setPositions()

Update nodes & tokens positions using BaseNode.setPositions()

const nodes = new NodeArray([NodeOne, NodeTwo])

nodes.setPositions()

toText

Convert the array of nodes into text using BaseNode.toText()

const nodes = parser.toNodes('Hello')

nodes.toText() // Hello word