File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import rehypeMermaid from "./src/plugins/rehype/mermaid.ts";
1414import rehypeAutolinkHeadings from "./src/plugins/rehype/autolink-headings.ts" ;
1515import rehypeExternalLinks from "./src/plugins/rehype/external-links.ts" ;
1616import rehypeHeadingSlugs from "./src/plugins/rehype/heading-slugs.ts" ;
17+ import rehypeRewriteUrls from "~/plugins/rehype/rewrite-urls.ts" ;
1718
1819import { 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 : {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments