fix: use sanitize-html (#14312)

* fix: use sanitize-html

* test: add brief test
This commit is contained in:
Tony 2024-01-23 18:40:11 +00:00 committed by GitHub
parent 839d36ef76
commit d1b3e161dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -8,6 +8,7 @@ const md = require('markdown-it')({
html: true,
});
const htmlToText = require('html-to-text');
const sanitizeHtml = require('sanitize-html');
let mercury_parser;
@ -353,7 +354,7 @@ module.exports = async (ctx, next) => {
for (const item of ctx.state.data.item) {
let text;
if (item.description) {
text = item.description.replaceAll(/<\/?[^>]+(>|$)/g, '');
text = sanitizeHtml(item.description, { allowedTags: [], allowedAttributes: {} });
}
if (text?.length) {
item.description = text.length > ctx.query.brief ? `<p>${text.substring(0, ctx.query.brief)}…</p>` : `<p>${text}</p>`;

View File

@ -223,6 +223,17 @@ module.exports = async (ctx) => {
break;
case 'brief':
item.push({
title: '小可愛',
description: '<p>宇宙無敵</p><br>'.repeat(1000),
link: `/DIYgod/RSSHub/issues/0`,
pubDate: new Date(1_546_272_000_000).toUTCString(),
author: `DIYgod0`,
});
break;
case 'json':
item.push(
{

View File

@ -406,6 +406,17 @@ describe('opencc', () => {
});
});
describe('brief', () => {
it(`brief`, async () => {
const response = await request.get('/test/brief?brief=100');
const parsed = await parser.parseString(response.text);
expect(parsed.items[0].title).toBe('小可愛');
expect(parsed.items[0].content).toBe(
'<p>宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵宇宙無敵…</p>'
);
});
});
describe('multi parameter', () => {
it(`filter before limit`, async () => {
const response = await request.get('/test/filter-limit?filterout_title=2&limit=2');