fix: rewrite improvements, including setting the base tag
This commit is contained in:
parent
6738b2337f
commit
ad71feea88
|
@ -12,6 +12,14 @@ export default class URLRewriteFilter implements ContentFilter {
|
||||||
let html = await response.text();
|
let html = await response.text();
|
||||||
|
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
// Insert or adjust the <base> tag
|
||||||
|
if ($("base").length) {
|
||||||
|
$("base").attr("href", "/browse/");
|
||||||
|
} else {
|
||||||
|
$("head").prepend('<base href="/browse/">');
|
||||||
|
}
|
||||||
|
|
||||||
["a", "link", "script", "img"].forEach((tag) => {
|
["a", "link", "script", "img"].forEach((tag) => {
|
||||||
$.root()
|
$.root()
|
||||||
.find(tag)
|
.find(tag)
|
||||||
|
@ -20,16 +28,7 @@ export default class URLRewriteFilter implements ContentFilter {
|
||||||
let urlValue = $(element).attr(attrName);
|
let urlValue = $(element).attr(attrName);
|
||||||
if (urlValue) {
|
if (urlValue) {
|
||||||
if (!isICANN(urlValue)) {
|
if (!isICANN(urlValue)) {
|
||||||
if (
|
if (urlValue.startsWith("http")) {
|
||||||
urlValue.startsWith("/") ||
|
|
||||||
urlValue.startsWith("../") ||
|
|
||||||
urlValue.startsWith("http")
|
|
||||||
) {
|
|
||||||
console.log("before", urlValue);
|
|
||||||
if (!urlValue.startsWith("/")) {
|
|
||||||
urlValue = `/${urlValue}`;
|
|
||||||
}
|
|
||||||
console.log("after", urlValue);
|
|
||||||
$(element).attr(attrName, `/browse${urlValue}`);
|
$(element).attr(attrName, `/browse${urlValue}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue