commonmark / commonmark/commonmark-java
Support Incremental Parsing for Streaming Markdown Input/支持流式 Markdown 输入的增量解析
还没有人认领这个 Issue。
- 主要语言
- Java
- 星标
- 2.7k
- 派生
- 336
- PR 合并指标
- 30 天内没有已合并 PR
描述
English
Background
Currently, commonmark-java parses Markdown via Parser.parse(String markdown), which always processes the entire input from scratch and rebuilds the AST.
In streaming scenarios (e.g., live Markdown preview, collaborative editing, chat messages), new content is appended continuously. Re-parsing the whole document each time is inefficient and can cause performance issues for large documents.
Request
I would like to request incremental parsing support — the ability to parse only the newly added Markdown content and update the existing AST without re-parsing unchanged parts.
Possible Approach
- Maintain a parsing context (
ParserContext) that stores:- Current open block parsers
- AST root node
- Current line number / position
- Add a method like:
which:Node parseIncremental(String newMarkdown, ParserContext context);- Resumes parsing from the saved context
- Continues open blocks if possible (
canContinue()logic) - Creates new blocks for new content
- Parses inline elements only for new blocks
- Update the AST in place, avoiding full rebuild.
Benefits
- Significant performance improvement for streaming or real-time editing scenarios.
- Reduced memory and CPU usage for large documents.
- Enables more responsive applications using
commonmark-java.
Example Use Case
Parser parser = Parser.builder().build();
ParserContext context = new ParserContext();
Node doc = parser.parseIncremental(initialMarkdown, context);
doc = parser.parseIncremental(newMarkdownChunk, context);
Would you consider adding this feature or providing hooks to implement it externally?
中文
背景
目前,commonmark-java 通过 Parser.parse(String markdown) 解析 Markdown,每次都会从头处理整个输入并重建 AST。
在流式场景(例如实时 Markdown 预览、协同编辑、聊天消息)中,内容会不断追加。每次都全量解析会导致性能浪费,尤其是文档较大时。
需求
希望能支持 增量解析 —— 只解析新增的 Markdown 内容,并在已有 AST 基础上更新,而不重新解析未变化的部分。
可能的实现思路
- 维护一个解析上下文(
ParserContext),保存:- 当前未闭合的块解析器
- AST 根节点
- 当前行号 / 位置
- 增加一个方法,例如:
该方法:Node parseIncremental(String newMarkdown, ParserContext context);- 从保存的上下文恢复解析状态
- 如果可能,继续旧块(依赖
canContinue()逻辑) - 为新增内容创建新块
- 仅对新增块进行行内解析
- 在原 AST 上直接更新,避免全量重建。
好处
- 在流式或实时编辑场景下显著提升性能。
- 降低大文档的内存和 CPU 消耗。
- 让基于
commonmark-java的应用更加流畅、响应更快。
使用示例
Parser parser = Parser.builder().build();
ParserContext context = new ParserContext();
Node doc = parser.parseIncremental(initialMarkdown, context);
doc = parser.parseIncremental(newMarkdownChunk, context);
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查 Parser.parse(String markdown) 以及通过 canContinue() 描述的现有块解析器续解析逻辑。定义 ParserContext 如何在追加的 chunk 之间保留未关闭的解析器、AST 根节点和当前位置。完成的标准是达成一致的增量 API 和行为:在不重新解析未更改输入的情况下更新现有 AST,并通过测试覆盖流式场景。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- java
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100