From 35641f377ca20882d43123241bef839641c68df9 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 28 Aug 2025 14:27:41 -0400 Subject: [PATCH] feat: use sbd to more intelligently put together a sub-500 character summary based on existing sentences in post content The original behaviour was to just shove the entire post content (html and all) into summary. Summary _can_ include HTML, but it's a little harder to retain HTML but truncate the content based on sentences, without accidentally dropping tags. --- src/activitypub/mocks.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js index 2f65471dee..b3c641e0c3 100644 --- a/src/activitypub/mocks.js +++ b/src/activitypub/mocks.js @@ -5,6 +5,7 @@ const mime = require('mime'); const path = require('path'); const validator = require('validator'); const sanitize = require('sanitize-html'); +const tokenizer = require('sbd'); const db = require('../database'); const user = require('../user'); @@ -756,7 +757,17 @@ Mocks.notes.public = async (post) => { attachment: normalizeAttachment(noteAttachment), }; - summary = post.content; + const sentences = tokenizer.sentences(post.content, { sanitize: true }); + // Append sentences to summary until it contains just under 500 characters of content + const limit = 500; + summary = sentences.reduce((memo, sentence) => { + const remaining = limit - memo.length; + if (sentence.length < remaining) { + memo += ` ${sentence}`; + } + + return memo; + }, ''); } let context = await posts.getPostField(post.pid, 'context');