From c76bb81536f83474bbe181bed1e7dc7de65f6ff5 Mon Sep 17 00:00:00 2001 From: Zhenyi-Wang <47094597+Zhenyi-Wang@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:53:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BD=91=E9=A1=B5=E6=8A=93=E5=8F=96?= =?UTF-8?q?=EF=BC=9A=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86//=E5=BC=80?= =?UTF-8?q?=E5=A4=B4=E7=9A=84=E8=B6=85=E9=93=BE=E6=8E=A5=20(#2803)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zhenyiwang --- packages/service/common/string/cheerio.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/service/common/string/cheerio.ts b/packages/service/common/string/cheerio.ts index ca493c358..99b26a461 100644 --- a/packages/service/common/string/cheerio.ts +++ b/packages/service/common/string/cheerio.ts @@ -14,6 +14,7 @@ export const cheerioToHtml = ({ }) => { // get origin url const originUrl = new URL(fetchUrl).origin; + const protocol = new URL(fetchUrl).protocol; // http: or https: const usedSelector = selector || 'body'; const selectDom = $(usedSelector); @@ -32,14 +33,22 @@ export const cheerioToHtml = ({ // if link,img startWith /, add origin url selectDom.find('a').each((i, el) => { const href = $(el).attr('href'); - if (href && href.startsWith('/')) { - $(el).attr('href', originUrl + href); + if (href) { + if (href.startsWith('//')) { + $(el).attr('href', protocol + href); + } else if (href.startsWith('/')) { + $(el).attr('href', originUrl + href); + } } }); selectDom.find('img').each((i, el) => { const src = $(el).attr('src'); - if (src && src.startsWith('/')) { - $(el).attr('src', originUrl + src); + if (src) { + if (src.startsWith('//')) { + $(el).attr('src', protocol + src); + } else if (src.startsWith('/')) { + $(el).attr('src', originUrl + src); + } } });