22 * Markdown rendering utilities
33 */
44import { marked , type Tokens } from "marked" ;
5+ import { basePath } from "../data/docs.ts" ;
56
67/** Generate slug from text (for heading IDs) */
78function slugify ( text : string ) : string {
@@ -13,7 +14,7 @@ function slugify(text: string): string {
1314 . replace ( / \s + / g, "-" ) ;
1415}
1516
16- // Configure marked with custom heading renderer for IDs
17+ // Configure marked with custom heading and link renderers
1718marked . use ( {
1819 gfm : true , // GitHub Flavored Markdown
1920 breaks : false ,
@@ -26,6 +27,15 @@ marked.use({
2627 : token . text ;
2728 return `<h${ token . depth } id="${ id } ">${ text } </h${ token . depth } >\n` ;
2829 } ,
30+ link ( token : Tokens . Link ) : string {
31+ // Add basePath to internal links (starting with /)
32+ let href = token . href ;
33+ if ( href . startsWith ( "/" ) && ! href . startsWith ( "//" ) ) {
34+ href = `${ basePath } ${ href } ` ;
35+ }
36+ const title = token . title ? ` title="${ token . title } "` : "" ;
37+ return `<a href="${ href } "${ title } >${ token . text } </a>` ;
38+ } ,
2939 } ,
3040} ) ;
3141
@@ -74,14 +84,14 @@ function processJsDocLinks(
7484
7585 // Check if it's a local type in the current package
7686 if ( localTypes ?. has ( name ) && currentPackage ) {
77- href = `/api/${ currentPackage } #${ name . toLowerCase ( ) } ` ;
87+ href = `${ basePath } /api/${ currentPackage } #${ name . toLowerCase ( ) } ` ;
7888 } // Check if it's in another package
7989 else if ( typeToPackage ?. has ( name ) ) {
8090 const targetPackage = typeToPackage . get ( name ) ! ;
81- href = `/api/${ targetPackage } #${ name . toLowerCase ( ) } ` ;
91+ href = `${ basePath } /api/${ targetPackage } #${ name . toLowerCase ( ) } ` ;
8292 } // Default: link to current package anchor (best effort)
8393 else if ( currentPackage ) {
84- href = `/api/${ currentPackage } #${ name . toLowerCase ( ) } ` ;
94+ href = `${ basePath } /api/${ currentPackage } #${ name . toLowerCase ( ) } ` ;
8595 }
8696
8797 // Format the display text
@@ -121,6 +131,21 @@ export function extractTitle(content: string): string | undefined {
121131 return match ?. [ 1 ] ;
122132}
123133
134+ /**
135+ * Rewrite internal links in markdown content to include basePath.
136+ * Transforms [text](/path) to [text](/basePath/path) for internal links.
137+ */
138+ export function rewriteMarkdownLinks ( content : string ) : string {
139+ if ( ! basePath ) return content ;
140+
141+ // Match markdown links: [text](url) or [text](url "title")
142+ // Only rewrite internal links starting with /
143+ return content . replace (
144+ / \[ ( [ ^ \] ] * ) \] \( ( \/ ) ( [ ^ ) " ' \s ] * ) ( (?: \s + " [ ^ " ] * " ) ? \) ) / g,
145+ ( _ , text , _slash , path , rest ) => `[${ text } ](${ basePath } /${ path } ${ rest } ` ,
146+ ) ;
147+ }
148+
124149/** Extract table of contents from markdown headings (h2 only) */
125150export function extractToc (
126151 content : string ,
0 commit comments