fix: unnecessary eval

This commit is contained in:
Han Xiao 2025-03-19 08:37:35 +08:00
parent 71157a7468
commit 023bf0ef9c
2 changed files with 57 additions and 8 deletions

View File

@ -31,7 +31,7 @@ import {
rankURLs,
filterURLs,
normalizeUrl,
weightedURLToString, getLastModified, keepKPerHostname, processURLs
weightedURLToString, getLastModified, keepKPerHostname, processURLs, fixBadURLMdLinks
} from "./utils/url-tools";
import {
buildMdFromAnswer,
@ -911,13 +911,17 @@ But unfortunately, you failed to solve the issue. You need to think out of the b
}
if (!trivialQuestion) {
(thisStep as AnswerAction).mdAnswer = fixCodeBlockIndentation(await fixMarkdown(
buildMdFromAnswer((thisStep as AnswerAction)),
allKnowledge,
context,
SchemaGen
)
);
(thisStep as AnswerAction).mdAnswer =
fixBadURLMdLinks(
fixCodeBlockIndentation(
await fixMarkdown(
buildMdFromAnswer((thisStep as AnswerAction)),
allKnowledge,
context,
SchemaGen
)
),
allURLs);
} else {
(thisStep as AnswerAction).mdAnswer = fixCodeBlockIndentation(
buildMdFromAnswer((thisStep as AnswerAction))

View File

@ -545,4 +545,49 @@ export async function processURLs(
success: validResults.length > 0,
badURLs
};
}
export function fixBadURLMdLinks(mdContent: string, allURLs: Record<string, SearchSnippet>): string {
// Regular expression to find markdown links with the pattern [url](url)
const mdLinkRegex = /\[([^\]]+)]\(([^)]+)\)/g;
// Replace each match with a prettier version
return mdContent.replace(mdLinkRegex, (match, text, url) => {
// Check if the text and URL are the same
if (text === url) {
// Look up the URL directly in the record using the url as key
const urlInfo = allURLs[url];
if (urlInfo) {
try {
// Extract hostname from the URL
const hostname = new URL(url).hostname;
// If title is available, use [title - hostname](url) format
if (urlInfo.title) {
return `[${urlInfo.title} - ${hostname}](${url})`;
}
// Otherwise use [hostname](url) format
else {
return `[${hostname}](${url})`;
}
} catch (e) {
// If URL parsing fails, return the original link
return match;
}
} else {
// If URL is not in allURLs, try to extract hostname
try {
const hostname = new URL(url).hostname;
return `[${hostname}](${url})`;
} catch (e) {
// If URL parsing fails, return the original link
return match;
}
}
} else {
// If the text and URL are not the same, leave the link as is
return match;
}
});
}