fix: md ref

This commit is contained in:
Han Xiao 2025-04-14 12:19:45 +08:00
parent 1f9565ec84
commit 098564eb81
2 changed files with 30 additions and 12 deletions

View File

@ -996,7 +996,7 @@ But unfortunately, you failed to solve the issue. You need to think out of the b
answerStep.answer = answer; answerStep.answer = answer;
answerStep.references = references; answerStep.references = references;
await updateReferences(answerStep, allURLs) await updateReferences(answerStep, allURLs)
answerStep.mdAnswer = buildMdFromAnswer(answerStep); answerStep.mdAnswer = repairMarkdownFootnotesOuter(buildMdFromAnswer(answerStep));
} else { } else {
answerStep.mdAnswer = answerStep.mdAnswer =
convertHtmlTablesToMd( convertHtmlTablesToMd(

View File

@ -223,7 +223,7 @@ export async function buildReferences(
const referencesByPosition = [...references] const referencesByPosition = [...references]
.sort((a, b) => a.answerChunkPosition![0] - b.answerChunkPosition![0]); .sort((a, b) => a.answerChunkPosition![0] - b.answerChunkPosition![0]);
// Insert markers from beginning to end, tracking offset // Insert markers from beginning to end, tracking offset
let offset = 0; let offset = 0;
for (let i = 0; i < referencesByPosition.length; i++) { for (let i = 0; i < referencesByPosition.length; i++) {
const ref = referencesByPosition[i]; const ref = referencesByPosition[i];
@ -232,17 +232,35 @@ export async function buildReferences(
// Calculate position to insert the marker (end of the chunk + current offset) // Calculate position to insert the marker (end of the chunk + current offset)
let insertPosition = ref.answerChunkPosition![1] + offset; let insertPosition = ref.answerChunkPosition![1] + offset;
// Check if there's a newline or table pipe at the end of the chunk and adjust position // Look ahead to check if there's a list item coming next
const chunkEndText = modifiedAnswer.substring(Math.max(0, insertPosition - 5), insertPosition); const textAfterInsert = modifiedAnswer.substring(insertPosition);
const newlineMatch = chunkEndText.match(/\n+$/); const nextListItemMatch = textAfterInsert.match(/^\s*\n\s*\*/);
const tableEndMatch = chunkEndText.match(/\s*\|\s*$/);
if (newlineMatch) { // If we're at a position where the next content is a list item,
// Move the insertion position before the newline(s) // we need to adjust WHERE we place the footnote
insertPosition -= newlineMatch[0].length; if (nextListItemMatch) {
} else if (tableEndMatch) { // Move the marker to right after the last content character,
// Move the insertion position before the table end pipe // but INSIDE any punctuation at the end of the content
insertPosition -= tableEndMatch[0].length; const beforeText = modifiedAnswer.substring(Math.max(0, insertPosition - 30), insertPosition);
const lastPunctuation = beforeText.match(/[!。?!.?]$/);
if (lastPunctuation) {
// If there's punctuation at the end, insert the marker before it
insertPosition--;
}
} else {
// The original conditions for newlines and table pipes can remain
const chunkEndText = modifiedAnswer.substring(Math.max(0, insertPosition - 5), insertPosition);
const newlineMatch = chunkEndText.match(/\n+$/);
const tableEndMatch = chunkEndText.match(/\s*\|\s*$/);
if (newlineMatch) {
// Move the insertion position before the newline(s)
insertPosition -= newlineMatch[0].length;
} else if (tableEndMatch) {
// Move the insertion position before the table end pipe
insertPosition -= tableEndMatch[0].length;
}
} }
// Insert the marker // Insert the marker