~james/core

core/web/src/lib/markup/text.test.ts -rw-r--r-- 1.9 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { describe, expect, it } from "vitest";
import { markdownToText, summarize } from "#lib/markup/text.ts";

describe("markdownToText", () => {
	it("keeps the label of a link", () => {
		expect(
			markdownToText("Fixes [#4656](https://tangled.org/tangled.org/core/issues/4656).")
		).toBe("Fixes #4656.");
	});

	it("drops markup around text", () => {
		expect(markdownToText("# Title\n\n**bold** and `code`\n\n- one\n- two")).toBe(
			"Title bold and code one two"
		);
	});

	it("keeps the alt text of an image and drops raw html", () => {
		expect(markdownToText("![a cat](cat.png) <br> done")).toBe("a cat done");
	});

	it("renders emoji shortcodes", () => {
		expect(markdownToText("ship it :rocket:")).toBe("ship it 🚀");
	});
});

describe("summarize", () => {
	it("cuts long text with an ellipsis", () => {
		expect(summarize("word ".repeat(100), 20)).toBe("word word word word…");
	});

	it("keeps short text whole", () => {
		expect(summarize("short", 20)).toBe("short");
	});

	it("ends in an ellipsis when the body is cut before its end", () => {
		expect(summarize(`short\n\n${"filler ".repeat(1000)}`, 500)).toMatch(/…$/);
	});

	it("reaches the text after a long html comment", () => {
		const body = `<!-- ${"template instructions ".repeat(300)} -->\n\nThe sidebar overlaps.`;
		expect(summarize(body)).toBe("The sidebar overlaps.");
	});

	it("skips link definitions but keeps footnote-like text", () => {
		const defs = Array.from(
			{ length: 80 },
			(_, i) => `[r${i}]: https://example.com/${"p/".repeat(30)}`
		);
		expect(summarize(`${defs.join("\n")}\n\nSee [r1] and [^1]: stays.`)).toBe(
			"See [r1] and [^1]: stays."
		);
	});

	it("leaves an unclosed comment as it is and stays linear on many of them", () => {
		expect(summarize("a <!-- b")).toBe("a <!-- b");
		const started = performance.now();
		summarize("<!--".repeat(60_000));
		expect(performance.now() - started).toBeLessThan(500);
	});
});