Skip to content

Commit

Permalink
fix: use sanitize-html (#14312)
Browse files Browse the repository at this point in the history
* fix: use sanitize-html

* test: add brief test
  • Loading branch information
TonyRL authored Jan 23, 2024
1 parent 839d36e commit d1b3e16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/middleware/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>`;
Expand Down
11 changes: 11 additions & 0 deletions lib/v2/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
11 changes: 11 additions & 0 deletions test/middleware/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit d1b3e16

Please sign in to comment.