@@ -14,6 +14,7 @@ const cheerio = require("cheerio");
14
14
const path = require ( "path" ) ;
15
15
const slug = require ( "slug" ) ;
16
16
const resolve = require ( "resolve-pathname" ) ;
17
+ const loaderUtils = require ( "loader-utils" ) ;
17
18
18
19
const routerDelegationClassName = "internal-link" ;
19
20
@@ -70,53 +71,53 @@ const isOtherTypeSibling = href =>
70
71
71
72
const isCrossPackage = href => href . startsWith ( "../../../" ) ;
72
73
73
- const makeHref = ( hash , ...segments ) => {
74
- let href = "/" + segments . join ( "/" ) . replace ( / \. m d $ / , "" ) ;
74
+ const makeHref = ( basename , hash , ...segments ) => {
75
+ let href = basename + "/" + segments . join ( "/" ) . replace ( / \. m d $ / , "" ) ;
75
76
if ( hash ) href += "/" + hash ;
76
77
return href ;
77
78
} ;
78
79
79
- const correctLinks = ( $ , moduleSlug , environment , type ) => {
80
+ const correctLinks = ( $ , moduleSlug , environment , type , basename ) => {
80
81
$ ( "a[href]" ) . each ( ( i , e ) => {
81
82
const $e = $ ( e ) ;
82
83
const [ href , hash ] = $e . attr ( "href" ) . split ( "#" ) ;
83
84
84
85
if ( isSelfHeader ( href ) ) {
85
86
// #render-func
86
87
// /web/api/Route/render-func
87
- $e . attr ( "href" , `/${ environment } /${ type } /${ moduleSlug } /${ hash } ` ) ;
88
+ $e . attr ( "href" , `${ basename } /${ environment } /${ type } /${ moduleSlug } /${ hash } ` ) ;
88
89
$e . addClass ( routerDelegationClassName ) ;
89
90
} else if ( isSibling ( href ) ) {
90
91
// ./quick-start.md
91
92
// /web/guides/quick-start
92
93
const [ _ , doc ] = href . split ( "/" ) ;
93
94
94
- $e . attr ( "href" , makeHref ( hash , environment , type , doc ) ) ;
95
+ $e . attr ( "href" , makeHref ( basename , hash , environment , type , doc ) ) ;
95
96
$e . addClass ( routerDelegationClassName ) ;
96
97
} else if ( isOtherTypeSibling ( href ) ) {
97
98
// ../api/NativeRouter.md
98
99
// /web/api/NativeRouter
99
100
const [ _ , type , doc ] = href . split ( "/" ) ;
100
- $e . attr ( "href" , makeHref ( hash , environment , type , doc ) ) ;
101
+ $e . attr ( "href" , makeHref ( basename , hash , environment , type , doc ) ) ;
101
102
$e . addClass ( routerDelegationClassName ) ;
102
103
} else if ( isCrossPackage ( href ) ) {
103
104
// ../../../react-router/docs/api/Route.md
104
105
// /core/api/Route
105
106
const [ $0 , $1 , $2 , env , $4 , type , doc ] = href . split ( "/" ) ;
106
- $e . attr ( "href" , makeHref ( hash , envMap [ env ] , type , doc ) ) ;
107
+ $e . attr ( "href" , makeHref ( basename , hash , envMap [ env ] , type , doc ) ) ;
107
108
$e . addClass ( routerDelegationClassName ) ;
108
109
}
109
110
} ) ;
110
111
} ;
111
112
112
- const makeHeaderLinks = ( $ , moduleSlug , environment , type ) => {
113
+ const makeHeaderLinks = ( $ , moduleSlug , environment , type , basename ) => {
113
114
// can abstract these two things a bit, but it's late.
114
115
$ ( "h1" ) . each ( ( i , e ) => {
115
116
const $e = $ ( e ) ;
116
117
$e . attr ( "id" , moduleSlug ) ;
117
118
const children = $e . html ( ) ;
118
119
const link = $ (
119
- `<a href="/${ environment } /${ type } /${ moduleSlug } " class="${ routerDelegationClassName } "/>`
120
+ `<a href="${ basename } /${ environment } /${ type } /${ moduleSlug } " class="${ routerDelegationClassName } "/>`
120
121
) ;
121
122
link . html ( children ) ;
122
123
$e . empty ( ) . append ( link ) ;
@@ -129,7 +130,7 @@ const makeHeaderLinks = ($, moduleSlug, environment, type) => {
129
130
$e . attr ( "id" , `${ moduleSlug } -${ slug } ` ) ;
130
131
const children = $e . html ( ) ;
131
132
const link = $ (
132
- `<a href="/${ environment } /${ type } /${ moduleSlug } /${ slug } " class="${ routerDelegationClassName } "/>`
133
+ `<a href="${ basename } /${ environment } /${ type } /${ moduleSlug } /${ slug } " class="${ routerDelegationClassName } "/>`
133
134
) ;
134
135
link . html ( children ) ;
135
136
$e . empty ( ) . append ( link ) ;
@@ -145,11 +146,13 @@ const md = markdownIt({
145
146
146
147
module . exports = function ( content ) {
147
148
this . cacheable ( ) ;
149
+ const options = loaderUtils . getOptions ( this ) ;
150
+ const basename = options . basename || "" ;
148
151
const markup = md . render ( content ) ;
149
152
const $markup = cheerio . load ( markup ) ;
150
153
const title = extractHeaders ( $markup , "h1" , this . data . type ) [ 0 ] ;
151
- correctLinks ( $markup , title . slug , this . data . environment , this . data . type ) ;
152
- makeHeaderLinks ( $markup , title . slug , this . data . environment , this . data . type ) ;
154
+ correctLinks ( $markup , title . slug , this . data . environment , this . data . type , basename ) ;
155
+ makeHeaderLinks ( $markup , title . slug , this . data . environment , this . data . type , basename ) ;
153
156
const headers = extractHeaders ( $markup , "h2" , this . data . type ) ;
154
157
const value = {
155
158
markup : $markup . html ( ) ,
0 commit comments