Skip to content

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

fix(preview): drop a Setext underline, and read a spaced rule as one

lhy-dcff601619eee25
diff --git a/shared/plain-preview.test.ts b/shared/plain-preview.test.ts
new file mode 100644
index 0000000000..898ad7b6d9
--- /dev/null
+++ b/shared/plain-preview.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+
+import { plainPreviewText } from "./plain-preview.ts";
+
+describe("the one line a sidebar or a notification shows", () => {
+ it("keeps the words of the markdown a person typed, and none of its markers", () => {
+ expect(plainPreviewText("**bold** and _italic_ and ~~strike~~")).toBe("bold and italic and strike");
+ expect(plainPreviewText("- [ ] buy milk")).toBe("buy milk");
+ expect(plainPreviewText("1. first\n2. second")).toBe("first second");
+ expect(plainPreviewText("# Heading\ntext")).toBe("Heading text");
+ expect(plainPreviewText("> quoted\n> lines")).toBe("quoted lines");
+ expect(plainPreviewText("see [docs](https://x/y_(z)) now")).toBe("see docs now");
+ // Underscores inside a word, and a lone `*` between numbers, are no emphasis.
+ expect(plainPreviewText("snake_case_name stays; 2 * 3 = 6")).toBe("snake_case_name stays; 2 * 3 = 6");
+ expect(plainPreviewText("use `code` here")).toBe("use code here");
+ expect(plainPreviewText("```js\nconst x = 1;\n```\nafter")).toBe("const x = 1; after");
+ });
+
+ it("drops a Setext heading's underline, which is no word of the line", () => {
+ expect(plainPreviewText("Report\n======\nthis week")).toBe("Report this week");
+ expect(plainPreviewText("Q3 plan\n===")).toBe("Q3 plan");
+ expect(plainPreviewText("Q3 plan\n---")).toBe("Q3 plan");
+ expect(plainPreviewText("Q3 plan\n--")).toBe("Q3 plan");
+ });
+
+ it("reads a spaced thematic break as the rule it is, not as a list of dashes", () => {
+ expect(plainPreviewText("- - -")).toBe("");
+ expect(plainPreviewText("before\n* * *\nafter")).toBe("before after");
+ expect(plainPreviewText("---")).toBe("");
+ });
+
+ it("keeps a list item whose text starts with a dash", () => {
+ expect(plainPreviewText("- -5 degrees outside")).toBe("-5 degrees outside");
+ });
+});
diff --git a/shared/plain-preview.ts b/shared/plain-preview.ts
index 5a97622cd3..690e36e123 100644
--- a/shared/plain-preview.ts
+++ b/shared/plain-preview.ts
@@ -24,9 +24,12 @@ export function plainPreviewText(text: string): string {
out = out.replace(/^[ \t]{0,3}#{1,6}[ \t]+/gm, "");
out = out.replace(/^[ \t]{0,3}#{1,6}[ \t]*$/gm, "");
out = out.replace(/^[ \t]*(?:>[ \t]?)+/gm, "");
+ // A rule, before a list marker can eat its first dash: `- - -` is one rule, not a list of two dashes.
+ out = out.replace(/^[ \t]{0,3}(?:[-*_][ \t]*){3,}$/gm, "");
+ // A Setext heading's underline (`Report` over `======`, `--` under it) is no word of the line.
+ out = out.replace(/^[ \t]{0,3}(?:=+|-+)[ \t]*$/gm, "");
out = out.replace(/^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]+(?:\[[ xX]\][ \t]+)?/gm, "");
out = out.replace(/^[ \t]*\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)*\|?[ \t]*$/gm, "");
- out = out.replace(/^[ \t]{0,3}(?:[-*_][ \t]*){3,}$/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) => {