pter 0.1.0

Plain Text Email Renderer — convert HTML email bodies into readable markdown
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use pter::convert;

#[test]
fn simple_email() {
    let html = r#"
    <html>
    <head><title>Email</title></head>
    <body>
        <h1>Meeting Tomorrow</h1>
        <p>Hi Max,</p>
        <p>Just confirming our meeting tomorrow at <strong>2pm</strong>.</p>
        <p>Best,<br>Alice</p>
    </body>
    </html>
    "#;

    let md = convert(html);
    assert!(md.contains("# Meeting Tomorrow"));
    assert!(md.contains("Hi Max,"));
    assert!(md.contains("**2pm**"));
    assert!(md.contains("Best,\nAlice"));
}

#[test]
fn email_with_links() {
    let html = r#"
    <body>
        <p>Please review the <a href="https://example.com/doc">document</a>.</p>
        <p>Direct link: <a href="https://example.com">https://example.com</a></p>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("[document](https://example.com/doc)"));
    // Link text matches URL — no markdown link syntax
    assert!(md.contains("Direct link: https://example.com"));
}

#[test]
fn email_with_tracking_pixels() {
    let html = r#"
    <body>
        <p>Content here</p>
        <img src="https://tracker.example.com/open.gif" width="1" height="1" alt="">
        <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">
        <img src="real-image.jpg" alt="A real photo" width="600">
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Content here"));
    assert!(!md.contains("tracker"));
    assert!(!md.contains("data:image"));
    assert!(md.contains("![A real photo](real-image.jpg)"));
}

#[test]
fn email_with_quoted_reply() {
    let html = r#"
    <body>
        <p>Thanks, that works for me.</p>
        <blockquote>
            <p>Can we meet at 3pm instead?</p>
        </blockquote>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Thanks, that works for me."));
    assert!(md.contains("> Can we meet at 3pm instead?"));
}

#[test]
fn email_with_signature_line() {
    let html = r#"
    <body>
        <p>See you then.</p>
        <hr>
        <p>Alice Smith</p>
        <p>Engineering Lead</p>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("See you then."));
    assert!(md.contains("---"));
    assert!(md.contains("Alice Smith"));
}

#[test]
fn deeply_nested_blockquotes() {
    let html = r#"
    <body>
        <p>Got it.</p>
        <blockquote>
            <p>Sounds good.</p>
            <blockquote>
                <p>Can we reschedule?</p>
                <blockquote>
                    <p>Original message here.</p>
                </blockquote>
            </blockquote>
        </blockquote>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Got it."));
    assert!(md.contains("> Sounds good."));
    assert!(md.contains("> > Can we reschedule?"));
    assert!(md.contains("> > > Original message here."));
}

#[test]
fn complex_list_structure() {
    let html = r#"
    <body>
        <p>Action items:</p>
        <ol>
            <li>Review the PR
                <ul>
                    <li>Check tests</li>
                    <li>Check docs</li>
                </ul>
            </li>
            <li>Deploy to staging</li>
        </ol>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Action items:"));
    assert!(md.contains("1. Review the PR"));
    assert!(md.contains("  - Check tests"));
    assert!(md.contains("2. Deploy to staging"));
}

#[test]
fn pre_block_preserves_formatting() {
    let html = r#"
    <body>
        <p>Here's the code:</p>
        <pre><code>fn main() {
    println!("hello");
}</code></pre>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Here's the code:"));
    assert!(md.contains("```\nfn main()"));
    assert!(md.contains("    println!"));
}

#[test]
fn hidden_content_stripped() {
    let html = r#"
    <body>
        <p>Visible content</p>
        <div style="display: none;">
            <p>This should not appear</p>
        </div>
        <span style="visibility: hidden;">Also hidden</span>
        <p>More visible</p>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Visible content"));
    assert!(!md.contains("should not appear"));
    assert!(!md.contains("Also hidden"));
    assert!(md.contains("More visible"));
}

#[test]
fn script_and_style_fully_removed() {
    let html = r#"
    <html>
    <head>
        <style>body { color: red; }</style>
        <script>alert('xss');</script>
    </head>
    <body>
        <p>Safe content</p>
        <script>document.write('injected')</script>
    </body>
    </html>
    "#;

    let md = convert(html);
    assert_eq!(md, "Safe content");
}

#[test]
fn newsletter_table_layout() {
    // Typical email newsletter wrapped in layout tables
    let html = r#"
    <html>
    <body>
    <table width="100%" cellpadding="0" cellspacing="0" role="presentation">
        <tr>
            <td align="center">
                <table width="600" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <h2>Weekly Digest</h2>
                            <p>Here are your updates for this week.</p>
                            <ul>
                                <li>New release v2.0</li>
                                <li>Bug fixes</li>
                            </ul>
                            <p>Thanks for reading!</p>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <img src="https://track.example.com/open.gif" width="1" height="1">
    </body>
    </html>
    "#;

    let md = convert(html);
    assert!(md.contains("## Weekly Digest"));
    assert!(md.contains("Here are your updates for this week."));
    assert!(md.contains("- New release v2.0"));
    assert!(md.contains("- Bug fixes"));
    assert!(md.contains("Thanks for reading!"));
    assert!(!md.contains("track.example.com"));
    // No table markup in output
    assert!(!md.contains("| "));
}

