mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 05:59:00 +08:00
feat(core): handle link and pubDate parameter
This commit is contained in:
@@ -26,10 +26,32 @@ module.exports = async (ctx, next) => {
|
|||||||
ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
|
ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
|
||||||
ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
|
ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
|
||||||
|
|
||||||
// handle description
|
// sort items
|
||||||
|
ctx.state.data.item = ctx.state.data.item.sort((a, b) => +new Date(b.pubDate || 0) - +new Date(a.pubDate || 0));
|
||||||
|
|
||||||
ctx.state.data.item.forEach((item) => {
|
ctx.state.data.item.forEach((item) => {
|
||||||
item.title && (item.title = he.decode(item.title + ''));
|
item.title && (item.title = he.decode(item.title + ''));
|
||||||
|
|
||||||
|
// handle pubDate
|
||||||
|
if (item.pubDate) {
|
||||||
|
item.pubDate = new Date(item.pubDate).toUTCString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle link
|
||||||
|
if (item.link) {
|
||||||
|
let baseUrl = ctx.state.data.link;
|
||||||
|
if (baseUrl && !baseUrl.match(/^https?:\/\//)) {
|
||||||
|
if (baseUrl.match(/^\/\//)) {
|
||||||
|
baseUrl = 'http:' + baseUrl;
|
||||||
|
} else {
|
||||||
|
baseUrl = 'http://' + baseUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
item.link = new URL(item.link, baseUrl).href;
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle description
|
||||||
if (item.description) {
|
if (item.description) {
|
||||||
const $ = cheerio.load(item.description);
|
const $ = cheerio.load(item.description);
|
||||||
let baseUrl = item.link || ctx.state.data.link;
|
let baseUrl = item.link || ctx.state.data.link;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module.exports = async (ctx) => {
|
|||||||
item.push({
|
item.push({
|
||||||
title: `Long Title `.repeat(10),
|
title: `Long Title `.repeat(10),
|
||||||
description: `Long Description `.repeat(10),
|
description: `Long Description `.repeat(10),
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
pubDate: new Date(`2019-3-1`).toUTCString(),
|
||||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||||
author: `DIYgod0`,
|
author: `DIYgod0`,
|
||||||
});
|
});
|
||||||
@@ -32,7 +32,7 @@ module.exports = async (ctx) => {
|
|||||||
item.push({
|
item.push({
|
||||||
title: 'Cache Title',
|
title: 'Cache Title',
|
||||||
description: description.text,
|
description: description.text,
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
pubDate: new Date(`2019-3-1`).toUTCString(),
|
||||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||||
author: `DIYgod0`,
|
author: `DIYgod0`,
|
||||||
});
|
});
|
||||||
@@ -49,15 +49,7 @@ module.exports = async (ctx) => {
|
|||||||
<img mock="/DIYgod/RSSHub.gif">
|
<img mock="/DIYgod/RSSHub.gif">
|
||||||
<img src="http://mock.com/DIYgod/DIYgod/RSSHub">
|
<img src="http://mock.com/DIYgod/DIYgod/RSSHub">
|
||||||
<img src="/DIYgod/RSSHub.jpg" onclick="alert(1);" onerror="alert(1);" onload="alert(1);">`,
|
<img src="/DIYgod/RSSHub.jpg" onclick="alert(1);" onerror="alert(1);" onload="alert(1);">`,
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
pubDate: new Date(`2019-3-1`).toUTCString(),
|
||||||
link: `mock.com/DIYgod/RSSHub`,
|
|
||||||
author: `DIYgod`,
|
|
||||||
});
|
|
||||||
item.push({
|
|
||||||
title: `Complicated Title`,
|
|
||||||
description: `<a href="/DIYgod/RSSHub"></a>
|
|
||||||
<img src="/DIYgod/RSSHub.jpg">`,
|
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
|
||||||
link: `//mock.com/DIYgod/RSSHub`,
|
link: `//mock.com/DIYgod/RSSHub`,
|
||||||
author: `DIYgod`,
|
author: `DIYgod`,
|
||||||
});
|
});
|
||||||
@@ -65,10 +57,40 @@ module.exports = async (ctx) => {
|
|||||||
title: `Complicated Title`,
|
title: `Complicated Title`,
|
||||||
description: `<a href="/DIYgod/RSSHub"></a>
|
description: `<a href="/DIYgod/RSSHub"></a>
|
||||||
<img src="/DIYgod/RSSHub.jpg">`,
|
<img src="/DIYgod/RSSHub.jpg">`,
|
||||||
pubDate: new Date(`2018-3-1`).toUTCString(),
|
pubDate: new Date(`2019-3-1`).toUTCString(),
|
||||||
link: `https://mock.com/DIYgod/RSSHub`,
|
link: `https://mock.com/DIYgod/RSSHub`,
|
||||||
author: `DIYgod`,
|
author: `DIYgod`,
|
||||||
});
|
});
|
||||||
|
} else if (ctx.params.id === 'sort') {
|
||||||
|
item.push({
|
||||||
|
title: `Sort Title 0`,
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/s1`,
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
item.push({
|
||||||
|
title: `Sort Title 1`,
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/s1`,
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
item.push({
|
||||||
|
title: `Sort Title 2`,
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/s2`,
|
||||||
|
pubDate: new Date(1546272000000 - 10 * 10 * 1000).toUTCString(),
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
item.push({
|
||||||
|
title: `Sort Title 3`,
|
||||||
|
link: `https://github.com/DIYgod/RSSHub/issues/s3`,
|
||||||
|
pubDate: new Date(1546272000000).toUTCString(),
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
|
} else if (ctx.params.id === 'mess') {
|
||||||
|
item.push({
|
||||||
|
title: `Mess Title`,
|
||||||
|
link: `/DIYgod/RSSHub/issues/0`,
|
||||||
|
pubDate: 1546272000000,
|
||||||
|
author: `DIYgod0`,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 1; i < 6; i++) {
|
for (let i = 1; i < 6; i++) {
|
||||||
|
|||||||
@@ -152,19 +152,39 @@ describe('complicated_description', () => {
|
|||||||
const response = await request.get('/test/complicated');
|
const response = await request.get('/test/complicated');
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
const parsed = await parser.parseString(response.text);
|
const parsed = await parser.parseString(response.text);
|
||||||
expect(parsed.items[0].content).toBe(`<a href="http://mock.com/DIYgod/RSSHub"></a>
|
expect(parsed.items[0].content).toBe(`<a href="https://mock.com/DIYgod/RSSHub"></a>
|
||||||
<img src="http://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">
|
<img src="https://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">
|
||||||
|
|
||||||
<a href="http://mock.com/DIYgod/RSSHub"></a>
|
<a href="http://mock.com/DIYgod/RSSHub"></a>
|
||||||
<img src="http://mock.com/DIYgod/RSSHub.jpg" data-src="/DIYgod/RSSHub0.jpg" referrerpolicy="no-referrer">
|
<img src="https://mock.com/DIYgod/RSSHub.jpg" data-src="/DIYgod/RSSHub0.jpg" referrerpolicy="no-referrer">
|
||||||
<img data-src="/DIYgod/RSSHub.jpg" src="http://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">
|
<img data-src="/DIYgod/RSSHub.jpg" src="https://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">
|
||||||
<img data-mock="/DIYgod/RSSHub.png" src="http://mock.com/DIYgod/RSSHub.png" referrerpolicy="no-referrer">
|
<img data-mock="/DIYgod/RSSHub.png" src="https://mock.com/DIYgod/RSSHub.png" referrerpolicy="no-referrer">
|
||||||
<img mock="/DIYgod/RSSHub.gif" src="http://mock.com/DIYgod/RSSHub.gif" referrerpolicy="no-referrer">
|
<img mock="/DIYgod/RSSHub.gif" src="https://mock.com/DIYgod/RSSHub.gif" referrerpolicy="no-referrer">
|
||||||
<img src="http://mock.com/DIYgod/DIYgod/RSSHub" referrerpolicy="no-referrer">
|
<img src="http://mock.com/DIYgod/DIYgod/RSSHub" referrerpolicy="no-referrer">
|
||||||
<img src="http://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
<img src="https://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
||||||
expect(parsed.items[1].content).toBe(`<a href="http://mock.com/DIYgod/RSSHub"></a>
|
expect(parsed.items[1].content).toBe(`<a href="https://mock.com/DIYgod/RSSHub"></a>
|
||||||
<img src="http://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
|
||||||
expect(parsed.items[2].content).toBe(`<a href="https://mock.com/DIYgod/RSSHub"></a>
|
|
||||||
<img src="https://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
<img src="https://mock.com/DIYgod/RSSHub.jpg" referrerpolicy="no-referrer">`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('sort', () => {
|
||||||
|
it(`sort`, async () => {
|
||||||
|
const response = await request.get('/test/sort');
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
const parsed = await parser.parseString(response.text);
|
||||||
|
expect(parsed.items[0].title).toBe('Sort Title 3');
|
||||||
|
expect(parsed.items[parsed.items.length - 3].title).toBe('Sort Title 2');
|
||||||
|
expect(parsed.items[parsed.items.length - 2].title).toBe('Sort Title 0');
|
||||||
|
expect(parsed.items[parsed.items.length - 1].title).toBe('Sort Title 1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('mess parameter', () => {
|
||||||
|
it(`date`, async () => {
|
||||||
|
const response = await request.get('/test/mess');
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
const parsed = await parser.parseString(response.text);
|
||||||
|
expect(parsed.items[0].pubDate).toBe('Mon, 31 Dec 2018 16:00:00 GMT');
|
||||||
|
expect(parsed.items[0].link).toBe('https://github.com/DIYgod/RSSHub/issues/0');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user