kotmark

A Markdown engine
Kotlin Multiplatform

Guide / custom block parsing

Block continuation

BlockContinue makes a parser's line-consumption and indentation behavior explicit.

Continuation decisions

Return AtIndex for a UTF-16 input position, AtColumn for a visual column, Finished to consume the closing line, or null to let the enclosing parser handle it.

A parser with a closing line and indentation
import io.heapy.kotmark.node.CustomBlock
import io.heapy.kotmark.parser.block.AbstractBlockParser
import io.heapy.kotmark.parser.block.BlockContinue
import io.heapy.kotmark.parser.block.ParserState

class NoteBlock : CustomBlock()

class NoteBlockParser : AbstractBlockParser() {
    override val block = NoteBlock()

    override fun tryContinue(parserState: ParserState): BlockContinue? = when {
        parserState.line.content.toString() == "END" -> BlockContinue.Finished
        parserState.indent >= 4 -> BlockContinue.AtColumn(parserState.column + 4)
        parserState.isBlank -> BlockContinue.AtIndex(parserState.index)
        else -> null
    }
}

Edge cases

  • Return Finished when the current line closes the block; the enclosing parser consumes that line.
  • null is different from Finished: it gives the enclosing parser a chance to process the line.
  • Tabs affect visual columns, while AtIndex positions remain UTF-16 offsets.
  • Use the complete custom block guide on the home page for a BlockParserFactory and extension registration example.