Parse HTML Message Previews on the Backend
- Deciders: Qudsia Mehmood, Julian Bäume
- Date: 2026-05-12
Status
Accepted
Context
The rich-text editor in feedback and new-thread dialogs produces HTML, which is written to threads.message. That column powers the thread list preview, so the value needs to be plain text — HTML tags and entities in a one-line preview are visible noise to the user.
Commit bdbad4b moved the strip step from the browser to the backend so the work happens once at write time instead of on every list render. The first backend implementation used a small hand-rolled regex parser (htmlToPlainText in src/routes/api/threads/threads.ts). On review the regex was found to be fragile in three ways:
/<[^>]*>/gmis-detects the end of a tag when>appears inside an attribute value (e.g.<a title="a>b">), dropping subsequent content.- Only six named entities are decoded. Curly quotes, em-dashes,
…, numeric (’) and hex (—) entities leak into previews as literal escape sequences. <script>/<style>content is not removed; the rich-text editor does not produce these today, but the parser would happily include CSS bodies if it ever did.
Decision
Use the html-to-text library to convert the message body to a single-line preview on the backend.
- Input that does not contain
<is treated as plain text and short-circuits the parser. This avoids the well-known HTML-parser-eats-plain-text class of bugs ("2 < 3"→"2", multi-line text losing newlines, etc.). - For HTML input,
html-to-textis configured withwordwrap: false,selectors: [{ selector: 'img', format: 'skip' }, { selector: 'a', options: { ignoreHref: true } }], then whitespace is collapsed and trimmed. - The original HTML body still lives in
actions.data.messagefor the comment view, so rich rendering is unaffected.
Consequences
Positive
- Correct handling of all HTML entities (named, numeric, hex).
- Robust against attribute values containing
>, comments, scripts, and malformed HTML. - Plain-text messages pass through untouched — no risk of mangling.
- The conversion logic is a documented dependency rather than ad-hoc regex.
Negative
- Adds one runtime dependency (
html-to-text, ~50KB). - Behavior changes are pinned to an external library — entity tables and whitespace rules now live there, not in the codebase.
References
html-to-texton GitHub- Commit
bdbad4b— original regex-based implementation

