@@ -260,7 +260,7 @@ function LinkerPluginShell() {
260260
261261 const rec = this . documentRegistry . get ( doc . id ) ;
262262
263- fileUrl = rec ? rec . uri : this . getURI ( doc ) ;
263+ fileUrl = rec && rec . uri ? this . processInternalURI ( rec . uri ) : this . getURI ( doc ) ;
264264
265265 // Cache this query
266266 if ( fileUrl ) {
@@ -334,9 +334,10 @@ function LinkerPluginShell() {
334334 * wasn't already.
335335 *
336336 * @param {Doc } doc
337+ * @param {boolean } outputRelative
337338 * @return {string }
338339 */
339- getURI ( doc : Doc ) : string {
340+ getURI ( doc : Doc , outputRelative ?: boolean ) : string {
340341 const id = doc . id ;
341342
342343 const record = this . getDocumentRecord ( id ) ;
@@ -347,14 +348,31 @@ function LinkerPluginShell() {
347348 record . uri = uri ;
348349 }
349350
350- return uri ;
351+ return this . processInternalURI ( uri , { outputRelative } ) ;
351352 }
352353
353- createURI ( preferredUri : string ) : string {
354+ createURI ( preferredUri : string , outputRelative ? : boolean ) : string {
354355 const uri = this . generateBaseURI ( preferredUri ) ;
355356
356357 this . getFileRecord ( uri ) ;
357358
359+ return this . processInternalURI ( uri , { outputRelative} ) ;
360+ }
361+
362+ /**
363+ * Replaces variables like <siteRoot/> in the URI.
364+ *
365+ * @param {string } uri
366+ * @param {object } options
367+ * @return {string } uri with siteRoot
368+ */
369+ processInternalURI ( uri : string , options : { outputRelative ?: boolean } = { } ) : string {
370+ if ( ! options . outputRelative ) {
371+ uri = uri . replace ( "%3CsiteRoot%3E" , this . siteRoot ) ;
372+ } else {
373+ uri = uri . replace ( "/%3CsiteRoot%3E/" , "" ) ;
374+ }
375+
358376 return uri;
359377 }
360378
@@ -369,27 +387,29 @@ function LinkerPluginShell() {
369387 *
370388 * @private
371389 * @param {string } canonicalPath - The canonical
390+ * @param {string } pathPrefix - The folder path inside which to place the document, if desired.
372391 * @return {string } The filename to use for the string.
373392 */
374- generateBaseURI ( canonicalPath : string ) : string {
393+ generateBaseURI ( canonicalPath : string , pathPrefix : string = "" ) : string {
375394 let seedURI = ( canonicalPath || "" )
376395 // use - instead of : in namespace prefixes
377396 // replace characters that can cause problems on some filesystems
378- . replace ( / [ \\ / ? * : | ' " < > ] / g, "_" )
397+ . replace ( / [ \\ ? * : | ' " < > ] / g, "_" )
379398 // use - instead of ~ to denote 'inner'
380399 . replace ( / ~ / g, "-" )
381400 // use _ instead of # to denote 'instance'
382401 . replace ( / # / g, "_" )
383- // use _ instead of / (for example, in module names)
384- . replace ( / \/ / g, "_" )
385402 // remove the variation, if any
386403 . replace ( / \( [ \s \S ] * \) $ / , "" )
387404 // make sure we don't create hidden files, or files whose names start with a dash
388405 . replace ( / ^ [ . - ] / , "" ) ;
389406
390407 if ( this . fileLayout === "tree" ) {
391408 seedURI = seedURI . replace ( / [ . ] / g, "/" ) ;
392- seedURI = "/" + path . join ( this . siteRoot , seedURI ) ;
409+ seedURI = path . join ( "/<siteRoot>" , pathPrefix , seedURI ) ;
410+ } else {
411+ // use _ instead of / (for example, in module names)
412+ seedURI = seedURI . replace ( / \/ / g, "_" ) ;
393413 }
394414
395415 // in case we've now stripped the entire basename (uncommon, but possible):
@@ -416,7 +436,7 @@ function LinkerPluginShell() {
416436 }
417437 }
418438
419- return probeURI ;
439+ return encodeURI ( probeURI ) ;
420440 }
421441
422442 /**
@@ -476,7 +496,18 @@ function LinkerPluginShell() {
476496
477497 // the doc gets its own HTML file
478498 if ( standaloneDocTypes . includes ( doc . type ) ) {
479- baseURI = this . generateBaseURI ( docPath ) ;
499+ let pathPrefix = "" ;
500+
501+ if ( doc . type !== "PackageDoc" && doc . loc ) {
502+ const pkg = doc . loc . file . package ;
503+
504+ if ( pkg ) {
505+ // Remove extension of package URI and use as folder
506+ pathPrefix = this . getURI ( pkg , true ) . split ( "." ) . slice ( 0 , - 1 ) . join ( "." ) ;
507+ }
508+ }
509+
510+ baseURI = this . generateBaseURI ( docPath , pathPrefix ) ;
480511
481512 this . getFileRecord ( baseURI ) ;
482513 } else { // inside another HTML file
@@ -490,7 +521,7 @@ function LinkerPluginShell() {
490521 ) ;
491522 }
492523
493- return `${encodeURI ( baseURI ) } ${fragment ? "#" + fragment : "" } `;
524+ return `${ baseURI } ${ fragment ? "#" + fragment : "" } ` ;
494525 }
495526 }
496527
0 commit comments