-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_markdown.py
More file actions
33 lines (25 loc) · 1.08 KB
/
preview_markdown.py
File metadata and controls
33 lines (25 loc) · 1.08 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
#!/usr/bin/env python3
"""Shared markdown-safety helpers for generated Gmail preview text.
Preview/snippet text is treated as plain content, not intentional Markdown.
That means we prefer safe escaping of markdown-significant punctuation over
trying to preserve fancy inline formatting from arbitrary emails.
"""
from __future__ import annotations
import re
_INLINE_MARKERS_RE = re.compile(r'(?<!\\)([\*\[\]])')
_UNESCAPED_UNDERSCORE_RE = re.compile(r'(?<!\\)_')
_WHITESPACE_RE = re.compile(r'\s+')
def sanitize_markdown_preview(text: str) -> str:
"""Normalize preview text so generated Markdown stays lint-safe.
Rules:
- collapse newlines / repeated whitespace into a single space
- preserve text content, but escape markdown-significant inline markers
- stay idempotent so repeated fixer runs are harmless
"""
if not text:
return ''
text = text.replace('\r', ' ').replace('\n', ' ')
text = _WHITESPACE_RE.sub(' ', text).strip()
text = _UNESCAPED_UNDERSCORE_RE.sub(r'\\_', text)
text = _INLINE_MARKERS_RE.sub(r'\\\1', text)
return text