1
1
import type { Config } from "@docusaurus/types" ;
2
2
import type { Root } from "mdast" ;
3
- import { relative , resolve } from "path" ;
3
+ import { dirname , relative , resolve } from "path" ;
4
4
import { themes } from "prism-react-renderer" ;
5
- import * as unified from "unified" ;
5
+ import type { Transformer } from "unified" ;
6
6
import { visit } from "unist-util-visit" ;
7
+ import { createRequire } from "node:module" ;
8
+ import { fileURLToPath } from "node:url" ;
9
+
10
+ const require = createRequire ( import . meta. url ) ;
7
11
8
12
/**
9
13
* Files within /docs reference repository directories
@@ -17,24 +21,27 @@ import { visit } from "unist-util-visit";
17
21
* - Try resolving it relative to repo root.
18
22
* - If anywhere but /docs - link to GitHub.
19
23
*/
20
- function remarkPluginFixLinksToRepositoryArtifacts ( ) {
21
- const transformer : unified . Transformer < Root > = async ( ast , file ) => {
24
+ function remarkPluginFixLinksToRepositoryArtifacts ( ) : Transformer < Root > {
25
+ return ( ast , file ) => {
22
26
visit ( ast , "link" , ( node ) => {
23
- const link = node . url ;
24
- if ( link . startsWith ( "http://" ) || link . startsWith ( "https://" ) ) {
27
+ const { url } = node ;
28
+ if ( url . startsWith ( "http://" ) || url . startsWith ( "https://" ) ) {
25
29
return ;
26
30
}
27
31
28
32
// Docusaurus runs this plugin on its intermediate
29
33
// markdown representaiton as well as on our original files.
30
34
// These are relative links that docusaurus already figured out
31
35
// based on realative links to .md files
32
- if ( link . startsWith ( "/docs/" ) ) {
36
+ if ( url . startsWith ( "/docs/" ) ) {
33
37
return ;
34
38
}
35
39
36
- const repoRoot = resolve ( __dirname , "../.." ) ;
37
- const artifact = resolve ( file . dirname ! , link ) ;
40
+ const repoRoot = resolve (
41
+ dirname ( fileURLToPath ( import . meta. url ) ) ,
42
+ "../.." ,
43
+ ) ;
44
+ const artifact = resolve ( file . dirname ! , url ) ;
38
45
const artifactRelative = relative ( repoRoot , artifact ) ;
39
46
40
47
// We host all files under docs, will resolve as a relative link
@@ -49,7 +56,6 @@ function remarkPluginFixLinksToRepositoryArtifacts() {
49
56
node . url = linkToRepositoryArtifact ;
50
57
} ) ;
51
58
} ;
52
- return transformer ;
53
59
}
54
60
55
61
const config : Config = {
0 commit comments