2022-08-11 12:38:08 +00:00
|
|
|
//@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
|
2022-08-11 12:38:08 +00:00
|
|
|
*/
|
|
|
|
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("#")) {
|
2022-08-11 12:38:08 +00:00
|
|
|
a.setAttribute("href", "#/" + href.replace(/^\.?\//, ""));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return container;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default rewriteLocalUrls;
|