Skip to content

Commit d154c59

Browse files
committed
Add packageName prefix in all URLs
1 parent 86b3ea5 commit d154c59

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

packages/webdoc-cli/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ async function main(argv: yargs.Argv) {
6969
if (config.template.siteRoot[0] === "/") {
7070
config.template.siteRoot = config.template.siteRoot.slice(1);
7171
}
72+
if (config.template.siteRoot.endsWith("/")) {
73+
config.template.siteRoot.length -= 1;
74+
}
7275

7376
// TODO: Fix what env/conf is?
7477
global.Webdoc.env = config;

packages/webdoc-default-template/helper/crawl/crawl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function buildLinks(tree /*: RootDoc */) /*: void */ {
4444
}
4545

4646
function buildIndex(tree /*: RootDoc */) /*: [id: string]: [] & { url: string } */ {
47-
const classIndexUrl = linker.createURI("Class-Index");
47+
const classIndexUrl = linker.createURI("Class-Index", true);
4848

4949
const index /*: [id: string]: [] & { url: string } */ = {
5050
classes: [],

packages/webdoc-default-template/publish.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ exports.publish = (options /*: PublishOptions */) => {
5252
const docTree = options.documentTree;
5353
const outDir = path.normalize(options.config.opts.destination);
5454
const index = linker.createURI("index");
55-
const indexRelative = index.slice(linker.siteRoot.length + 1);
55+
const indexRelative = index.replace(`/${linker.siteRoot}/`, "");
5656

5757
fse.ensureDir(outDir);
5858

@@ -173,18 +173,13 @@ function outIndexes(
173173
config /*: WebdocConfig */,
174174
index, /*: Index */
175175
) {
176-
const siteRoot = `/${config.template.siteRoot}`;
177176
const KEY_TO_TITLE = {
178177
"classes": "Class Index",
179178
};
180179

181180
function outIndex(indexKey, indexList) {
182181
const title = KEY_TO_TITLE[indexKey];
183-
let url = indexList.url;
184-
185-
if (url.startsWith(siteRoot)) {
186-
url = url.slice(siteRoot.length);
187-
}
182+
const url = linker.processInternalURI(indexList.url, {outputRelative: true});
188183

189184
pipeline.render("pages/api-index.tmpl", {
190185
documentList: indexList,
@@ -206,17 +201,16 @@ function outReference(
206201
config /*: WebdocConfig */,
207202
docTree, /*: RootDoc */
208203
) {
209-
const siteRoot = `/${config.template.siteRoot}`;
210-
211204
for (const [id, docRecord] of linker.documentRegistry) {
212205
let {uri: page} = docRecord;
213206

214207
if (page.includes("#")) {
215208
continue;// skip fragments (non-standalone docs)
216209
}
217-
if (page.startsWith(siteRoot)) {
218-
page = page.slice(siteRoot.length);
219-
}
210+
211+
page = linker.processInternalURI(page, {outputRelative: true});
212+
213+
console.log(page);
220214

221215
let doc;
222216

packages/webdoc-template-library/src/template-plugins/LinkerPlugin.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ? 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 {
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

Comments
 (0)