@@ -3,15 +3,17 @@ import { visit } from "unist-util-visit";
3
3
import path from "node:path" ;
4
4
import { type StarlightPlugin } from "@astrojs/starlight/types" ;
5
5
import { DOCS_DIR } from "./utils/constants.ts" ;
6
+ import { readdirSync } from "node:fs" ;
6
7
7
8
type RemarkPlugin = RemarkPlugins [ number ] ;
8
9
9
10
const markdownUrlsRemarkPlugin : RemarkPlugin = (
10
- [ logger , docsDir , site , baseUrl ] : [
11
+ [ logger , docsDir , site , baseUrl , crossPackageUrlRegex ] : [
11
12
AstroIntegrationLogger ,
12
13
string ,
13
14
string ,
14
15
string ,
16
+ RegExp ,
15
17
] ,
16
18
) =>
17
19
( tree , file ) => {
@@ -38,25 +40,36 @@ const markdownUrlsRemarkPlugin: RemarkPlugin = (
38
40
return ;
39
41
}
40
42
43
+ // if the url is a cross-package link, go back one level and add the api directory
44
+ const normalizedUrl = url . replace ( crossPackageUrlRegex , '../../$1/api' ) ;
45
+ const absoluteLinkedFilePath = path . resolve ( currentFileDir , normalizedUrl ) ;
46
+
41
47
// normalize all other relative URLs to the docs directory
42
- const absoluteLinkedFilePath = path . resolve ( currentFileDir , url ) ;
43
48
const relativeToDocs = path . relative ( docsDir , absoluteLinkedFilePath ) ;
44
- const normalizedUrl = `${ baseUrl } ${
49
+ const nodeUrl = `${ baseUrl } ${
45
50
relativeToDocs . replace ( / ( i n d e x ) ? \. m d x ? ( # .* ) ? $ / , "$2" ) . toLowerCase ( )
46
51
} `;
47
- logger . debug ( `Normalizing URL: ${ url } -> ${ normalizedUrl } ` ) ;
52
+ logger . debug ( `Normalizing URL: ${ url } -> ${ nodeUrl } ` ) ;
48
53
49
- node . url = normalizedUrl ;
54
+ node . url = nodeUrl ;
50
55
} ) ;
51
56
} ;
52
57
53
- export function markdownUrlsPlugin ( ) : StarlightPlugin {
58
+ interface MarkdownUrlsPluginOptions {
59
+ packagesDir : string ;
60
+ }
61
+
62
+ export function markdownUrlsPlugin ( { packagesDir } : MarkdownUrlsPluginOptions ) : StarlightPlugin {
54
63
return {
55
64
name : "@dfinity/starlight/markdown-urls" ,
56
65
hooks : {
57
66
"config:setup" : ( ctx ) => {
58
67
const site = ctx . astroConfig . site ;
59
68
69
+ // get all the packages in the packagesDir
70
+ const packages = readdirSync ( packagesDir ) ;
71
+ const crossPackageUrlRegex = new RegExp ( `\\.\\.\\/(${ packages . join ( '|' ) } )` ) ;
72
+
60
73
ctx . addIntegration ( {
61
74
name : "libs-astro-plugin" ,
62
75
hooks : {
@@ -70,6 +83,7 @@ export function markdownUrlsPlugin(): StarlightPlugin {
70
83
DOCS_DIR ,
71
84
site ,
72
85
ctx . astroConfig . base ,
86
+ crossPackageUrlRegex ,
73
87
] ] ,
74
88
] ,
75
89
} ,
0 commit comments