@incremark/core
Framework-agnostic core parser library.
Installation
pnpm add @incremark/coreIncremarkParser
Incremental Markdown parser class.
Constructor
new IncremarkParser(options?: ParserOptions)Methods
append(chunk)
Append content and return incremental update.
append(chunk: string): IncrementalUpdateParameters:
chunk- Text fragment to append
Returns:
IncrementalUpdate- Block information for this update
finalize()
Mark parsing as complete, process remaining pending content.
finalize(): IncrementalUpdatereset()
Reset parser state.
reset(): voidrender(content)
One-shot render complete Markdown (reset + append + finalize).
render(content: string): IncrementalUpdateParameters:
content- Complete Markdown content
Returns:
IncrementalUpdate- Parse result
getBuffer()
Get current buffer content.
getBuffer(): stringgetCompletedBlocks()
Get all completed blocks.
getCompletedBlocks(): ParsedBlock[]getAst()
Get current complete AST.
getAst(): RootsetOnChange(callback)
Set state change callback (for DevTools).
setOnChange(callback: ((state: ParserState) => void) | undefined): voidcreateIncremarkParser
Factory function to create parser instance.
function createIncremarkParser(options?: ParserOptions): IncremarkParserBlockTransformer
Typewriter effect controller, acting as middleware between parser and renderer.
Constructor
new BlockTransformer(options?: TransformerOptions)Methods
push(blocks)
Push source blocks to queue.
push(blocks: SourceBlock[]): voidupdate(block)
Update currently displaying block content (for growing pending blocks).
update(block: SourceBlock): voidskip()
Skip all animations, display all content immediately.
skip(): voidreset()
Reset state.
reset(): voiddestroy()
Destroy transformer, clean up timers.
destroy(): voidisProcessing()
Check if currently processing.
isProcessing(): booleansetOptions(options)
Dynamically update configuration.
setOptions(options: { charsPerTick?: number; tickInterval?: number }): voidcreateBlockTransformer
Factory function to create BlockTransformer instance.
function createBlockTransformer(options?: TransformerOptions): BlockTransformerPlugin System
BlockTransformer supports plugins to handle special node types.
Built-in Plugins
| Plugin | Description | Behavior |
|---|---|---|
imagePlugin | Images | Display immediately (0 chars) |
thematicBreakPlugin | Thematic breaks | Display immediately (0 chars) |
codeBlockPlugin | Code blocks | Display as whole (1 char) |
mermaidPlugin | Mermaid diagrams | Display as whole (1 char) |
mathPlugin | Math formulas | Display as whole (1 char) |
Plugin Collections
// Default plugins: images and breaks display immediately, others participate in typewriter
import { defaultPlugins } from '@incremark/core'
// All plugins: code blocks, mermaid, math also display as whole
import { allPlugins } from '@incremark/core'Custom Plugins
import { createPlugin } from '@incremark/core'
const myPlugin = createPlugin(
'my-plugin',
(node) => node.type === 'myType',
{
countChars: (node) => 1,
sliceNode: (node, displayed, total) => node
}
)TransformerPlugin Interface
interface TransformerPlugin {
name: string
match: (node: RootContent) => boolean
countChars?: (node: RootContent) => number
sliceNode?: (node: RootContent, displayedChars: number, totalChars: number) => RootContent | null
}Utility Functions
countChars(node)
Count displayable characters in a node.
function countChars(node: RootContent): numbersliceAst(node, chars)
Slice node by character count.
function sliceAst(node: RootContent, chars: number): RootContent | nullcloneNode(node)
Deep clone a node.
function cloneNode<T>(node: T): TType Definitions
ParserOptions
interface ParserOptions {
/** Enable GFM extension */
gfm?: boolean
/** Enable ::: container syntax */
containers?: boolean | ContainerConfig
/** Custom block boundary detection function */
blockBoundaryDetector?: (content: string, position: number) => boolean
/** micromark extensions */
extensions?: Extension[]
/** mdast extensions */
mdastExtensions?: Extension[]
/** State change callback */
onChange?: (state: ParserState) => void
}TransformerOptions
interface TransformerOptions {
/** Characters per tick (default: 2) */
charsPerTick?: number
/** Tick interval in ms (default: 50) */
tickInterval?: number
/** Plugin list */
plugins?: TransformerPlugin[]
/** State change callback */
onChange?: (blocks: DisplayBlock[]) => void
}ParsedBlock
interface ParsedBlock {
/** Unique block ID */
id: string
/** Block status */
status: BlockStatus
/** AST node */
node: RootContent
/** Raw text start offset */
startOffset: number
/** Raw text end offset */
endOffset: number
/** Raw text content */
rawText: string
}SourceBlock
interface SourceBlock<T = unknown> {
id: string
node: RootContent
status: 'pending' | 'stable' | 'completed'
meta?: T
}DisplayBlock
interface DisplayBlock<T = unknown> {
id: string
sourceNode: RootContent
displayNode: RootContent
displayedChars: number
totalChars: number
isDisplayComplete: boolean
meta?: T
}BlockStatus
type BlockStatus = 'pending' | 'stable' | 'completed'IncrementalUpdate
interface IncrementalUpdate {
/** Newly completed blocks */
completed: ParsedBlock[]
/** Updated blocks */
updated: ParsedBlock[]
/** Pending blocks */
pending: ParsedBlock[]
/** Complete AST */
ast: Root
}Detector Functions
detectFenceStart
Detect code block start.
function detectFenceStart(line: string): { char: string; length: number; indent: number } | nulldetectFenceEnd
Detect code block end.
function detectFenceEnd(line: string, fenceChar: string, fenceLength: number): booleanisEmptyLine
Detect empty line.
function isEmptyLine(line: string): booleanisHeading
Detect heading line.
function isHeading(line: string): booleanisThematicBreak
Detect thematic break.
function isThematicBreak(line: string): booleanisListItemStart
Detect list item start.
function isListItemStart(line: string): booleanisBlockquoteStart
Detect blockquote start.
function isBlockquoteStart(line: string): boolean