How simon-he95/markdown-it-ts works
1. Project shape
This repository is a TypeScript-first reimplementation of markdown-it. Its core product is a Markdown parser plus HTML renderer, but the codebase is split into separately importable packages/entrypoints so consumers can tree-shake parse-only, render-only, or streaming features.
2. Top-level runtime boundaries
The public API is centered in src/index.ts, which composes the parser, renderer, presets, plugins, and stream utilities. The package also exposes subpath imports for lower-level building blocks such as src/render/renderer.ts, src/stream/*, and src/plugins/with-renderer.ts.
3. Parsing pipeline
The parser is organized as a markdown-it-style ruler system. src/parse/ contains the core parse orchestration, while src/rules/block, src/rules/inline, and src/rules/core implement the actual rule sets. The flow is: source text → block parsing → inline parsing → core post-processing → token stream.
4. Token and state model
Shared data structures live in src/common/, src/helpers/, and src/types/. The parser operates on token objects and mutable state objects (StateBlock, StateInline, and parser state wrappers) so rules can annotate ranges, nesting, and derived metadata in a way that stays compatible with markdown-it plugins.
5. Renderer subsystem
Rendering is a separate subsystem under src/render/. It turns tokens into HTML and supports the same plugin-compatible renderer surface as markdown-it. The repo also supports async rendering paths, which matters for use cases like async syntax highlighting or post-processing rules.
6. Presets and compatibility modes
src/presets/ defines ready-made configurations such as default, commonmark, and zero. These presets control which rules are enabled and are important because the architecture preserves markdown-it’s behavior while allowing slimmer or stricter profiles.
7. Plugin architecture
Plugins are first-class and are expected to mutate the parser/renderer instance via rulers and renderer hooks. src/plugins/with-renderer.ts is the key bridge for consumers that start from parse-only mode and later attach renderer capabilities without changing the rest of the pipeline.
8. Streaming and incremental parsing
A major differentiator is the streaming layer in src/stream/ and src/streaming/. It adds append-oriented and chunk-aware parsing strategies, including bounded buffers, debounced/throttled stream parsers, piece-table-backed editable buffers, and fallback strategies for large or non-append edits. This is the main architectural extension beyond vanilla markdown-it.
9. Large-document strategy selection
The parser can automatically switch strategies for large inputs. Supporting code in src/support/ and src/parse/strategy_diagnostics.ts helps choose between full parse, chunked fallback, or incremental stream modes. This means the system is optimized not just for correctness, but for different input shapes and editing workloads.
10. Test architecture
The test suite is organized by concern: core rules, parse behavior, streaming behavior, renderer output, plugin interop, and compatibility with upstream markdown-it fixtures. There are also integration tests for real-world editor-style flows and compatibility parity checks against original markdown-it token output.
11. Benchmarking and regression control
The benchmark/ and scripts/perf-* tooling form a performance harness around the parser/renderer. It compares multiple implementations and strategies, generates CSV/JSON/Markdown reports in docs/, and archives historical snapshots. Performance is therefore treated as a first-class architectural constraint, not an afterthought.
12. Documentation, reports, and migration tracking
The docs/ tree is unusually central: it contains architecture notes, migration guides, compatibility reports, stream optimization guidance, and performance history. Root-level MIGRATION_STATUS.md and COMPATIBILITY_REPORT.md reinforce that this repo is both a library and an ongoing compatibility/migration project.
13. Developer demo and docs site
The editor/ folder is a small Vue/Vite demo application used to showcase the parser in an interactive editor-like UI. The support/ scripts build the docs site and demo artifacts, which are then deployed through GitHub Actions and optionally published to Netlify.
14. CI and external deployment
GitHub Actions workflows handle build verification, docs generation, benchmark generation, and optional Netlify deploys for both docs and demo sites. This means the repository’s operational boundary includes CI-generated artifacts and hosted documentation/demo infrastructure, not just the library package itself.
Main entry points: parse/index.ts Parse core · src/index.ts API entry · parser.ts Stream parser · default.ts Presets
Components 20
Source
Main library implementation and public entrypoints.
- API entry · Top-level composition of parser, renderer, presets, plugins, and stream utilities.
src/index.ts - Parse core · Coordinates the markdown-it-style parsing pipeline and parser state flow.
src/parse/index.ts - Block rules · Block-level tokenization rules for structure such as lists, quotes, code, tables, and headings.
src/rules/block/index.ts - Inline rules · Inline tokenization rules for emphasis, links, code spans, entities, escapes, and text.
src/rules/inline/index.ts - Core rules · Core passes that normalize, enrich, and finalize the token stream after block and inline parsing.
src/rules/core/ruler.ts - Renderer · Turns tokens into HTML and exposes renderer hooks compatible with markdown-it plugins.
src/render/renderer.ts - Presets · Ready-made parser configurations such as default, commonmark, and zero.
src/presets/default.ts - Plugin bridge · Attaches renderer capabilities to parse-first instances and preserves plugin compatibility.
src/plugins/with-renderer.ts - Common model · Shared tokens, HTML helpers, and utility functions used across parse and render.
src/common/token.ts - Parse state · Mutable parser state wrappers and source abstractions used by parsing rules.
src/parse/state.ts - Strategy select · Chooses between full parse, chunked fallback, and incremental stream modes for different inputs.
src/parse/strategy_diagnostics.ts - Chunk tuning · Recommendations and tuning helpers for chunk sizing and strategy selection.
src/support/chunk_recommend.ts
Streaming
Append-oriented parsing, chunking, and editable-buffer strategies.
- Stream parser · Append-oriented and chunk-aware parsing layer with bounded buffers and editable-buffer support.
src/stream/parser.ts - Streaming API · Higher-level streaming parser surface built on top of the stream layer.
src/streaming/stream_parser.ts - Buffers · Chunked, debounced, unbounded, and piece-table-backed buffer implementations.
src/stream/buffer.ts - Editable buffer · Piece-table and edit-aware buffer support for non-append workflows.
src/stream/editable.ts
Ops
Documentation, benchmarks, CI, and demo tooling.
- Bench harness · Implementation comparisons, regression checks, and report generation around parser and renderer performance.
benchmark/stream-parse-bench.ts - Docs + reports · Architecture notes, migration docs, compatibility reports, performance history, and generated summaries.
docs/architecture.md - Demo editor · Interactive Vite/Vue demo for showcasing the parser in an editor-like workflow.
editor/src/App.vue - CI deploy · GitHub Actions for build verification, docs generation, benchmark generation, and optional Netlify deployment.
.github/workflows/ci.yml
Connections 28
- API entry → Parse core: uses · Public API composes the parser pipeline.
- API entry → Renderer: uses · Public API exposes HTML rendering.
- API entry → Presets: exports · Entry point makes presets available.
- API entry → Plugin bridge: exports · Entry point exposes plugin bridge helpers.
- API entry → Streaming API: exports · Entry point exposes streaming surface.
- Parse core → Block rules: runs · Parser starts with block tokenization.
- Parse core → Inline rules: runs · Parser delegates inline tokenization.
- Parse core → Core rules: finalizes · Core passes post-process the token stream.
- Parse core → Parse state: mutates · Parsing rules operate on shared state objects.
- Parse core → Common model: shares · Parser relies on shared token and utility types.
- Renderer → Common model: reads · Renderer consumes token structures and helpers.
- Plugin bridge → Renderer: bridges · Attaches renderer hooks to parse-first instances.
- Presets → Block rules: configures · Presets enable or disable block rules.
- Presets → Inline rules: configures · Presets enable or disable inline rules.
- Presets → Core rules: configures · Presets control core post-processing behavior.
- Strategy select → Parse core: chooses · Strategy logic decides whether to run full or fallback parse modes.
- Strategy select → Stream parser: chooses · Strategy logic can route inputs into streaming paths.
- Stream parser → Buffers: uses · Stream parser depends on buffer implementations.
- Stream parser → Editable buffer: uses · Stream parser supports editable, piece-table-backed flows.
- Streaming API → Stream parser: wraps · Higher-level streaming API sits on the lower-level stream parser.
- Chunk tuning → Strategy select: advises · Chunk recommendations feed strategy selection.
- Bench harness → Parse core: measures · Benchmarks compare parser strategies and implementations.
- Bench harness → Stream parser: measures · Benchmarks cover streaming and incremental behavior.
- Docs + reports → Strategy select: documents · Docs explain parse strategy selection and tuning.
- Docs + reports → Bench harness: tracks · Docs include reports and performance history.
- Demo editor → API entry: consumes · Demo app uses the public library API.
- CI deploy → Docs + reports: generates · Workflows build and publish docs and reports.
- CI deploy → Bench harness: runs · Workflows run performance and regression jobs.