Streaming Markdown Parser

0.0.86 · active · verified Wed Apr 22

stream-markdown-parser is a pure JavaScript Markdown parsing and rendering utility library designed with streaming capabilities and framework-agnosticism. Currently at version 0.0.86, it exhibits a rapid release cadence with frequent patch and nightly builds, indicating active development. The library provides core Markdown parsing logic, extracted from the `markstream-vue` project, enabling its use in any JavaScript or TypeScript project without direct Vue dependencies. Key differentiators include its lightweight footprint, extensibility via a plugin-based architecture, comprehensive TypeScript support for type safety, and optimized performance for progressive, stream-friendly parsing. It is built upon `markdown-it-ts`, a TypeScript-first distribution of `markdown-it`, maintaining API compatibility while offering richer token type definitions for developers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use stream-markdown-parser for both incremental (streaming) and static Markdown parsing, outputting a progressively built AST structure. It highlights reusing the parser instance for efficiency.

import { getMarkdown, parseMarkdownToStructure } from 'stream-markdown-parser';

// Create a markdown-it-ts instance with default plugins. Reusing this instance
// avoids redundant plugin registrations for performance.
const md = getMarkdown();

// Imagine receiving markdown content in chunks from a stream (e.g., SSE, AI response).
let bufferedMarkdown = '';

async function simulateStreamingParsing(chunks: string[]) {
  for (const chunk of chunks) {
    bufferedMarkdown += chunk;
    // Parse the accumulated buffer into an Abstract Syntax Tree (AST).
    // This AST is designed to be lightweight and suitable for progressive rendering.
    const nodes = parseMarkdownToStructure(bufferedMarkdown, md);
    console.log('--- Current AST ---');
    // In a real application, 'nodes' would be passed to a renderer (e.g., markstream-vue)
    // to update the UI progressively without re-parsing the entire content from scratch.
    console.log(JSON.stringify(nodes, null, 2).substring(0, 300) + '...');
    await new Promise(resolve => setTimeout(resolve, 50)); // Simulate delay
  }
  console.log('\n--- Final AST ---');
  const finalNodes = parseMarkdownToStructure(bufferedMarkdown, md);
  console.log(JSON.stringify(finalNodes, null, 2));
}

// Example usage: Simulate receiving markdown content in multiple parts.
const markdownChunks = [
  '# Hello ', 'World\n\nThis is a ', '**streaming** ', 'parser example.\n\n- Item 1\n- Item 2\n
```js
console.log("Hello");
```
'];
simulateStreamingParsing(markdownChunks);

// For basic, non-streaming parsing, you can directly render to HTML if needed:
const staticMarkdown = '## Static Content\n\nThis is _just_ a test.';
const staticNodes = parseMarkdownToStructure(staticMarkdown, md);
console.log('\n--- Static AST ---');
console.log(JSON.stringify(staticNodes, null, 2).substring(0, 300) + '...');
// If HTML output is still required, the underlying markdown-it-ts instance can render it.
// const htmlOutput = md.render?.(staticMarkdown); // Uncomment to see HTML output

view raw JSON →