in _includes/resources/playlist/PlaylistLayout.11ty.tsx [30:70]
function relativize(originalUrl: string, content: string) {
const prefix = originalUrl; //`../../${originalUrl}`;
const doc = parse(content);
const anchors = doc.getElementsByTagName("a");
const imgs = doc.getElementsByTagName("img");
function rewriteAttribute(element: HTMLElement, attribute: string) {
const href = element.attrs[attribute];
// no value
if (!href) return;
// already good
if (href.startsWith(prefix)) return;
// data URI
if (href.startsWith("data:")) return;
// absolute urls
if (href.startsWith("http://") || href.startsWith("https://")) return;
// relative paths
if (href.startsWith(".") && !href.startsWith("../")) return;
// root link
if (href.startsWith("/")) return;
//anchor link
if (href.startsWith("#")) return;
// ignore VITE ASSETS
if (href.startsWith("__VITE_ASSET__")) return;
if (prefix && href) {
element.setAttribute(attribute, path.join(prefix, href));
}
}
anchors.forEach((element) => {
rewriteAttribute(element, "href");
});
imgs.forEach((element) => {
rewriteAttribute(element, "src");
});
return doc.toString();
}