Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(preview): handle asterisk emphasis beside letters and digits

bluefateludic5ca99fdb86a
diff --git a/shared/plain-preview.ts b/shared/plain-preview.ts
index 690e36e123..ec89cc56fe 100644
--- a/shared/plain-preview.ts
+++ b/shared/plain-preview.ts
@@ -32,8 +32,9 @@ export function plainPreviewText(text: string): string {
out = out.replace(/^[ \t]*\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)*\|?[ \t]*$/gm, "");
out = out.replace(/^[ \t]*\|(.*)\|[ \t]*$/gm, (_row, cells: string) => cells.split("|").map((cell) => cell.trim()).filter(Boolean).join(" "));
// Underscores inside a word (snake_case) and a lone * (2 * 3) are not emphasis.
- out = out.replace(/(\*{1,3}|_{1,3})(?=\S)([\s\S]*?\S)\1(?![\p{L}\p{N}])/gu, (match, marks: string, inner: string, offset: number, whole: string) => {
- if (marks[0] === "_" && offset > 0 && /[\p{L}\p{N}]/u.test(whole[offset - 1]!)) return match;
+ out = out.replace(/(\*{1,3})(?=\S)([\s\S]*?\S)\1/gu, "$2");
+ out = out.replace(/(_{1,3})(?=\S)([\s\S]*?\S)\1(?![\p{L}\p{N}])/gu, (match, _marks: string, inner: string, offset: number, whole: string) => {
+ if (/[\p{L}\p{N}]$/u.test(whole.slice(0, offset))) return match;
return inner;
});
out = out.replace(/~~(?=\S)([\s\S]*?\S)~~/g, "$1");
diff --git a/src/lib/plain-preview.test.ts b/src/lib/plain-preview.test.ts
index 7a1c4e9397..fc3ebe442e 100644
--- a/src/lib/plain-preview.test.ts
+++ b/src/lib/plain-preview.test.ts
@@ -15,6 +15,19 @@ describe("plainPreviewText", () => {
["> quoted line\n>> nested", "quoted line nested"],
["| name | score |\n|---|:---:|\n| Ada | 9 |", "name score Ada 9"],
["_lightly_ and *also* and ~~gone~~ and ***all***", "lightly and also and gone and all"],
+ ["任务*已经完成*请查收", "任务已经完成请查收"],
+ ["pre*view*text", "previewtext"],
+ ["*a*b", "ab"],
+ ["*a*1", "a1"],
+ ["*a* b", "a b"],
+ ["*a*。", "a。"],
+ ["_a_b", "_a_b"],
+ ["a_b_", "a_b_"],
+ ["_a_b_c_", "a_b_c"],
+ ["𐐀_a_", "𐐀_a_"],
+ ["_a_𐐀", "_a_𐐀"],
+ ["\\*a\\*b", "*a*b"],
+ ["`*a*b`", "*a*b"],
["above\n\n---\n\nbelow", "above below"],
["line one<br>line two", "line one line two"],
["a reply cut off mid **bold", "a reply cut off mid bold"],