fix: 优化markdown解析器
This commit is contained in:
parent
dde679fd6e
commit
ba2ff0a2b0
|
|
@ -236,8 +236,24 @@ impl MarkdownParser {
|
||||||
|
|
||||||
// 收集所有事件和位置信息
|
// 收集所有事件和位置信息
|
||||||
for event in parser {
|
for event in parser {
|
||||||
events.push((event, current_offset));
|
events.push((event.clone(), current_offset));
|
||||||
current_offset += 1; // 简化的偏移计算
|
|
||||||
|
// 根据事件类型正确计算字节偏移
|
||||||
|
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
|
// 构建AST
|
||||||
|
|
@ -347,8 +363,6 @@ impl MarkdownParser {
|
||||||
// 处理其他事件类型
|
// 处理其他事件类型
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
current_offset += 1; // 简化的偏移计算
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(root)
|
Ok(root)
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,8 @@ export const EnhancedMarkdownRenderer: React.FC<EnhancedMarkdownRendererProps> =
|
||||||
// 计算节点在原始文本中的字节偏移位置(与grounding数据的字节偏移匹配)
|
// 计算节点在原始文本中的字节偏移位置(与grounding数据的字节偏移匹配)
|
||||||
const nodeStartOffset = node.range?.start?.byte_offset || 0;
|
const nodeStartOffset = node.range?.start?.byte_offset || 0;
|
||||||
const nodeEndOffset = node.range?.end?.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支持信息
|
// 查找与当前节点位置重叠的grounding支持信息
|
||||||
const relatedSupports = groundingMetadata.grounding_supports.filter(support => {
|
const relatedSupports = groundingMetadata.grounding_supports.filter(support => {
|
||||||
// grounding数据使用字节偏移
|
// grounding数据使用字节偏移
|
||||||
|
|
@ -116,7 +117,9 @@ export const EnhancedMarkdownRenderer: React.FC<EnhancedMarkdownRendererProps> =
|
||||||
console.log({
|
console.log({
|
||||||
relatedSupports,
|
relatedSupports,
|
||||||
nodeStartOffset,
|
nodeStartOffset,
|
||||||
nodeEndOffset
|
nodeEndOffset,
|
||||||
|
nodeStartOffset1,
|
||||||
|
nodeEndOffset1
|
||||||
})
|
})
|
||||||
if (relatedSupports.length > 0) {
|
if (relatedSupports.length > 0) {
|
||||||
// 获取相关的来源信息
|
// 获取相关的来源信息
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue