forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.test.ts
More file actions
83 lines (71 loc) · 3.16 KB
/
Copy pathmarkdown.test.ts
File metadata and controls
83 lines (71 loc) · 3.16 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Tests for stripMarkdownToPlainText (#89).
*
* Used by BountyCard's line-clamp-2 preview so a markdown-containing
* description doesn't show a truncated fragment of literal, mid-syntax
* markdown (e.g. "Use `**config" cut mid-token).
*/
import { stripMarkdownToPlainText } from "./markdown";
describe("stripMarkdownToPlainText", () => {
it("strips bold, italic, and inline code", () => {
expect(stripMarkdownToPlainText("Use **bold**, *italic*, and `code`.")).toBe(
"Use bold, italic, and code.",
);
});
it("keeps link text and drops the URL", () => {
expect(
stripMarkdownToPlainText("See [the docs](https://example.com/docs) for details."),
).toBe("See the docs for details.");
});
it("drops images entirely", () => {
expect(stripMarkdownToPlainText("Before  after")).toBe(
"Before after",
);
});
it("unwraps fenced code blocks to their inner content", () => {
const input = "Repro:\n```js\nconst x = 1;\n```\nThanks";
expect(stripMarkdownToPlainText(input)).toBe("Repro: const x = 1; Thanks");
});
it("strips heading markers", () => {
expect(stripMarkdownToPlainText("## Steps to reproduce")).toBe("Steps to reproduce");
});
it("strips list markers from bullet and numbered lists", () => {
const input = "- first\n- second\n1. one\n2. two";
expect(stripMarkdownToPlainText(input)).toBe("first second one two");
});
it("strips blockquote markers", () => {
expect(stripMarkdownToPlainText("> quoted text")).toBe("quoted text");
});
it("strips strikethrough", () => {
expect(stripMarkdownToPlainText("~~deprecated~~ replaced")).toBe("deprecated replaced");
});
it("produces clean plain text for a description with markdown near the truncation boundary", () => {
// Simulates BountyCard's line-clamp-2 scenario: the raw markdown, if
// shown verbatim and cut off by the CSS clamp, would end mid-syntax
// (e.g. "...crashes when calling `getBalance" with an unclosed
// backtick). The stripped plain text has no such artifact anywhere in
// it, so truncating it at any point is always safe.
const input =
"The `getBalance()` method throws when the account is unfunded — see " +
"[issue #12](https://github.com/org/repo/issues/12) for the original report.";
const result = stripMarkdownToPlainText(input);
// No markdown syntax markers survive — backticks, asterisks/underscores
// for emphasis, or link-bracket syntax. (Literal parentheses from
// "getBalance()" in the source prose are expected to remain — they're
// real content, not markdown syntax.)
expect(result).not.toMatch(/[`*_[\]]/);
expect(result).toBe(
"The getBalance() method throws when the account is unfunded — see issue #12 for the original report.",
);
});
it("passes plain text through unchanged", () => {
expect(stripMarkdownToPlainText("Just a normal sentence.")).toBe(
"Just a normal sentence.",
);
});
it("collapses newlines into single spaces for inline flow", () => {
expect(stripMarkdownToPlainText("Line one\n\nLine two\nLine three")).toBe(
"Line one Line two Line three",
);
});
});