feat: add content_preview helper and apply rendered content across templates

Introduce a new `content_preview()` function in `rendering.py` that strips HTML from rendered content and truncates to a given length with an ellipsis. Add a corresponding `plainText()` and `preview()` method to the `ContentRenderer` JS class. Register `content_preview` as a Jinja2 global in `templating.py`. Update multiple templates (`feed.html`, `gists.html`, `landing.html`, `messages.html`, `news.html`, `notifications.html`, `post.html`, `profile.html`) to use `render_title`, `render_content`, or `content_preview` instead of raw string slicing, ensuring consistent rendering of rich text (e.g., Markdown, HTML) in previews, descriptions, bios, and notification messages. Also fix the conversation preview in `MessagesLayout.js` to use the new JS preview method.
This commit is contained in:
2026-06-22 21:17:54 +00:00
parent e0e64c8d9e
commit c9a4a4b280
12 changed files with 46 additions and 15 deletions
+19
View File
@@ -43,6 +43,25 @@ export class ContentRenderer {
return html;
}
plainText(text) {
if (!text) return "";
let html;
try {
html = this.render(text);
} catch (err) {
return String(text).replace(/\s+/g, " ").trim();
}
const div = document.createElement("div");
div.innerHTML = html;
return (div.textContent || "").replace(/\s+/g, " ").trim();
}
preview(text, length = 60) {
const plain = this.plainText(text);
if (plain.length <= length) return plain;
return plain.slice(0, length).replace(/\s+$/, "") + "...";
}
escapeHtml(text) {
const div = document.createElement("div");
div.textContent = text;
+1 -1
View File
@@ -457,7 +457,7 @@ export class MessagesLayout {
const item = document.querySelector(`.conversation-item[data-conv-uid="${partnerUid}"]`);
if (!item) return;
const preview = item.querySelector(".conversation-preview");
if (preview) preview.textContent = frame.content.slice(0, 60);
if (preview) preview.textContent = contentRenderer.preview(frame.content, 60);
const dot = item.querySelector(".conversation-unread-dot");
if (dot) {
dot.hidden = mine || partnerUid === this.otherUid;