From b2355436cc8b9afc6fc3228329f23761f1f1abd5 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 28 Aug 2019 15:50:05 +0800 Subject: [PATCH] fix: url base --- lib/middleware/parameter.js | 12 ++++++++++-- lib/routes/test/index.js | 29 +++++++++++++++++++++++------ test/middleware/parameter.js | 22 ++++++++++++++-------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/middleware/parameter.js b/lib/middleware/parameter.js index 89f086601d..863e86286d 100644 --- a/lib/middleware/parameter.js +++ b/lib/middleware/parameter.js @@ -38,6 +38,14 @@ module.exports = async (ctx, next) => { ctx.state.data.item.forEach((item) => { if (item.description) { const $ = cheerio.load(item.description); + let baseUrl = item.link || ctx.state.data.link; + if (!baseUrl.match(/^https?:\/\//)) { + if (baseUrl.match(/^\/\//)) { + baseUrl = 'http:' + baseUrl; + } else { + baseUrl = 'http://' + baseUrl; + } + } $('script').remove(); @@ -46,7 +54,7 @@ module.exports = async (ctx, next) => { // absolute link if (ctx.state.data.link) { - $ele.attr('href', new URL($ele.attr('href'), item.link || ctx.state.data.link).href); + $ele.attr('href', new URL($ele.attr('href'), baseUrl).href); } }); @@ -66,7 +74,7 @@ module.exports = async (ctx, next) => { // absolute link if (ctx.state.data.link) { - $ele.attr('src', new URL($ele.attr('src'), item.link || ctx.state.data.link).href); + $ele.attr('src', new URL($ele.attr('src'), baseUrl).href); } // referrerpolicy diff --git a/lib/routes/test/index.js b/lib/routes/test/index.js index 4549a8bcf7..7b4f02924b 100644 --- a/lib/routes/test/index.js +++ b/lib/routes/test/index.js @@ -32,18 +32,35 @@ module.exports = async (ctx) => { } else if (ctx.params.id === 'complicated') { item.push({ title: `Complicated Title`, - description: ` - - + description: ` + + + - + `, pubDate: new Date(`2018-3-1`).toUTCString(), - link: `https://github.com/DIYgod/RSSHub/issues/0`, - author: `DIYgod0`, + link: `mock.com/DIYgod/RSSHub`, + author: `DIYgod`, + }); + item.push({ + title: `Complicated Title`, + description: ` +`, + pubDate: new Date(`2018-3-1`).toUTCString(), + link: `//mock.com/DIYgod/RSSHub`, + author: `DIYgod`, + }); + item.push({ + title: `Complicated Title`, + description: ` +`, + pubDate: new Date(`2018-3-1`).toUTCString(), + link: `https://mock.com/DIYgod/RSSHub`, + author: `DIYgod`, }); } diff --git a/test/middleware/parameter.js b/test/middleware/parameter.js index 44e2d32d59..cf910ed644 100644 --- a/test/middleware/parameter.js +++ b/test/middleware/parameter.js @@ -150,13 +150,19 @@ describe('complicated_description', () => { const response = await request.get('/test/complicated'); expect(response.status).toBe(200); const parsed = await parser.parseString(response.text); - expect(parsed.items[0].content).toBe(` - - - - - - -`); + expect(parsed.items[0].content).toBe(` + + + + + + + + +`); + expect(parsed.items[1].content).toBe(` +`); + expect(parsed.items[2].content).toBe(` +`); }); });