# simon-he95/markdown-it-ts architecture

> Architecture diagram of the GitHub repository simon-he95/markdown-it-ts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/simon-he95/markdown-it-ts
- Repository: https://github.com/simon-he95/markdown-it-ts

## Overview

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: [`src/parse/index.ts`](https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/index.ts) (Parse core), [`src/index.ts`](https://github.com/simon-he95/markdown-it-ts/blob/main/src/index.ts) (API entry), [`src/stream/parser.ts`](https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/parser.ts) (Stream parser), [`src/presets/default.ts`](https://github.com/simon-he95/markdown-it-ts/blob/main/src/presets/default.ts) (Presets)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_src["Source"]
  node_src_index["API entry<br/>public entrypoint<br/>[index.ts]"]
  node_src_parse["Parse core<br/>parse orchestration<br/>[index.ts]"]
  node_src_block_rules["Block rules<br/>block rule set<br/>[index.ts]"]
  node_src_inline_rules["Inline rules<br/>inline rule set<br/>[index.ts]"]
  node_src_core_rules["Core rules<br/>post-process rules<br/>[ruler.ts]"]
  node_src_renderer["Renderer<br/>[renderer.ts]"]
  node_src_presets["Presets<br/>[default.ts]"]
  node_src_plugins["Plugin bridge<br/>plugin adapter<br/>[with-renderer.ts]"]
  node_src_common["Common model<br/>shared types<br/>[token.ts]"]
  node_src_state["Parse state<br/>state model<br/>[state.ts]"]
  node_src_strategy{{"Strategy select<br/>heuristics"}}
  node_src_support{{"Chunk tuning<br/>heuristics support<br/>[chunk_recommend.ts]"}}
end

subgraph group_group_stream["Streaming"]
  node_src_stream["Stream parser<br/>incremental parsing<br/>[parser.ts]"]
  node_src_streaming["Streaming API<br/>stream facade<br/>[stream_parser.ts]"]
  node_src_stream_buffers[("Buffers<br/>buffer strategies<br/>[buffer.ts]")]
  node_src_stream_editable["Editable buffer<br/>editable text core<br/>[editable.ts]"]
end

subgraph group_group_docs["Ops"]
  node_benchmark_ops["Bench harness<br/>performance suite"]
  node_docs_ops["Docs + reports<br/>docs and tracking<br/>[architecture.md]"]
  node_editor_demo["Demo editor<br/>vue demo app<br/>[App.vue]"]
  node_ci_deploy["CI deploy<br/>workflows<br/>[ci.yml]"]
end

node_src_index -->|"uses"| node_src_parse
node_src_index -->|"uses"| node_src_renderer
node_src_index -->|"exports"| node_src_presets
node_src_index -->|"exports"| node_src_plugins
node_src_index -->|"exports"| node_src_streaming
node_src_parse -->|"runs"| node_src_block_rules
node_src_parse -->|"runs"| node_src_inline_rules
node_src_parse -->|"finalizes"| node_src_core_rules
node_src_parse -->|"mutates"| node_src_state
node_src_parse -->|"shares"| node_src_common
node_src_renderer -->|"reads"| node_src_common
node_src_plugins -->|"bridges"| node_src_renderer
node_src_presets -->|"configures"| node_src_block_rules
node_src_presets -->|"configures"| node_src_inline_rules
node_src_presets -->|"configures"| node_src_core_rules
node_src_strategy -->|"chooses"| node_src_parse
node_src_strategy -->|"chooses"| node_src_stream
node_src_stream -->|"uses"| node_src_stream_buffers
node_src_stream -->|"uses"| node_src_stream_editable
node_src_streaming -->|"wraps"| node_src_stream
node_src_support -->|"advises"| node_src_strategy
node_benchmark_ops -->|"measures"| node_src_parse
node_benchmark_ops -->|"measures"| node_src_stream
node_docs_ops -->|"documents"| node_src_strategy
node_docs_ops -->|"tracks"| node_benchmark_ops
node_editor_demo -->|"consumes"| node_src_index
node_ci_deploy -->|"generates"| node_docs_ops
node_ci_deploy -->|"runs"| node_benchmark_ops

click node_src_index "https://github.com/simon-he95/markdown-it-ts/blob/main/src/index.ts"
click node_src_parse "https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/index.ts"
click node_src_block_rules "https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/block/index.ts"
click node_src_inline_rules "https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/inline/index.ts"
click node_src_core_rules "https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/core/ruler.ts"
click node_src_renderer "https://github.com/simon-he95/markdown-it-ts/blob/main/src/render/renderer.ts"
click node_src_presets "https://github.com/simon-he95/markdown-it-ts/blob/main/src/presets/default.ts"
click node_src_plugins "https://github.com/simon-he95/markdown-it-ts/blob/main/src/plugins/with-renderer.ts"
click node_src_common "https://github.com/simon-he95/markdown-it-ts/blob/main/src/common/token.ts"
click node_src_state "https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/state.ts"
click node_src_strategy "https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/strategy_diagnostics.ts"
click node_src_stream "https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/parser.ts"
click node_src_streaming "https://github.com/simon-he95/markdown-it-ts/blob/main/src/streaming/stream_parser.ts"
click node_src_stream_buffers "https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/buffer.ts"
click node_src_stream_editable "https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/editable.ts"
click node_src_support "https://github.com/simon-he95/markdown-it-ts/blob/main/src/support/chunk_recommend.ts"
click node_benchmark_ops "https://github.com/simon-he95/markdown-it-ts/blob/main/benchmark/stream-parse-bench.ts"
click node_docs_ops "https://github.com/simon-he95/markdown-it-ts/blob/main/docs/architecture.md"
click node_editor_demo "https://github.com/simon-he95/markdown-it-ts/blob/main/editor/src/App.vue"
click node_ci_deploy "https://github.com/simon-he95/markdown-it-ts/blob/main/.github/workflows/ci.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_src_parse,node_src_block_rules,node_src_inline_rules,node_src_core_rules,node_src_renderer,node_src_presets,node_src_plugins,node_src_common,node_src_state,node_src_strategy,node_src_support toneBlue
class node_src_stream,node_src_streaming,node_src_stream_buffers,node_src_stream_editable toneAmber
class node_benchmark_ops,node_docs_ops,node_editor_demo,node_ci_deploy toneMint
```

## Components

### Source

Main library implementation and public entrypoints.

- [API entry](https://github.com/simon-he95/markdown-it-ts/blob/main/src/index.ts): public entrypoint · Top-level composition of parser, renderer, presets, plugins, and stream utilities. · `src/index.ts`
- [Parse core](https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/index.ts): parse orchestration · Coordinates the markdown-it-style parsing pipeline and parser state flow. · `src/parse/index.ts`
- [Block rules](https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/block/index.ts): block rule set · Block-level tokenization rules for structure such as lists, quotes, code, tables, and headings. · `src/rules/block/index.ts`
- [Inline rules](https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/inline/index.ts): inline rule set · Inline tokenization rules for emphasis, links, code spans, entities, escapes, and text. · `src/rules/inline/index.ts`
- [Core rules](https://github.com/simon-he95/markdown-it-ts/blob/main/src/rules/core/ruler.ts): post-process rules · Core passes that normalize, enrich, and finalize the token stream after block and inline parsing. · `src/rules/core/ruler.ts`
- [Renderer](https://github.com/simon-he95/markdown-it-ts/blob/main/src/render/renderer.ts): html renderer · Turns tokens into HTML and exposes renderer hooks compatible with markdown-it plugins. · `src/render/renderer.ts`
- [Presets](https://github.com/simon-he95/markdown-it-ts/blob/main/src/presets/default.ts): config presets · Ready-made parser configurations such as default, commonmark, and zero. · `src/presets/default.ts`
- [Plugin bridge](https://github.com/simon-he95/markdown-it-ts/blob/main/src/plugins/with-renderer.ts): plugin adapter · Attaches renderer capabilities to parse-first instances and preserves plugin compatibility. · `src/plugins/with-renderer.ts`
- [Common model](https://github.com/simon-he95/markdown-it-ts/blob/main/src/common/token.ts): shared types · Shared tokens, HTML helpers, and utility functions used across parse and render. · `src/common/token.ts`
- [Parse state](https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/state.ts): state model · Mutable parser state wrappers and source abstractions used by parsing rules. · `src/parse/state.ts`
- [Strategy select](https://github.com/simon-he95/markdown-it-ts/blob/main/src/parse/strategy_diagnostics.ts): heuristics · Chooses between full parse, chunked fallback, and incremental stream modes for different inputs. · `src/parse/strategy_diagnostics.ts`
- [Chunk tuning](https://github.com/simon-he95/markdown-it-ts/blob/main/src/support/chunk_recommend.ts): heuristics support · 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](https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/parser.ts): incremental parsing · Append-oriented and chunk-aware parsing layer with bounded buffers and editable-buffer support. · `src/stream/parser.ts`
- [Streaming API](https://github.com/simon-he95/markdown-it-ts/blob/main/src/streaming/stream_parser.ts): stream facade · Higher-level streaming parser surface built on top of the stream layer. · `src/streaming/stream_parser.ts`
- [Buffers](https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/buffer.ts): buffer strategies · Chunked, debounced, unbounded, and piece-table-backed buffer implementations. · `src/stream/buffer.ts`
- [Editable buffer](https://github.com/simon-he95/markdown-it-ts/blob/main/src/stream/editable.ts): editable text core · Piece-table and edit-aware buffer support for non-append workflows. · `src/stream/editable.ts`

### Ops

Documentation, benchmarks, CI, and demo tooling.

- [Bench harness](https://github.com/simon-he95/markdown-it-ts/blob/main/benchmark/stream-parse-bench.ts): performance suite · Implementation comparisons, regression checks, and report generation around parser and renderer performance. · `benchmark/stream-parse-bench.ts`
- [Docs + reports](https://github.com/simon-he95/markdown-it-ts/blob/main/docs/architecture.md): docs and tracking · Architecture notes, migration docs, compatibility reports, performance history, and generated summaries. · `docs/architecture.md`
- [Demo editor](https://github.com/simon-he95/markdown-it-ts/blob/main/editor/src/App.vue): vue demo app · Interactive Vite/Vue demo for showcasing the parser in an editor-like workflow. · `editor/src/App.vue`
- [CI deploy](https://github.com/simon-he95/markdown-it-ts/blob/main/.github/workflows/ci.yml): workflows · GitHub Actions for build verification, docs generation, benchmark generation, and optional Netlify deployment. · `.github/workflows/ci.yml`

## Connections

- 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.
