{"id":16909,"library":"stream-markdown-parser","title":"Streaming Markdown Parser","description":"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.","status":"active","version":"0.0.86","language":"javascript","source_language":"en","source_url":"https://github.com/Simon-He95/markstream-vue","tags":["javascript","markdown","markdown-it","markdown-it-ts","parser","markdown-parser","markdown-renderer","ast","markdown-ast","typescript"],"install":[{"cmd":"npm install stream-markdown-parser","lang":"bash","label":"npm"},{"cmd":"yarn add stream-markdown-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add stream-markdown-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses named ESM exports. CommonJS `require` is not officially supported and may lead to issues.","wrong":"const getMarkdown = require('stream-markdown-parser').getMarkdown","symbol":"getMarkdown","correct":"import { getMarkdown } from 'stream-markdown-parser'"},{"note":"Ensure to use named imports from the main package entry point. Direct imports from internal paths are discouraged and unstable.","wrong":"import parseMarkdownToStructure from 'stream-markdown-parser/dist/parseMarkdownToStructure'","symbol":"parseMarkdownToStructure","correct":"import { parseMarkdownToStructure } from 'stream-markdown-parser'"},{"note":"Configuration utilities like `setDefaultMathOptions` are also exposed as named exports from the primary package.","wrong":"import { setDefaultMathOptions } from 'stream-markdown-parser/math'","symbol":"setDefaultMathOptions","correct":"import { setDefaultMathOptions } from 'stream-markdown-parser'"}],"quickstart":{"code":"import { getMarkdown, parseMarkdownToStructure } from 'stream-markdown-parser';\n\n// Create a markdown-it-ts instance with default plugins. Reusing this instance\n// avoids redundant plugin registrations for performance.\nconst md = getMarkdown();\n\n// Imagine receiving markdown content in chunks from a stream (e.g., SSE, AI response).\nlet bufferedMarkdown = '';\n\nasync function simulateStreamingParsing(chunks: string[]) {\n  for (const chunk of chunks) {\n    bufferedMarkdown += chunk;\n    // Parse the accumulated buffer into an Abstract Syntax Tree (AST).\n    // This AST is designed to be lightweight and suitable for progressive rendering.\n    const nodes = parseMarkdownToStructure(bufferedMarkdown, md);\n    console.log('--- Current AST ---');\n    // In a real application, 'nodes' would be passed to a renderer (e.g., markstream-vue)\n    // to update the UI progressively without re-parsing the entire content from scratch.\n    console.log(JSON.stringify(nodes, null, 2).substring(0, 300) + '...');\n    await new Promise(resolve => setTimeout(resolve, 50)); // Simulate delay\n  }\n  console.log('\\n--- Final AST ---');\n  const finalNodes = parseMarkdownToStructure(bufferedMarkdown, md);\n  console.log(JSON.stringify(finalNodes, null, 2));\n}\n\n// Example usage: Simulate receiving markdown content in multiple parts.\nconst markdownChunks = [\n  '# Hello ', 'World\\n\\nThis is a ', '**streaming** ', 'parser example.\\n\\n- Item 1\\n- Item 2\\n\n```js\nconsole.log(\"Hello\");\n```\n'];\nsimulateStreamingParsing(markdownChunks);\n\n// For basic, non-streaming parsing, you can directly render to HTML if needed:\nconst staticMarkdown = '## Static Content\\n\\nThis is _just_ a test.';\nconst staticNodes = parseMarkdownToStructure(staticMarkdown, md);\nconsole.log('\\n--- Static AST ---');\nconsole.log(JSON.stringify(staticNodes, null, 2).substring(0, 300) + '...');\n// If HTML output is still required, the underlying markdown-it-ts instance can render it.\n// const htmlOutput = md.render?.(staticMarkdown); // Uncomment to see HTML output\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Regularly review the GitHub repository's changelog and release notes for updates, especially when upgrading to new versions. Consider pinning to exact versions in production environments.","message":"The package version is currently in 0.0.x, indicating an early development stage. While highly functional, the API surface may still be subject to breaking changes without adhering strictly to semantic versioning (e.g., a minor version bump could introduce breaking changes).","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Refer to the main `markstream-vue` GitHub repository for the most comprehensive context on development, issues, and future plans, as this package's lifecycle is intertwined with it.","message":"This package is part of a larger monorepository (markstream-vue) and extracts its core parsing logic. While framework-agnostic, its development roadmap and bug fixes might be influenced by the needs of the companion `markstream-vue` and `markstream-react` libraries.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Understand that `parseMarkdownToStructure` returns an AST for progressive rendering, not HTML. If you need HTML, ensure your workflow explicitly uses the `render` method provided by the `markdown-it-ts` instance returned by `getMarkdown()`.","message":"While built on `markdown-it-ts` for API compatibility, `stream-markdown-parser` processes tokens into its own stream-friendly AST (`ParsedNode[]`) rather than directly producing HTML strings. If direct HTML rendering is required, you must explicitly call `md.render()` on the underlying `markdown-it-ts` instance.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you are using standard ESM named imports: `import { getMarkdown } from 'stream-markdown-parser'` and that your environment supports ESM (e.g., Node.js with type:module or a bundler like Webpack/Vite).","cause":"This usually happens when attempting to use CommonJS `require()` syntax or an incorrect named import with a modern ESM-only package.","error":"TypeError: getMarkdown is not a function"},{"fix":"First, ensure the package is installed: `npm install stream-markdown-parser` or `pnpm add stream-markdown-parser`. If the issue persists, check your bundler configuration (e.g., Vite alias, Webpack resolve paths) to ensure it can correctly locate `node_modules`.","cause":"The package is not installed or the path is incorrect for your module resolver.","error":"Module not found: Can't resolve 'stream-markdown-parser'"}],"ecosystem":"npm","meta_description":null}