#[test]
fn data_table_preserved() {
    let html = r#"
    <body>
        <p>Order summary:</p>
        <table>
            <thead><tr><th>Item</th><th>Qty</th><th>Price</th></tr></thead>
            <tbody>
                <tr><td>Widget</td><td>3</td><td>$15.00</td></tr>
                <tr><td>Gadget</td><td>1</td><td>$29.99</td></tr>
            </tbody>
        </table>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Order summary:"));
    assert!(md.contains("| Item | Qty | Price |"));
    assert!(md.contains("| --- | --- | --- |"));
    assert!(md.contains("| Widget | 3 | $15.00 |"));
    assert!(md.contains("| Gadget | 1 | $29.99 |"));
}

#[test]
fn spacer_and_tracking_stripped() {
    let html = r#"
    <body>
        <p>Real content</p>
        <div style="font-size: 0; line-height: 0;">&nbsp;</div>
        <img src="pixel.gif" width="1" height="1" style="display:none">
        <div style="height:0;overflow:hidden">invisible</div>
        <p>More content</p>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Real content"));
    assert!(md.contains("More content"));
    assert!(!md.contains("invisible"));
    assert!(!md.contains("pixel.gif"));
}

// -- Reply chain tests --

#[test]
fn gmail_reply_chain() {
    let html = r#"
    <body>
        <div dir="ltr">
            <p>Thanks, that works for me.</p>
        </div>
        <div class="gmail_quote">
            <div class="gmail_attr">On Mon, Jan 5, 2026 at 3:00 PM Alice &lt;alice@example.com&gt; wrote:</div>
            <blockquote class="gmail_quote">
                <div dir="ltr">
                    <p>Can we meet at 3pm instead of 2pm?</p>
                </div>
            </blockquote>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Thanks, that works for me."));
    // The gmail_quote div should be rendered as a quote block
    assert!(md.contains("> "));
    assert!(md.contains("3pm instead of 2pm"));
}

#[test]
fn apple_mail_reply() {
    let html = r#"
    <body>
        <div>Sounds good, see you then.</div>
        <div>
            <br>
            <blockquote type="cite">
                <div>Hey, are we still on for lunch?</div>
            </blockquote>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Sounds good, see you then."));
    assert!(md.contains("> "));
    assert!(md.contains("still on for lunch"));
}

#[test]
fn outlook_reply_with_separator() {
    let html = r#"
    <body>
        <div>
            <p>I'll handle it.</p>
        </div>
        <hr>
        <div>
            <p>From: Alice Smith<br>
            Sent: Monday, January 5, 2026<br>
            To: Bob Jones<br>
            Subject: Action needed</p>
        </div>
        <div>
            <p>Can you take a look at the report?</p>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("I'll handle it."));
    assert!(md.contains("---")); // hr separator
    assert!(md.contains("From: Alice Smith"));
    assert!(md.contains("take a look at the report"));
}

#[test]
fn nested_gmail_reply_chain() {
    let html = r#"
    <body>
        <div dir="ltr"><p>Got it, thanks!</p></div>
        <div class="gmail_quote">
            On Tue, Jan 6, Bob wrote:
            <blockquote class="gmail_quote">
                <div dir="ltr"><p>Here's the update.</p></div>
                <div class="gmail_quote">
                    On Mon, Jan 5, Alice wrote:
                    <blockquote class="gmail_quote">
                        <div dir="ltr"><p>What's the status?</p></div>
                    </blockquote>
                </div>
            </blockquote>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Got it, thanks!"));
    // Should have nested quoting
    assert!(md.contains("> "));
    assert!(md.contains("Here's the update."));
    assert!(md.contains("What's the status?"));
}

#[test]
fn forwarded_message() {
    let html = r#"
    <body>
        <div><p>FYI, see below.</p></div>
        <div class="gmail_quote">
            ---------- Forwarded message ----------
            <blockquote>
                <p>From: Alice</p>
                <p>The deadline has been moved to Friday.</p>
            </blockquote>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("FYI, see below."));
    assert!(md.contains("Forwarded message"));
    assert!(md.contains("deadline has been moved"));
}

#[test]
fn protonmail_reply() {
    let html = r#"
    <body>
        <div>Will do, thanks.</div>
        <blockquote class="protonmail_quote" type="cite">
            <div>Please send me the files by EOD.</div>
        </blockquote>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Will do, thanks."));
    assert!(md.contains("> "));
    assert!(md.contains("send me the files"));
}

#[test]
fn attribution_preserved_above_quote() {
    let html = r#"
    <body>
        <p>Agreed.</p>
        <div class="gmail_quote">
            On Wed, Jan 7, 2026 at 10:00 AM Carol wrote:
            <blockquote>
                <p>Let's go with option B.</p>
            </blockquote>
        </div>
    </body>
    "#;

    let md = convert(html);
    assert!(md.contains("Agreed."));
    // Attribution should appear
    assert!(md.contains("Carol wrote:"));
    assert!(md.contains("option B"));
}