sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -0,0 +1,33 @@
function stripCompleteFences(content: string): string {
return content.replace(/```[\s\S]*?```/g, "");
}
export function looksLikeHtmlDocument(content: string): boolean {
const unfenced = stripCompleteFences(content);
if (/<!DOCTYPE\s+html/i.test(unfenced) || /<html(?:\s|>)/i.test(unfenced)) {
return true;
}
// Mid-stream ```html fence is not closed yet, so the page source is still
// visible to marked as ordinary markdown.
return (
/```(?:html|htm|svg|xml)\b/i.test(unfenced) &&
/<\/?(?:html|head|body|style|div|section)\b/i.test(unfenced)
);
}
const UNFENCED_HTML_RE =
/<\/?(?:html|head|body|style|script|section|header|main)\b/i;
const RUNAWAY_LIST_MARKERS_RE = /(?:[-*+] |\d+\. ){40,}/;
export function isUnsafeStreamdownContent(content: string): boolean {
if (looksLikeHtmlDocument(content)) {
return true;
}
const unfenced = stripCompleteFences(content);
if (UNFENCED_HTML_RE.test(unfenced)) {
return true;
}
// marked's list tokenizer also blows the stack on a single line of
// thousands of "- " markers, not only on deep indentation.
return RUNAWAY_LIST_MARKERS_RE.test(unfenced);
}
@@ -1,2 +1,6 @@
export * from "./plugins";
export * from "./mermaid-png-download";
export * from "./html-document";
export * from "./markdown-render-boundary";
export * from "./preprocess";
export * from "./safe-streamdown";
@@ -0,0 +1,59 @@
import assert from "node:assert/strict";
import test from "node:test";
// @ts-expect-error Node's type-stripping test runner requires the explicit .ts suffix.
import {
isUnsafeStreamdownContent,
looksLikeHtmlDocument,
} from "./html-document.ts";
// @ts-expect-error Node's type-stripping test runner requires the explicit .ts suffix.
import {
canSafelyLexMarkdown,
capListNesting,
capMarkdownNesting,
} from "./preprocess.ts";
test("looksLikeHtmlDocument detects a streamed HTML page", () => {
assert.equal(looksLikeHtmlDocument("<!DOCTYPE html>\n<html><body>hi</body></html>"), true);
assert.equal(
looksLikeHtmlDocument('<html lang="zh"><body style="background-image:url(x)"></body></html>'),
true,
);
assert.equal(looksLikeHtmlDocument("请查看右侧预览"), false);
assert.equal(
looksLikeHtmlDocument("示例:\n```html\n<!DOCTYPE html>\n<html></html>\n```"),
false,
);
assert.equal(
looksLikeHtmlDocument("先写结构:\n```html\n<!DOCTYPE html>\n<html><body>"),
true,
);
});
test("isUnsafeStreamdownContent catches unfenced page source and list bombs", () => {
assert.equal(
isUnsafeStreamdownContent(
'<style>\nbody { background-image: url("https://example.test"); }\n</style>',
),
true,
);
assert.equal(isUnsafeStreamdownContent("- 第一步\n- 第二步\n然后写入文件"), false);
assert.equal(isUnsafeStreamdownContent(`${"- ".repeat(40)}item`), true);
});
test("capListNesting leaves ordinary thinking text unchanged", () => {
const input = "我将生成页面:\n- 使用背景图\n- 写入 HTML 文件";
assert.equal(capListNesting(input), input);
});
test("capMarkdownNesting caps pathological list indentation", () => {
const input = `${" ".repeat(500)}- item`;
const result = capMarkdownNesting(input);
assert.equal(/^[ \t]*/.exec(result)![0].length, 200);
assert.ok(result.endsWith("- item"));
});
test("canSafelyLexMarkdown rejects a single-line list bomb", () => {
assert.equal(canSafelyLexMarkdown("- 第一步\n- 第二步"), true);
assert.equal(canSafelyLexMarkdown(`${"- ".repeat(3000)}item`), false);
});
@@ -0,0 +1,63 @@
"use client";
import { Component, type ErrorInfo, type ReactNode } from "react";
export function MarkdownFallback({
message,
text,
}: {
message: string;
text?: string;
}) {
return (
<div className="text-muted-foreground rounded-md border bg-muted/40 px-3 py-2 text-sm">
<p>{message}</p>
{text ? (
<pre className="mt-2 max-h-40 overflow-auto whitespace-pre-wrap break-all text-xs">
{text}
</pre>
) : null}
</div>
);
}
export class MarkdownRenderBoundary extends Component<
{ children: ReactNode; resetKey: string; fallbackText?: string },
{ error: Error | null; resetKey: string }
> {
state = { error: null as Error | null, resetKey: this.props.resetKey };
static getDerivedStateFromError(error: Error) {
return { error };
}
static getDerivedStateFromProps(
props: { resetKey: string },
state: { error: Error | null; resetKey: string },
) {
if (props.resetKey !== state.resetKey) {
return { error: null, resetKey: props.resetKey };
}
return null;
}
componentDidCatch(error: Error, info: ErrorInfo) {
console.error("Markdown renderer failed", error, info.componentStack);
}
render() {
if (this.state.error) {
if (this.props.fallbackText) {
return (
<div className="whitespace-pre-wrap break-words">
{this.props.fallbackText}
</div>
);
}
return (
<MarkdownFallback message="这段内容暂时无法渲染,已改为纯文本显示,聊天不会中断。" />
);
}
return this.props.children;
}
}
@@ -0,0 +1,92 @@
import { marked } from "marked";
const CODE_FENCE_RE = /^ {0,3}(?:```|~~~)/;
const INDENTED_CODE_RE = /^(?: {4}|\t)/;
// marked's blockquote tokenizer recurses once per nesting level.
const MAX_BLOCKQUOTE_DEPTH = 100;
const DEEP_BLOCKQUOTE_HINT_RE = new RegExp(
`^(?:[ \\t]*>){${MAX_BLOCKQUOTE_DEPTH + 1}}`,
"m",
);
const BLOCKQUOTE_PREFIX_RE = /^ {0,3}(?:[ \t]*>)+/;
// marked's list tokenizer recurses list → blockTokens → list. Each level
// needs ~2 columns of indent; 200 columns ≈ 100 levels, below the browser
// stack overflow that white-screens the chat (upstream deer-flow #3393).
const MAX_LIST_INDENT = 200;
const DEEP_INDENT_HINT_RE = new RegExp(`^[ \\t]{${MAX_LIST_INDENT + 1},}`, "m");
export function capBlockquoteNesting(markdown: string): string {
if (!DEEP_BLOCKQUOTE_HINT_RE.test(markdown)) {
return markdown;
}
let insideFence = false;
return markdown
.split("\n")
.map((line) => {
if (CODE_FENCE_RE.test(line)) {
insideFence = !insideFence;
return line;
}
if (insideFence || INDENTED_CODE_RE.test(line)) {
return line;
}
const match = BLOCKQUOTE_PREFIX_RE.exec(line);
if (!match) {
return line;
}
const prefix = match[0];
let depth = 0;
for (let i = 0; i < prefix.length; i++) {
if (prefix[i] === ">") {
depth += 1;
if (depth > MAX_BLOCKQUOTE_DEPTH) {
return line.slice(0, i) + line.slice(prefix.length);
}
}
}
return line;
})
.join("\n");
}
export function capListNesting(markdown: string): string {
if (!DEEP_INDENT_HINT_RE.test(markdown)) {
return markdown;
}
let insideFence = false;
return markdown
.split("\n")
.map((line) => {
if (CODE_FENCE_RE.test(line)) {
insideFence = !insideFence;
return line;
}
if (insideFence) {
return line;
}
const whitespace = /^[ \t]*/.exec(line)![0];
if (whitespace.length <= MAX_LIST_INDENT) {
return line;
}
return " ".repeat(MAX_LIST_INDENT) + line.slice(whitespace.length);
})
.join("\n");
}
export function capMarkdownNesting(markdown: string): string {
return capListNesting(capBlockquoteNesting(markdown));
}
/** Same call Streamdown uses (`Lexer.lex` + gfm). RangeError means do not mount it. */
export function canSafelyLexMarkdown(markdown: string): boolean {
try {
marked.lexer(markdown, { gfm: true });
return true;
} catch {
return false;
}
}
@@ -0,0 +1,70 @@
"use client";
import { useMemo } from "react";
import { Streamdown, type StreamdownProps } from "streamdown";
import {
isUnsafeStreamdownContent,
looksLikeHtmlDocument,
} from "./html-document";
import {
MarkdownFallback,
MarkdownRenderBoundary,
} from "./markdown-render-boundary";
import { canSafelyLexMarkdown, capMarkdownNesting } from "./preprocess";
function PlainMarkdownText({
className,
text,
}: {
className?: string;
text: string;
}) {
return (
<div className={className}>
<div className="whitespace-pre-wrap break-words">{text}</div>
</div>
);
}
export function SafeStreamdown({
children,
parseIncompleteMarkdown = false,
className,
...props
}: StreamdownProps) {
const raw = typeof children === "string" ? children : null;
const text = raw != null ? capMarkdownNesting(raw) : null;
const resetKey = text != null ? `${text.length}:${text.slice(-48)}` : "node";
const renderAsPlain = useMemo(() => {
if (text == null) {
return false;
}
if (looksLikeHtmlDocument(text) || isUnsafeStreamdownContent(text)) {
return true;
}
return !canSafelyLexMarkdown(text);
}, [text]);
if (text != null && looksLikeHtmlDocument(text)) {
return (
<MarkdownFallback message="正在生成 HTML 页面,请查看右侧预览。" />
);
}
if (text != null && renderAsPlain) {
return <PlainMarkdownText className={className} text={text} />;
}
return (
<MarkdownRenderBoundary resetKey={resetKey} fallbackText={text ?? undefined}>
<Streamdown
className={className}
parseIncompleteMarkdown={parseIncompleteMarkdown}
{...props}
>
{text ?? children}
</Streamdown>
</MarkdownRenderBoundary>
);
}