From 3ea1ce06c94aca425b05cb4c5d4dbac2cf5410b3 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 18 Oct 2023 08:58:33 -0400 Subject: [PATCH] fix: add srcset support --- src/backend/filters/urlRewrite.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/backend/filters/urlRewrite.ts b/src/backend/filters/urlRewrite.ts index 130f27a..5cb34ec 100644 --- a/src/backend/filters/urlRewrite.ts +++ b/src/backend/filters/urlRewrite.ts @@ -43,6 +43,20 @@ export default class URLRewriteFilter implements ContentFilter { $(element).attr(attrName, urlValue); } } + + if (tag === "img" && $(element).attr("srcset")) { + let srcsetValue = $(element).attr("srcset"); + let srcsetValues = srcsetValue?.split(","); + let rewrittenSrcsetValues = srcsetValues?.map((srcsetEntry) => { + let [url, descriptor] = srcsetEntry.trim().split(" "); + if (!url.startsWith("http") && !url.startsWith("//")) { + url = path.join(rUrl.pathname, url); + url = `${rUrl.protocol}//${rUrl.hostname}/browse/${rUrl.hostname}${url}`; + } + return `${url} ${descriptor}`; + }); + $(element).attr("srcset", rewrittenSrcsetValues?.join(", ")); + } }); });