tickle/modules/utils/rewriteLocalUrls.mjs

21 lines
603 B
JavaScript
Raw Permalink Normal View History

//@ts-check
import isExternalUrl from "./isExternalUrl.mjs";
import { querySelectorParent } from "./querySelectorAll.mjs";
/**
2023-05-09 02:00:53 +00:00
* Makes sure urls to local pages are prepended with #/
* @param {ParentNode} container the element containing links to find
*/
export const rewriteLocalUrls = (container) => {
querySelectorParent(container, "a").forEach((a) => {
const href = a.getAttribute("href");
2023-05-09 02:00:53 +00:00
if (href && !isExternalUrl(href) && !href.startsWith("#")) {
a.setAttribute("href", "#/" + href.replace(/^\.?\//, ""));
}
});
return container;
};
export default rewriteLocalUrls;