Files
authentik/website/docusaurus-theme/remark/link-rewrite-directive.mjs
Teffen Ellis 72af009de8 website/docs: Improved Version Picker. (#14404)
* website: Flesh out version picker. Port 3.8 theme.

* website: Update Dockerfile to include compose.

* website: Flesh out branch override. Tidy list items.
2025-07-10 15:36:48 -04:00

37 lines
967 B
JavaScript

/**
* @import { Root } from "mdast";
*/
import { SKIP, visit } from "unist-util-visit";
/**
* @typedef {[pattern: string | RegExp, replacement: string]} Rewrite
*/
/**
* Remark plugin to transform relative links to docs to absolute URLs
* @param {Iterable<[string, string]>} rewrites Map of urls to rewrite where the key is the prefix to check for and the value is the domain to add
*/
export function remarkLinkRewrite(rewrites) {
const map = new Map(rewrites);
return () => {
/**
* @param {Root} tree The MDAST tree to transform.
*/
return (tree) => {
visit(tree, "link", (node) => {
for (const [pattern, replacement] of map) {
if (!node.url.startsWith(pattern)) continue;
node.url = node.url.replace(pattern, replacement);
}
return SKIP;
});
};
};
}
export default remarkLinkRewrite;