Skip to content

Commit d8466de

Browse files
authored
Merge pull request #62 from webdoc-js/fix/root-path
Add --site-root option and respect that within default template. Use …
2 parents 3c485c6 + 45f3c60 commit d8466de

File tree

10 files changed

+58
-22
lines changed

10 files changed

+58
-22
lines changed

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
},
2424
"scripts": {
2525
"unit-test": "",
26-
"build": "webdoc --tutorials ./tutorials",
26+
"build": "webdoc --tutorials ./tutorials --site-root example-documentation",
2727
"build-next": "cd .. && webdoc && cd example",
28-
"build-pixi-api": "cd ../../pixi-api && webdoc && cd ../webdoc/example"
28+
"build-pixi-api": "cd ../../pixi-api && webdoc --site-root docs && cd ../webdoc/example"
2929
},
3030
"bugs": {
3131
"url": "https://github.com/SukantPal/webdoc/issues"

packages/webdoc-cli/src/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type ConfigSchema = {
2727
export?: string,
2828
template?: string,
2929
},
30-
template?: {
30+
template: {
31+
siteRoot: string,
3132
mainPage?: {
3233
title?: string
3334
},
@@ -69,6 +70,7 @@ const defaultConfig: ConfigSchema = {
6970
template: "@webdoc/default-template",
7071
},
7172
template: {
73+
siteRoot: "",
7274
mainPage: {
7375
title: "Main Page",
7476
},

packages/webdoc-cli/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ async function main(argv: yargs.Argv) {
6363
const config = loadConfig(argv.config);
6464
const tutorials = loadTutorials(argv.tutorials);
6565

66+
if (argv.siteRoot) {
67+
config.template.siteRoot = argv.siteRoot;
68+
}
69+
if (config.template.siteRoot[0] === "/") {
70+
config.template.siteRoot = config.template.siteRoot.slice(1);
71+
}
72+
6673
// TODO: Fix what env/conf is?
6774
global.Webdoc.env = config;
6875
// $FlowFixMe
@@ -143,7 +150,7 @@ async function main(argv: yargs.Argv) {
143150
}
144151

145152
const argv = yargs.scriptName("@webdoc/cli")
146-
.usage("$0 -c <configFile> -u <tutorialDir> --verbose")
153+
.usage("$0 -c <configFile> -u <tutorialDir> --verbose --site-root <siteRoot>")
147154
.default("config", path.join(process.cwd(), "webdoc.conf.json"), "webdoc config file")
148155
.alias("c", "config")
149156
.alias("u", "tutorials")

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,18 @@ const HINTS = {
3030
};
3131

3232
// Crawls the tree searching for the API reference
33-
function crawlReference(doc /*: Doc */) {
33+
function crawlReference(doc /*: Doc */, index /*: string */) {
3434
const explorerHierarchy =
3535
buildExplorerHierarchy(doc, doc.packages ? (doc.packages.length > 1) : false);
36-
37-
const tree = buildExplorerTargetsTree(explorerHierarchy);
36+
const tree = buildExplorerTargetsTree(explorerHierarchy, "", index);
3837

3938
return tree;
4039
}
4140

4241
exports.crawlReference = crawlReference;
4342

4443
function getPage(doc /*: Doc */) {
45-
return "/" + linker.getURI(doc);
44+
return linker.getURI(doc);
4645
}
4746

4847
/*::
@@ -159,9 +158,10 @@ function traversePackage(doc /*: Doc | PackageDoc */, context /*: Object */, par
159158
function buildExplorerTargetsTree(
160159
node /*: ExplorerNode */,
161160
parentTitle /*: string */ = "",
161+
index /*: string */,
162162
) /*: ExplorerTarget */ {
163163
const doc = node.doc;
164-
const page = doc.type !== "RootDoc" ? getPage(doc) : "/index.html";
164+
const page = doc.type !== "RootDoc" ? getPage(doc) : index;
165165

166166
let title = "";
167167

@@ -187,16 +187,16 @@ function buildExplorerTargetsTree(
187187
};
188188

189189
if (node.doc.type === "RootDoc") {
190-
node.children["(overview)"].page = "/index.html";
190+
node.children["(overview)"].page = index;
191191

192192
node.children.ClassIndex = {
193193
title: "Class Index",
194-
page: "/" + linker.createURI("Class-Index"),
194+
page: linker.createURI("Class-Index"),
195195
};
196196
}
197197

198198
for (const [, value] of Object.entries(childNodes)) {
199-
const children = value.map((cn) => buildExplorerTargetsTree(cn, title));
199+
const children = value.map((cn) => buildExplorerTargetsTree(cn, title, index));
200200

201201
children.forEach((child) => {
202202
node.children[child.title] = child;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ export type CategorizedDocumentList = {
2020
*/
2121

2222

23-
exports.crawl = function crawl(tree /*: RootDoc */) {
23+
exports.crawl = function crawl(tree /*: RootDoc */, index /*: string */) {
2424
buildLinks(tree);
2525

2626
return {
2727
index: buildIndex(tree),
28-
reference: crawlReference(tree),
28+
reference: crawlReference(tree, index),
2929
};
3030
};
3131

packages/webdoc-default-template/publish.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ const PRETTIFIER_SCRIPT_FILES = [
4545
let idToDoc/*: Map<string, Doc> */;
4646

4747
exports.publish = (options /*: PublishOptions */) => {
48+
const config = options.config;
49+
50+
linker.siteRoot = config.template.siteRoot;
51+
4852
const docTree = options.documentTree;
4953
const outDir = path.normalize(options.config.opts.destination);
5054
const index = linker.createURI("index");
5155

5256
fse.ensureDir(outDir);
5357

54-
const crawlData = crawl(docTree);
58+
const crawlData = crawl(docTree, index);
5559
const renderer = new TemplateRenderer(path.join(__dirname, "tmpl"), null, docTree)
5660
.setLayoutTemplate("layout.tmpl")
5761
.installPlugin("linker", linker)
@@ -168,13 +172,18 @@ function outIndexes(
168172
config /*: WebdocConfig */,
169173
index, /*: Index */
170174
) {
175+
const siteRoot = `/${config.template.siteRoot}`;
171176
const KEY_TO_TITLE = {
172177
"classes": "Class Index",
173178
};
174179

175180
function outIndex(indexKey, indexList) {
176181
const title = KEY_TO_TITLE[indexKey];
177-
const url = indexList.url;
182+
let url = indexList.url;
183+
184+
if (url.startsWith(siteRoot)) {
185+
url = url.slice(siteRoot.length);
186+
}
178187

179188
pipeline.render("pages/api-index.tmpl", {
180189
documentList: indexList,
@@ -196,12 +205,17 @@ function outReference(
196205
config /*: WebdocConfig */,
197206
docTree, /*: RootDoc */
198207
) {
208+
const siteRoot = `/${config.template.siteRoot}`;
209+
199210
for (const [id, docRecord] of linker.documentRegistry) {
200-
const {uri: page} = docRecord;
211+
let {uri: page} = docRecord;
201212

202213
if (page.includes("#")) {
203214
continue;// skip fragments (non-standalone docs)
204215
}
216+
if (page.startsWith(siteRoot)) {
217+
page = page.slice(siteRoot.length);
218+
}
205219

206220
let doc;
207221

packages/webdoc-default-template/src/app/components/Explorer/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function Explorer(props) {
1212
const children = [];
1313

1414
if (!fetched) {
15-
fetch("/explorer/reference.json")
15+
fetch("/" + window.appData.siteRoot + "/explorer/reference.json")
1616
.then((response) => {
1717
if (response.ok) {
1818
response.json().then((idata) => {

packages/webdoc-default-template/static/scripts/default-template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4970,7 +4970,7 @@ function Explorer(props) {
49704970
var children = [];
49714971

49724972
if (!fetched) {
4973-
fetch("/explorer/reference.json").then(function (response) {
4973+
fetch("/" + window.appData.siteRoot + "/explorer/reference.json").then(function (response) {
49744974
if (response.ok) {
49754975
response.json().then(function (idata) {
49764976
setData(idata || {});

packages/webdoc-default-template/tmpl/layout.tmpl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22
/**
33
* Global layout render component.
44
*/
5-
const rootData = obj
5+
const rootData = obj;
6+
const siteRoot = this.getPlugin("linker").siteRoot;
67
?>
78
<!DOCTYPE html>
89
<html lang="en">
910
<head>
1011
<meta charset="utf-8" />
1112
<title>webdoc: <?js= title ?></title>
1213

13-
<link type="text/css" rel="stylesheet" href="/styles/index.css" />
14+
<link type="text/css" rel="stylesheet" href="/<?js= siteRoot ?>/styles/index.css" />
1415
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
1516
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono:300,400,500,700&display=swap" />
1617
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/googlecode.min.css">
1718

1819
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
1920
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
20-
<script src="/scripts/default-template.js"></script>
21+
<script>
22+
window.appData = {
23+
siteRoot: "<?js= siteRoot ?>",
24+
};
25+
</script>
26+
<script src="/<?js= siteRoot ?>/scripts/default-template.js"></script>
2127
</head>
2228
<body class="root">
2329
<div class="docs">

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type FileLayout = "linear" | "tree";
3131
function LinkerPluginShell() {
3232
// TODO: Replace catharsis with a in-built @webdoc/data-type-parser
3333
const catharsis = require("catharsis");
34+
const path = require("path");
3435
const {query} = require("@webdoc/model");
3536
const {MixinPlugin} = require("./MixinPlugin");
3637

@@ -114,6 +115,11 @@ function LinkerPluginShell() {
114115
*/
115116
fileRegistry = new Map<string, LinkerFileRecord>();
116117

118+
/**
119+
* The site root path, used to for absolute linking.
120+
*/
121+
siteRoot = "";
122+
117123
/**
118124
* Cache of DPL queries to their URIs.
119125
*/
@@ -383,6 +389,7 @@ function LinkerPluginShell() {
383389

384390
if (this.fileLayout === "tree") {
385391
seedURI = seedURI.replace(/[.]/g, "/");
392+
seedURI = "/" + path.join(this.siteRoot, seedURI);
386393
}
387394

388395
// in case we've now stripped the entire basename (uncommon, but possible):

0 commit comments

Comments
 (0)