mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-26 08:31:22 +01:00
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.
This commit is contained in:
@@ -5,6 +5,7 @@ const mime = require('mime');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const validator = require('validator');
|
const validator = require('validator');
|
||||||
const sanitize = require('sanitize-html');
|
const sanitize = require('sanitize-html');
|
||||||
|
const tokenizer = require('sbd');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
@@ -756,7 +757,17 @@ Mocks.notes.public = async (post) => {
|
|||||||
attachment: normalizeAttachment(noteAttachment),
|
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');
|
let context = await posts.getPostField(post.pid, 'context');
|
||||||
|
|||||||
Reference in New Issue
Block a user