Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import rehypeMermaid from "./src/plugins/rehype/mermaid.ts";
import rehypeAutolinkHeadings from "./src/plugins/rehype/autolink-headings.ts";
import rehypeExternalLinks from "./src/plugins/rehype/external-links.ts";
import rehypeHeadingSlugs from "./src/plugins/rehype/heading-slugs.ts";
import rehypeRewriteUrls from "./src/plugins/rehype/rewrite-urls.ts";

import { sidebar } from "./src/util/sidebar.ts";

Expand All @@ -31,6 +32,7 @@ export default defineConfig({
rehypeAutolinkHeadings,
// @ts-expect-error plugins types are outdated but functional
rehypeTitleFigure,
rehypeRewriteUrls,
],
},
experimental: {
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/rehype/rewrite-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import config from "../../../astro.config.ts";
import { visit } from "unist-util-visit";
import type { Root } from "hast";

// Rewrite relative /api/ links to be absolute, using the `site` base from the Astro config.
export default function () {
return function (tree: Root) {
visit(tree, "element", function (element) {
if (element.tagName === "a") {
if (element.properties.href) {
const url = new URL(element.properties.href as string, config.site);

if (url.origin === config.site) {
if (url.pathname.startsWith("/api/")) {
element.properties.href = url.toString();
}
}
}
}
});
};
}
Loading