-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip.test.ts
More file actions
206 lines (159 loc) · 5.83 KB
/
strip.test.ts
File metadata and controls
206 lines (159 loc) · 5.83 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { describe, expect, it } from "vitest";
import { stripMarkdown } from "./strip.js";
describe("stripMarkdown", () => {
describe("edge cases", () => {
it("returns empty string as-is", () => {
expect(stripMarkdown("")).toBe("");
});
it("returns plain text unchanged", () => {
expect(stripMarkdown("Just some normal text")).toBe("Just some normal text");
});
it("preserves emojis", () => {
expect(stripMarkdown("Hello 🌍🎉")).toBe("Hello 🌍🎉");
});
it("preserves newlines", () => {
expect(stripMarkdown("Line 1\n\nLine 2")).toBe("Line 1\n\nLine 2");
});
});
describe("inline formatting", () => {
it("strips bold **text**", () => {
expect(stripMarkdown("This is **bold** text")).toBe("This is bold text");
});
it("strips bold __text__", () => {
expect(stripMarkdown("This is __bold__ text")).toBe("This is bold text");
});
it("strips italic *text*", () => {
expect(stripMarkdown("This is *italic* text")).toBe("This is italic text");
});
it("strips italic _text_", () => {
expect(stripMarkdown("This is _italic_ text")).toBe("This is italic text");
});
it("strips bold+italic ***text***", () => {
expect(stripMarkdown("This is ***bold italic*** text")).toBe(
"This is bold italic text",
);
});
it("strips strikethrough ~~text~~", () => {
expect(stripMarkdown("This is ~~deleted~~ text")).toBe("This is deleted text");
});
it("strips inline code", () => {
expect(stripMarkdown("Run `npm install` now")).toBe("Run npm install now");
});
it("does not strip mid-word underscores (snake_case)", () => {
expect(stripMarkdown("use my_variable_name here")).toBe(
"use my_variable_name here",
);
});
it("handles nested bold inside italic", () => {
expect(stripMarkdown("This is ***nested*** text")).toBe("This is nested text");
});
});
describe("links and images", () => {
it("strips links, keeps text", () => {
expect(stripMarkdown("[OpenClaw](https://openclaw.ai)")).toBe("OpenClaw");
});
it("strips images, keeps alt text", () => {
expect(stripMarkdown("")).toBe("Screenshot");
});
it("strips reference-style links", () => {
expect(stripMarkdown("[OpenClaw][1]")).toBe("OpenClaw");
});
it("removes reference definitions", () => {
expect(stripMarkdown("[1]: https://openclaw.ai")).toBe("");
});
});
describe("block elements", () => {
it("strips h1-h6 headers", () => {
expect(stripMarkdown("# Title")).toBe("Title");
expect(stripMarkdown("## Subtitle")).toBe("Subtitle");
expect(stripMarkdown("### Section")).toBe("Section");
expect(stripMarkdown("###### Deep")).toBe("Deep");
});
it("strips blockquotes", () => {
expect(stripMarkdown("> This is a quote")).toBe("This is a quote");
});
it("strips nested blockquotes", () => {
expect(stripMarkdown(">> Nested quote")).toBe("Nested quote");
});
it("strips horizontal rules", () => {
expect(stripMarkdown("---")).toBe("");
expect(stripMarkdown("***")).toBe("");
expect(stripMarkdown("___")).toBe("");
});
it("strips fenced code blocks, keeps content", () => {
const input = "```javascript\nconst x = 1;\n```";
expect(stripMarkdown(input)).toBe("const x = 1;");
});
it("strips fenced code blocks without language", () => {
const input = "```\nhello world\n```";
expect(stripMarkdown(input)).toBe("hello world");
});
});
describe("lists", () => {
it("strips unordered list markers with -", () => {
expect(stripMarkdown("- Item one\n- Item two")).toBe("Item one\nItem two");
});
it("strips unordered list markers with *", () => {
expect(stripMarkdown("* Item one\n* Item two")).toBe("Item one\nItem two");
});
it("strips unordered list markers with +", () => {
expect(stripMarkdown("+ Item one")).toBe("Item one");
});
it("preserves ordered list numbers", () => {
expect(stripMarkdown("1. First\n2. Second")).toBe("1. First\n2. Second");
});
it("strips task list checkboxes", () => {
expect(stripMarkdown("[ ] Todo\n[x] Done")).toBe("Todo\nDone");
});
it("preserves indentation for nested lists", () => {
expect(stripMarkdown("Top\n - Nested item\n - Another")).toBe(
"Top\n Nested item\n Another",
);
});
});
describe("escaped characters", () => {
it("removes backslash escapes", () => {
expect(stripMarkdown("Price: \\$10")).toBe("Price: $10");
expect(stripMarkdown("Not \\*bold\\*")).toBe("Not *bold*");
});
});
describe("HTML comments", () => {
it("removes HTML comments", () => {
expect(stripMarkdown("Text <!-- hidden --> more")).toBe("Text more");
});
});
describe("whitespace cleanup", () => {
it("collapses 3+ blank lines to 2", () => {
expect(stripMarkdown("A\n\n\n\nB")).toBe("A\n\nB");
});
it("trims leading/trailing whitespace", () => {
expect(stripMarkdown(" hello \n\n")).toBe("hello");
});
});
describe("real-world LLM output", () => {
it("handles a typical Claude morning briefing", () => {
const input = `### Morgenbriefing
**Wetter:** 12°C, bewölkt, kein Regen erwartet.
**Termine:**
- 10:00 Standup
- 14:00 1:1 mit Lisa
**E-Mails:**
- _3 ungelesene_ in iCloud
- **1 wichtige** von HR
> Zitat des Tages: "The best way to predict the future is to invent it."
---
Guten Morgen! ☀️`;
const expected = `Morgenbriefing
Wetter: 12°C, bewölkt, kein Regen erwartet.
Termine:
10:00 Standup
14:00 1:1 mit Lisa
E-Mails:
3 ungelesene in iCloud
1 wichtige von HR
Zitat des Tages: "The best way to predict the future is to invent it."
Guten Morgen! ☀️`;
expect(stripMarkdown(input)).toBe(expected);
});
});
});