@@ -14,6 +14,7 @@ const cheerio = require("cheerio");
1414const path = require ( "path" ) ;
1515const slug = require ( "slug" ) ;
1616const resolve = require ( "resolve-pathname" ) ;
17+ const loaderUtils = require ( "loader-utils" ) ;
1718
1819const routerDelegationClassName = "internal-link" ;
1920
@@ -70,53 +71,53 @@ const isOtherTypeSibling = href =>
7071
7172const isCrossPackage = href => href . startsWith ( "../../../" ) ;
7273
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 $ / , "" ) ;
7576 if ( hash ) href += "/" + hash ;
7677 return href ;
7778} ;
7879
79- const correctLinks = ( $ , moduleSlug , environment , type ) => {
80+ const correctLinks = ( $ , moduleSlug , environment , type , basename ) => {
8081 $ ( "a[href]" ) . each ( ( i , e ) => {
8182 const $e = $ ( e ) ;
8283 const [ href , hash ] = $e . attr ( "href" ) . split ( "#" ) ;
8384
8485 if ( isSelfHeader ( href ) ) {
8586 // #render-func
8687 // /web/api/Route/render-func
87- $e . attr ( "href" , `/${ environment } /${ type } /${ moduleSlug } /${ hash } ` ) ;
88+ $e . attr ( "href" , `${ basename } /${ environment } /${ type } /${ moduleSlug } /${ hash } ` ) ;
8889 $e . addClass ( routerDelegationClassName ) ;
8990 } else if ( isSibling ( href ) ) {
9091 // ./quick-start.md
9192 // /web/guides/quick-start
9293 const [ _ , doc ] = href . split ( "/" ) ;
9394
94- $e . attr ( "href" , makeHref ( hash , environment , type , doc ) ) ;
95+ $e . attr ( "href" , makeHref ( basename , hash , environment , type , doc ) ) ;
9596 $e . addClass ( routerDelegationClassName ) ;
9697 } else if ( isOtherTypeSibling ( href ) ) {
9798 // ../api/NativeRouter.md
9899 // /web/api/NativeRouter
99100 const [ _ , type , doc ] = href . split ( "/" ) ;
100- $e . attr ( "href" , makeHref ( hash , environment , type , doc ) ) ;
101+ $e . attr ( "href" , makeHref ( basename , hash , environment , type , doc ) ) ;
101102 $e . addClass ( routerDelegationClassName ) ;
102103 } else if ( isCrossPackage ( href ) ) {
103104 // ../../../react-router/docs/api/Route.md
104105 // /core/api/Route
105106 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 ) ) ;
107108 $e . addClass ( routerDelegationClassName ) ;
108109 }
109110 } ) ;
110111} ;
111112
112- const makeHeaderLinks = ( $ , moduleSlug , environment , type ) => {
113+ const makeHeaderLinks = ( $ , moduleSlug , environment , type , basename ) => {
113114 // can abstract these two things a bit, but it's late.
114115 $ ( "h1" ) . each ( ( i , e ) => {
115116 const $e = $ ( e ) ;
116117 $e . attr ( "id" , moduleSlug ) ;
117118 const children = $e . html ( ) ;
118119 const link = $ (
119- `<a href="/${ environment } /${ type } /${ moduleSlug } " class="${ routerDelegationClassName } "/>`
120+ `<a href="${ basename } /${ environment } /${ type } /${ moduleSlug } " class="${ routerDelegationClassName } "/>`
120121 ) ;
121122 link . html ( children ) ;
122123 $e . empty ( ) . append ( link ) ;
@@ -129,7 +130,7 @@ const makeHeaderLinks = ($, moduleSlug, environment, type) => {
129130 $e . attr ( "id" , `${ moduleSlug } -${ slug } ` ) ;
130131 const children = $e . html ( ) ;
131132 const link = $ (
132- `<a href="/${ environment } /${ type } /${ moduleSlug } /${ slug } " class="${ routerDelegationClassName } "/>`
133+ `<a href="${ basename } /${ environment } /${ type } /${ moduleSlug } /${ slug } " class="${ routerDelegationClassName } "/>`
133134 ) ;
134135 link . html ( children ) ;
135136 $e . empty ( ) . append ( link ) ;
@@ -145,11 +146,13 @@ const md = markdownIt({
145146
146147module . exports = function ( content ) {
147148 this . cacheable ( ) ;
149+ const options = loaderUtils . getOptions ( this ) ;
150+ const basename = options . basename || "" ;
148151 const markup = md . render ( content ) ;
149152 const $markup = cheerio . load ( markup ) ;
150153 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 ) ;
153156 const headers = extractHeaders ( $markup , "h2" , this . data . type ) ;
154157 const value = {
155158 markup : $markup . html ( ) ,
0 commit comments