Repository navigation
fix: resolve markdown format loss in large docs - #623
Conversation
There was a problem hiding this comment.
Sorry @pengfeixx, you've used your own review budget of 250,000 diff characters for the last 7 days.
You can request another review in 1 day and 3 hours by commenting @sourcery-ai review. Upgrade to get a review now.
Reviewer's GuideThe PR fixes cross-chunk markdown formatting loss by parsing oversized documents as a whole, then progressively inserting the resulting top-level nodes in event-loop-yielding batches; the progressive path now activates only above 1MB while preserving initial rendering, progress reporting, scroll handling, and render-generation cancellation. Sequence diagram for progressive large-document markdown renderingsequenceDiagram
participant Input as Markdown input
participant Renderer as renderMarkdown
participant Parser as parserCtx
participant Editor as Editor state
participant Timer as Event loop
Input->>Renderer: renderMarkdown(md)
alt md below 1MB
Renderer->>Editor: replaceAll(md)
else md at least 1MB
Renderer->>Parser: parserCtx(md)
Parser-->>Renderer: parsed top-level nodes
Renderer->>Editor: replaceWithNodes(first batch)
loop remaining batches
Renderer->>Timer: setTimeout(step, 0)
Timer->>Editor: appendChunk(batch)
end
end
Flow diagram for markdown rendering path selectionflowchart TD
A[Markdown document] --> B{Size at least 1MB?}
B -- No --> C[replaceAll fast path]
B -- Yes --> D[Parse entire document with parserCtx]
D --> E[Group parsed top-level nodes into batches]
E --> F[Replace document with first batch]
F --> G[Yield with setTimeout]
G --> H[Insert next batch with tr.insert]
H --> I{More batches?}
I -- Yes --> G
I -- No --> J[Complete rendering and restore scroll handling]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
b65d892 to
fd919e4
Compare
deepin pr auto review🤖 AI 代码审查报告📊 总体评价
📋 PR 信息
🔍 详细分析1. 语法逻辑 ❌评价: 需改进 ❌ 不通过 潜在问题:
建议: 在 let parsed = null;
editor.action((ctx) => {
try {
parsed = ctx.get(parserCtx)(md);
} catch (e) {
console.error('Markdown parse failed:', e);
lastValue = '';
}
});
if (!parsed) return;2. 代码质量 ✅评价: 良好 ✅ 通过 潜在问题:
建议: 注释详尽,结构清晰。建议优化单节点场景的冗余滚动调用,可在 else 分支前增加判断条件。 3. 代码性能 ❌评价: 需改进 ❌ 不通过 潜在问题:
建议:
4. 代码安全 🔒评价: 优秀 ✅ 通过
安全漏洞详情: 漏洞对比统计: 新增漏洞 0 个,减少漏洞 0 个,持平 0 个 建议: 无安全风险,代码安全合规。ProseMirror 解析器内置输入净化机制,无注入风险。 💡 改进建议代码示例// 改进后的 renderProgressively 函数(添加异常处理 + 避免冗余拷贝)
function renderProgressively(md, gen) {
let parsed = null;
editor.action((ctx) => {
try {
parsed = ctx.get(parserCtx)(md);
} catch (e) {
console.error('Markdown parse failed:', e);
lastValue = ''; // 重置以允许重试
return;
}
});
if (!parsed) return;
// 直接使用 Fragment 索引访问,避免冗余数组拷贝
const content = parsed.content;
const totalSize = content.size;
const nodeCount = content.childCount;
let renderedSize = 0;
let index = 0;
buildProgress = { fraction: 0 };
const step = () => {
if (gen !== renderGeneration || !editor) return;
if (index >= nodeCount) {
buildProgress = null;
if (!userScrolledDuringBuild) reapplyScroll();
return;
}
const batch = content.child(index++);
renderedSize += batch.nodeSize;
if (index === 1) {
replaceWithNodes([batch]);
setTimeout(() => {
if (gen !== renderGeneration) return;
reapplyScroll();
}, 0);
} else {
appendChunk([batch]);
}
buildProgress.fraction = totalSize > 0 ? renderedSize / totalSize : 1;
if (index < nodeCount) {
setTimeout(step, 0);
} else {
buildProgress = null;
if (!userScrolledDuringBuild) reapplyScroll();
}
};
step();
}本报告由 AI 代码审查工具自动生成 |
fd919e4 to
b6de264
Compare
1. Root cause: progressive rendering split documents into chunks and parsed each independently, losing cross-chunk markdown semantics such as reference link definitions, tables, and loose lists 2. Fix: parse the entire document once with parserCtx(md) before batch inserting top-level nodes grouped by nodeSize (first batch ~256KB for fast first paint, subsequent batches ~1MB to bound dispatch count), ensuring all cross-chunk semantics are resolved correctly during progressive rendering 3. Impact: raised PROGRESSIVE_THRESHOLD from 64KB to 1MB so most documents use the fast replaceAll path; only >1MB documents trigger progressive insertion with correct whole-document parsing. Per-node batching filled a 1.4MB doc in ~172s; size-based batching fills it in ~7s (measured in headless Chromium) Log: Fixed markdown format loss when pasting large text in editor Influence: 1. Test pasting large markdown documents (>64KB) with tables and reference links to verify format renders correctly 2. Test normal-sized markdown documents for no regression 3. Test very large documents (>1MB) for progressive rendering behavior and verify the progressive fill completes within seconds fix: 修复大段文本粘贴时阅览区 markdown 格式丢失 1. 根因:渐进渲染将文档分块后独立解析,导致跨块的 markdown 语义 断裂,引用式链接定义、表格、松散列表等格式丢失 2. 方案:先用 parserCtx(md) 整篇解析文档,再将解析后的顶层节点 按 nodeSize 分批(首批约 256KB 抢首屏、后续每批约 1MB 压低批数) 插入,确保所有跨块语义正确解析 3. 影响:将渐进渲染阈值从 64KB 提高至 1MB,绝大多数文档走快速 replaceAll 路径,仅超大文档触发渐进插入。逐节点分批填充 1.4MB 文档实测约 172 秒,按大小分批后约 7 秒完成(headless Chromium 实测) Log: 修复文本编辑器粘贴大段文本时预览区 markdown 格式丢失 Influence: 1. 测试粘贴含表格和引用链接的大段 markdown 文档(>64KB),验证格式正确渲染 2. 测试普通大小 markdown 文档无回归 3. 测试超大文档(>1MB)渐进渲染行为,验证渐进填充在秒级完成 PMS: BUG-378329
b6de264 to
ead4518
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
fix: resolve markdown format loss in large docs
and parsed each chunk independently, breaking cross-chunk markdown
semantics like reference link definitions, loose lists, and tables
markdown semantics, then insert parsed top-level nodes in batches via
tr.insert with setTimeout between batches for UI responsiveness
use replaceAll fast path; only very large docs trigger progressive
insertion with correct semantics
Log: Fix markdown format loss when pasting large markdown text in editor
Influence:
fix: 修复大段markdown文本渐进渲染格式丢失
语义断裂(引用式链接定义、松散列表、表格等被截断)
节点分批通过tr.insert追加到文档末尾,每批间让出事件循环
快速路径,仅超大文档触发渐进插入且语义正确
Log: 修复文本编辑器粘贴大段markdown文本时阅览区格式丢失问题
Influence:
PMS: BUG-378329
Summary by Sourcery
Preserve Markdown formatting in large documents while keeping progressive rendering responsive.
Bug Fixes:
Enhancements: