forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.ts
More file actions
44 lines (44 loc) · 1.85 KB
/
Copy pathmarkdown.ts
File metadata and controls
44 lines (44 loc) · 1.85 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
/**
* Strips common Markdown syntax down to plain, readable text.
*
* Used for BountyCard's `line-clamp-2` preview (#89): that context needs
* predictable, single-line-flowing inline text — rendering full Markdown
* there risks block-level elements (headings, code fences, lists) breaking
* a 2-line CSS clamp in ways plain text can't, and pulling react-markdown
* into the bundle for every card in every list view isn't justified just
* to show a two-line snippet. Full Markdown rendering lives in
* BountyDescription, used only on the (single, non-list) issue detail page.
*
* Deliberately not a spec-accurate GFM parser — good enough to turn
* "**bold** [text](url)" into "bold text" rather than showing a raw,
* mid-syntax markdown fragment truncated by the clamp.
*/
export function stripMarkdownToPlainText(markdown: string): string {
return markdown
// Fenced code blocks -> their inner content, unfenced.
.replace(/```[a-zA-Z0-9]*\n?([\s\S]*?)```/g, "$1")
// Images: drop entirely — alt text isn't meaningful in a short preview.
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
// Links: keep the link text, drop the URL.
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
// Inline code.
.replace(/`([^`]+)`/g, "$1")
// Bold+italic, then bold, then italic (order matters — longest marker first).
.replace(/(\*\*\*|___)([^*_]+)\1/g, "$2")
.replace(/(\*\*|__)([^*_]+)\1/g, "$2")
.replace(/(\*|_)([^*_]+)\1/g, "$2")
// Strikethrough.
.replace(/~~([^~]+)~~/g, "$1")
// Headings.
.replace(/^#{1,6}\s+/gm, "")
// Blockquotes.
.replace(/^>\s?/gm, "")
// List markers.
.replace(/^\s*[-*+]\s+/gm, "")
.replace(/^\s*\d+\.\s+/gm, "")
// Collapse newlines/whitespace into single spaces for inline flow.
.replace(/\r\n/g, "\n")
.replace(/\n+/g, " ")
.replace(/[ \t]{2,}/g, " ")
.trim();
}