fix: 优化markdown解析器

This commit is contained in:
imeepos 2025-07-22 16:16:11 +08:00
parent dde679fd6e
commit ba2ff0a2b0
2 changed files with 23 additions and 6 deletions

View File

@ -236,8 +236,24 @@ impl MarkdownParser {
// 收集所有事件和位置信息
for event in parser {
events.push((event, current_offset));
current_offset += 1; // 简化的偏移计算
events.push((event.clone(), current_offset));
// 根据事件类型正确计算字节偏移
match &event {
Event::Text(text) => {
current_offset += text.len();
}
Event::Code(code) => {
current_offset += code.len();
}
Event::SoftBreak | Event::HardBreak => {
current_offset += 1; // 换行符通常是1字节
}
_ => {
// 对于其他事件类型,不增加偏移量
// 因为它们通常是结构性的,不对应实际的文本内容
}
}
}
// 构建AST
@ -347,8 +363,6 @@ impl MarkdownParser {
// 处理其他事件类型
}
}
current_offset += 1; // 简化的偏移计算
}
Ok(root)

View File

@ -102,7 +102,8 @@ export const EnhancedMarkdownRenderer: React.FC<EnhancedMarkdownRendererProps> =
// 计算节点在原始文本中的字节偏移位置与grounding数据的字节偏移匹配
const nodeStartOffset = node.range?.start?.byte_offset || 0;
const nodeEndOffset = node.range?.end?.byte_offset || 0;
const nodeStartOffset1 = node.range?.start?.offset || 0;
const nodeEndOffset1 = node.range?.end?.offset || 0;
// 查找与当前节点位置重叠的grounding支持信息
const relatedSupports = groundingMetadata.grounding_supports.filter(support => {
// grounding数据使用字节偏移
@ -116,7 +117,9 @@ export const EnhancedMarkdownRenderer: React.FC<EnhancedMarkdownRendererProps> =
console.log({
relatedSupports,
nodeStartOffset,
nodeEndOffset
nodeEndOffset,
nodeStartOffset1,
nodeEndOffset1
})
if (relatedSupports.length > 0) {
// 获取相关的来源信息