BaseProcessor

Reference

BaseClass to use when creating processors.

A processor is an class that will be used by parsers to convert tokens into Nodes.

A recommendation when creating processors is to have one for each type Node that your language will have

Example

import { MarkdownNodeHeading } from './MarkdownNodeHeading'
import { MarkdownProcessor } from './MarkdownProcessor'

export class MarkdownProcessorHeading extends MarkdownProcessor {
    public order = 10

    public process() {
        // current token being processed
        const current = this.tokens[0]

        // verify is is a heading patter
        if (current.value !== '#') return false

        // look for the end of the line that is Token.type === TokenType.BreakLine
        const endLineIndex = this.findEndLineIndex()

        if (endLineIndex === -1) return false
        
        // get all of this line
        const tokens = this.tokens.slice(0, endLineIndex + 1)

        // mount the node
        const node = new MarkdownNodeHeading()

        node.tokens = tokens
        node.body = tokens.slice(1).toText().trim()
        node.level = tokens.filter((token) => token.value === '#').length

        // add the created node to the output and remove node tokens from the queue
        this.addNode(node)

        return true
    }
}