Skip to content

Commit bb17624

Browse files
committed
[Docs Site] Rewrite /api/ links to point at production
1 parent a2fb75b commit bb17624

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

astro.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import rehypeMermaid from "./src/plugins/rehype/mermaid.ts";
1414
import rehypeAutolinkHeadings from "./src/plugins/rehype/autolink-headings.ts";
1515
import rehypeExternalLinks from "./src/plugins/rehype/external-links.ts";
1616
import rehypeHeadingSlugs from "./src/plugins/rehype/heading-slugs.ts";
17+
import rehypeRewriteUrls from "~/plugins/rehype/rewrite-urls.ts";
1718

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

@@ -31,6 +32,7 @@ export default defineConfig({
3132
rehypeAutolinkHeadings,
3233
// @ts-expect-error plugins types are outdated but functional
3334
rehypeTitleFigure,
35+
rehypeRewriteUrls,
3436
],
3537
},
3638
experimental: {

src/plugins/rehype/rewrite-urls.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import config from "../../../astro.config.ts";
2+
import { visit } from "unist-util-visit";
3+
import type { Root } from "hast";
4+
5+
// Rewrite relative /api/ links to be absolute, using the `site` base from the Astro config.
6+
export default function () {
7+
return function (tree: Root) {
8+
visit(tree, "element", function (element) {
9+
if (element.tagName === "a") {
10+
if (element.properties.href) {
11+
const url = new URL(element.properties.href as string, config.site);
12+
13+
if (url.origin === config.site) {
14+
if (url.pathname.startsWith("/api/")) {
15+
element.properties.href = url.toString();
16+
}
17+
}
18+
}
19+
}
20+
});
21+
};
22+
}

0 commit comments

Comments
 (0)