Custom Nodes

You can make your own processors and use together with the default processors to customize the output nodes

Check BaseProcessor to know more details about processors

Node Class

First you need to create your node class extending the base MarkdownNode

import { MarkdownNode } from '@language-kit/markdown'

export class CustomNode extends MarkdownNode {
    public type = "custom"
}

Processor

Then you need to create a processor that extends the MarkdownProcessor class and create the logic that process Tokens into Nodes

import { MarkdownParser, Processors, MarkdownProcessor } from '@language-kit/markdown'
import { CustomNode } from "./custom-node"

class CustomProcessor extends MarkdownProcessor {
    public order = 1
    public process() {
        const tokens = this.tokens.slice(0, 4)

        // all Hello word phase will be converted in a custom node
        if (tokens.toText() !== 'Hello Word\n') {
            return
        }
        
        const node = new CustomNode()

        node.tokens = tokens

        this.addNode(node)

        return true
    }
}

Parser

Finally you just need pass the custom processor to the MarkdownParser class and the output will be what we defined.

import { MarkdownParser, Processors } from '@language-kit/markdown'
import { CustomProcessor }  from "./custom-processor"

const MyProcessors = [Processors.Paragraph, CustomProcessor]

const parser = new MarkdownParser(MyProcessors)

const payload = ['Hello Word', 'Normal paragraph'].join('\n')

const [first, second] = parser.toNodes(payload)

first // CustomNode
second // MarkdownNodeParagraph
Table of Contents