mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-19 12:24:46 +08:00
fix: correctly remove outermost code block markers in model responses (fix markdown rendering issue) (#386)
* fix: correctly remove outermost code block markers in frontend * fix: correctly remove outermost quote block markers in 'dropMarkdownQuote' * fix: correctly remove outermost quote block markers in 'dropMarkdownQuote' * fix: correctly remove outermost quote block markers in 'dropMarkdownQuote' --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
@@ -122,11 +122,47 @@ function processKatexInMarkdown(markdown?: string | null) {
|
|||||||
return markdownWithKatexSyntax;
|
return markdownWithKatexSyntax;
|
||||||
}
|
}
|
||||||
|
|
||||||
function dropMarkdownQuote(markdown?: string | null) {
|
function dropMarkdownQuote(markdown?: string | null): string | null {
|
||||||
if (!markdown) return markdown;
|
if (!markdown) return null;
|
||||||
return markdown
|
|
||||||
.replace(/^```markdown\n/gm, "")
|
const patternsToRemove = [
|
||||||
.replace(/^```text\n/gm, "")
|
{ prefix: "```markdown\n", suffix: "\n```", prefixLen: 12 },
|
||||||
.replace(/^```\n/gm, "")
|
{ prefix: "```text\n", suffix: "\n```", prefixLen: 8 },
|
||||||
.replace(/\n```$/gm, "");
|
{ prefix: "```\n", suffix: "\n```", prefixLen: 4 },
|
||||||
}
|
];
|
||||||
|
|
||||||
|
let result = markdown;
|
||||||
|
|
||||||
|
for (const { prefix, suffix, prefixLen } of patternsToRemove) {
|
||||||
|
if (result.startsWith(prefix) && !result.endsWith(suffix)) {
|
||||||
|
result = result.slice(prefixLen);
|
||||||
|
break; // remove prefix without suffix only once
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let changed = true;
|
||||||
|
|
||||||
|
while (changed) {
|
||||||
|
changed = false;
|
||||||
|
|
||||||
|
for (const { prefix, suffix, prefixLen } of patternsToRemove) {
|
||||||
|
let startIndex = 0;
|
||||||
|
while ((startIndex = result.indexOf(prefix, startIndex)) !== -1) {
|
||||||
|
const endIndex = result.indexOf(suffix, startIndex + prefixLen);
|
||||||
|
if (endIndex !== -1) {
|
||||||
|
// only remove fully matched code blocks
|
||||||
|
const before = result.slice(0, startIndex);
|
||||||
|
const content = result.slice(startIndex + prefixLen, endIndex);
|
||||||
|
const after = result.slice(endIndex + suffix.length);
|
||||||
|
result = before + content + after;
|
||||||
|
changed = true;
|
||||||
|
startIndex = before.length + content.length;
|
||||||
|
} else {
|
||||||
|
startIndex += prefixLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user