mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-06 13:08:14 +08:00
fix(core): fix category filtering (#11451)
This commit is contained in:
@@ -181,7 +181,7 @@ module.exports = async (ctx, next) => {
|
||||
const title = item.title || '';
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
const category = item.category || [];
|
||||
const category = item.category ? (Array.isArray(item.category) ? item.category : [item.category]) : [];
|
||||
const isFilter =
|
||||
title.match(makeRegex(ctx.query.filter)) || description.match(makeRegex(ctx.query.filter)) || author.match(makeRegex(ctx.query.filter)) || category.some((c) => c.match(makeRegex(ctx.query.filter)));
|
||||
return isFilter;
|
||||
@@ -194,7 +194,7 @@ module.exports = async (ctx, next) => {
|
||||
const title = item.title || '';
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
const category = item.category || [];
|
||||
const category = item.category ? (Array.isArray(item.category) ? item.category : [item.category]) : [];
|
||||
let isFilter = true;
|
||||
ctx.query.filter_title && (isFilter = title.match(makeRegex(ctx.query.filter_title)));
|
||||
ctx.query.filter_description && (isFilter = isFilter && description.match(makeRegex(ctx.query.filter_description)));
|
||||
@@ -213,7 +213,7 @@ module.exports = async (ctx, next) => {
|
||||
const title = item.title;
|
||||
const description = item.description || title;
|
||||
const author = item.author || '';
|
||||
const category = item.category || [];
|
||||
const category = item.category ? (Array.isArray(item.category) ? item.category : [item.category]) : [];
|
||||
let isFilter = true;
|
||||
ctx.query.filterout_title && (isFilter = !title.match(makeRegex(ctx.query.filterout_title)));
|
||||
ctx.query.filterout_description && (isFilter = isFilter && !description.match(makeRegex(ctx.query.filterout_description)));
|
||||
|
||||
@@ -33,6 +33,14 @@ module.exports = async (ctx) => {
|
||||
author: `DIYgod0`,
|
||||
category: ['Category0', 'Category1', 'Category2'],
|
||||
},
|
||||
{
|
||||
title: 'Filter Title3',
|
||||
description: 'Description3',
|
||||
pubDate: new Date(`2019-3-1`).toUTCString(),
|
||||
link: `https://github.com/DIYgod/RSSHub/issues/1`,
|
||||
author: `DIYgod0`,
|
||||
category: 'Category3',
|
||||
},
|
||||
];
|
||||
} else if (ctx.params.id === 'long') {
|
||||
item.push({
|
||||
|
||||
@@ -105,6 +105,13 @@ describe('filter', () => {
|
||||
expect(parsed.items[1].title).toBe('Filter Title2');
|
||||
});
|
||||
|
||||
it(`filter_category filter_case_sensitive=false category string`, async () => {
|
||||
const response = await request.get('/test/filter?filter_category=category3&filter_case_sensitive=false');
|
||||
const parsed = await parser.parseString(response.text);
|
||||
expect(parsed.items.length).toBe(1);
|
||||
expect(parsed.items[0].title).toBe('Filter Title3');
|
||||
});
|
||||
|
||||
it(`filter_time`, async () => {
|
||||
const response = await request.get('/test/current_time?filter_time=25');
|
||||
const parsed = await parser.parseString(response.text);
|
||||
@@ -210,28 +217,29 @@ describe('filter', () => {
|
||||
it(`filterout_category`, async () => {
|
||||
const response = await request.get('/test/filter?filterout_category=Category0|Category1');
|
||||
const parsed = await parser.parseString(response.text);
|
||||
expect(parsed.items.length).toBe(5);
|
||||
expect(parsed.items[0].title).toBe('Title1');
|
||||
expect(parsed.items[1].title).toBe('Title2');
|
||||
expect(parsed.items[2].title).toBe('Title3');
|
||||
expect(parsed.items.length).toBe(6);
|
||||
expect(parsed.items[0].title).toBe('Filter Title3');
|
||||
expect(parsed.items[1].title).toBe('Title1');
|
||||
expect(parsed.items[2].title).toBe('Title2');
|
||||
});
|
||||
|
||||
it(`filterout_category filter_case_sensitive default`, async () => {
|
||||
const response = await request.get('/test/filter?filterout_category=category0|category1');
|
||||
const parsed = await parser.parseString(response.text);
|
||||
expect(parsed.items.length).toBe(7);
|
||||
expect(parsed.items.length).toBe(8);
|
||||
expect(parsed.items[0].title).toBe('Filter Title1');
|
||||
expect(parsed.items[1].title).toBe('Filter Title2');
|
||||
expect(parsed.items[2].title).toBe('Title1');
|
||||
expect(parsed.items[2].title).toBe('Filter Title3');
|
||||
expect(parsed.items[3].title).toBe('Title1');
|
||||
});
|
||||
|
||||
it(`filterout_category filter_case_sensitive=false`, async () => {
|
||||
const response = await request.get('/test/filter?filterout_category=category0|category1&filter_case_sensitive=false');
|
||||
const parsed = await parser.parseString(response.text);
|
||||
expect(parsed.items.length).toBe(5);
|
||||
expect(parsed.items[0].title).toBe('Title1');
|
||||
expect(parsed.items[1].title).toBe('Title2');
|
||||
expect(parsed.items[2].title).toBe('Title3');
|
||||
expect(parsed.items.length).toBe(6);
|
||||
expect(parsed.items[0].title).toBe('Filter Title3');
|
||||
expect(parsed.items[1].title).toBe('Title1');
|
||||
expect(parsed.items[2].title).toBe('Title2');
|
||||
});
|
||||
|
||||
it(`filter combination`, async () => {
|
||||
|
||||
Reference in New Issue
Block a user