tickle/modules/rewriteLocalUrls.mjs

21 lines
595 B
JavaScript
Raw Normal View History

//@ts-check
import isExternalUrl from "./isExternalUrl.mjs";
import { querySelectorParent } from "./querySelectorAll.mjs";
/**
* Makes sure urls to local pages get passed through the routing system
* @param {HTMLElement} container the element containing links to find
*/
export const rewriteLocalUrls = (container) => {
querySelectorParent(container, "a").forEach((a) => {
const href = a.getAttribute("href");
if (href && !isExternalUrl(href)) {
a.setAttribute("href", "#/" + href.replace(/^\.?\//, ""));
}
});
return container;
};
export default rewriteLocalUrls;