fix: url base

This commit is contained in:
DIYgod
2019-08-28 15:50:05 +08:00
parent 84fdea3d1e
commit b2355436cc
3 changed files with 47 additions and 16 deletions

View File

@@ -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