Skip to content

Commit 546b754

Browse files
committed
Tutorials rendering in @webdoc/default-template working correctly!
1 parent 8427265 commit 546b754

File tree

12 files changed

+113
-31
lines changed

12 files changed

+113
-31
lines changed

packages/webdoc-cli/src/load-tutorials/load-tutorials.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ export function loadTutorials(tutorialsDir?: string): Tutorial[] {
8080
// Tutorials can be part of the doc-tree!
8181
tutorials.push(createTutorialDoc(
8282
fileName,
83-
relativePath
83+
path.join('tutorials', relativePath
8484
.replace(".html", "")
8585
.replace(".htm", "")
86-
.replace(".md", ""),
86+
.replace(".md", "")),
8787
fileContent,
8888
));
8989
}

packages/webdoc-cli/src/load-tutorials/morph-tutorials.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type TutorialConfigurator = {
1212
"title": ?string,
1313
"kind"?: "category",
1414
"children": TutorialConfigurator[] | {
15-
[id: string]: TutorialReference
15+
[id: string]: TutorialConfigurator
1616
}
1717
};
1818

@@ -41,7 +41,7 @@ export function morphTutorials(
4141
tutorials.forEach((t) => tutorialDB.set(t.name, t));
4242

4343
// This is the new list of tutorials
44-
const rootTutorials = [];
44+
let rootTutorials = [];
4545

4646
// eslint-disable-next-line guard-for-in
4747
for (const [name, configurator] of Object.entries(tconf)) {
@@ -59,6 +59,12 @@ export function morphTutorials(
5959
rootTutorials.push(tdoc);
6060
}
6161

62+
// Go through rootTutorials and filter out any ones that became nested by a tutorial
63+
// after they were declared.
64+
rootTutorials = rootTutorials.filter((tutorial) => !nestedTutorials.has(tutorial.name));
65+
66+
console.log(rootTutorials.map((t) => t.name))
67+
6268
return rootTutorials;
6369
}
6470

@@ -83,6 +89,7 @@ function resolveConfigurator(
8389
// eslint-disable-next-line no-prototype-builtins
8490
if (configurator.children.hasOwnProperty(name)) {
8591
tdoc.members.push(tutorialDoc(name, (childConf: any)));
92+
resolveConfigurator(name, (childConf: any));
8693
}
8794
}
8895
}

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ function crawlTutorials(docTree /*: RootDoc */)/*: ?ExplorerTarget */ {
1818
return null;
1919
}
2020

21-
const rootTarget = {
22-
title: "Tutorials",
23-
page: linker.createURI("tutorials/index.html"),
21+
let rootTarget = {
2422
children: {},
2523
};
2624

2725
for (const tutorial of docTree.tutorials) {
2826
buildTutorialTargets(tutorial, rootTarget);
2927
}
3028

29+
// No need for single top-level item
30+
if (Object.keys(rootTarget.children).length === 1) {
31+
rootTarget = { children: rootTarget.children[Object.keys(rootTarget.children)[0]].children };
32+
}
33+
3134
return rootTarget;
3235
}
3336

@@ -37,16 +40,24 @@ function buildTutorialTargets(
3740
)/*: ExplorerTarget */ {
3841
const tutorialTarget = {
3942
title: tutorial.title,
40-
page: linker.createURI(tutorial.route),
43+
page: tutorial.route ? linker.getURI(tutorial) : null,
4144
children: {},
4245
};
4346

44-
parent.title = tutorialTarget;
4547
parent.children[tutorialTarget.title] = tutorialTarget;
4648

47-
for (const childTutorial of tutorial.members) {
48-
if (childTutorial.type === "TutorialDoc") {
49-
buildTutorialTargets(childTutorial, tutorialTarget);
49+
if (tutorial.members.length > 0) {
50+
if (tutorialTarget.page) {
51+
tutorialTarget.children.overview = {
52+
title: '(overview)',
53+
page: tutorialTarget.page,
54+
};
55+
}
56+
57+
for (const childTutorial of tutorial.members) {
58+
if (childTutorial.type === "TutorialDoc") {
59+
buildTutorialTargets(childTutorial, tutorialTarget);
60+
}
5061
}
5162
}
5263

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// @flow
22

3+
/*::
4+
export {CrawlData} from './crawl';
5+
*/
6+
37
exports.crawl = require("./crawl").crawl;

packages/webdoc-default-template/publish.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ const {categoryFilterPlugin} = require("./helper/renderer-plugins/category-filte
2222
/*::
2323
import type {
2424
Doc,
25-
RootDoc
25+
RootDoc,
26+
TutorialDoc,
2627
} from "@webdoc/types";
28+
29+
import type {CrawlData} from './crawl';
2730
*/
2831

2932
Object.assign(linker.standaloneDocTypes, [
@@ -96,6 +99,7 @@ exports.publish = async function publish(options /*: PublishOptions */) {
9699
outMainPage(path.join(outDir, indexRelative), pipeline, options.config);
97100
outIndexes(outDir, pipeline, options.config, crawlData.index);
98101
outReference(outDir, pipeline, options.config, docTree);
102+
outTutorials(outDir, pipeline, options.config, docTree);
99103

100104
pipeline.close();
101105
};
@@ -130,6 +134,17 @@ function outExplorerData(outDir /*: string */, crawlData /*: CrawlData */) {
130134
(err) => {
131135
if (err) throw err;
132136
});
137+
138+
if (crawlData.tutorials) {
139+
fse.writeFile(
140+
path.join(explorerDir, "./tutorials.json"),
141+
JSON.stringify(crawlData.tutorials),
142+
"utf8",
143+
(err) => {
144+
if (err) throw err;
145+
},
146+
);
147+
}
133148
});
134149
}
135150

@@ -175,7 +190,7 @@ async function outReadme(
175190
}
176191

177192
pipeline.render("pages/main-page.tmpl", {
178-
docs: [],
193+
document: null,
179194
readme,
180195
title: "Documentation",
181196
env: config,
@@ -247,7 +262,7 @@ function outReference(
247262
);
248263
} else {
249264
pipeline.render("document.tmpl", {
250-
docs: [doc],
265+
document: doc,
251266
title: doc.name,
252267
env: config,
253268
}, {
@@ -256,3 +271,26 @@ function outReference(
256271
}
257272
}
258273
}
274+
275+
function outTutorials(
276+
outDir /*: string */,
277+
pipeline /*: TemplatePipeline */,
278+
config /*: WebdocConfig */,
279+
docTree /*: RootDoc */,
280+
) {
281+
function out(tutorial /*: TutorialDoc */) {
282+
const uri = linker.getURI(tutorial);
283+
284+
pipeline.render("tutorial.tmpl", {
285+
document: tutorial,
286+
title: tutorial.title,
287+
env: config,
288+
}, {
289+
outputFile: path.join(outDir, uri),
290+
});
291+
292+
tutorial.members.forEach((out /*: any */));
293+
}
294+
295+
docTree.tutorials.forEach(out);
296+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export default connect(({
7878
const {root} = useExplorerStyles();
7979
const toggleOpen = React.useCallback(() => setOpen(!isOpen), [isOpen]);
8080
const children = [];
81-
const sitePrefix = window.appData.siteRoot ? "/" + window.appData.siteRoot + "/" : "/";
81+
const explorerTree = window.appData.explorerTree;
8282

8383
if (!fetched) {
84-
fetch(sitePrefix + "explorer/reference.json")
84+
fetch(explorerTree)
8585
.then((response) => {
8686
if (response.ok) {
8787
response.json().then((idata) => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9488,10 +9488,10 @@ function makeIds(data, collector) {
94889488
return setOpen(!isOpen);
94899489
}, [isOpen]);
94909490
var children = [];
9491-
var sitePrefix = window.appData.siteRoot ? "/" + window.appData.siteRoot + "/" : "/";
9491+
var explorerTree = window.appData.explorerTree;
94929492

94939493
if (!fetched) {
9494-
fetch(sitePrefix + "explorer/reference.json").then(function (response) {
9494+
fetch(explorerTree).then(function (response) {
94959495
if (response.ok) {
94969496
response.json().then(function (idata) {
94979497
var defaultExpanded = new Set();

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?js
2-
/**
3-
* Documentation object render component.
4-
*/
5-
const
6-
docData = obj,
7-
docList = obj.docs;
2+
/**
3+
* Documentation object render component.
4+
*/
5+
const doc = obj.document;
86

9-
docList.forEach((doc) => {
107
const titleMods = [
118
doc.deprecated ? "document__title_deprecated" : ""
129
].join(" ");
@@ -98,5 +95,4 @@ docList.forEach((doc) => {
9895
</div>
9996
<footer class="footer" id="footer-mount-point"></footer>
10097
</div>
101-
<?js= this.partial("components/members-explorer/index.tmpl", doc) ?>
102-
<?js }); ?>
98+
<?js= this.partial("components/members-explorer/index.tmpl", doc) ?>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Global layout render component.
44
*/
55
const rootData = obj;
6+
const document = rootData.document;
67
const config = rootData.env.template;
78
const siteRoot = this.getPlugin("linker").siteRoot;
89
const sitePrefix = siteRoot ? siteRoot + "/" : siteRoot;
@@ -26,6 +27,7 @@ const integrations = JSON.stringify(config.integrations);
2627
<script>
2728
window.appData = {
2829
applicationName: "<?js= config.applicationName ?>",
30+
explorerTree: "<?js= siteRoot + "/explorer/" + (document && document.type === "TutorialDoc" ? "tutorials" : "reference") + ".json" ?>",
2931
siteRoot: "<?js= siteRoot ?>",
3032
integrations: JSON.parse(`<?js= integrations ?>`),
3133
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?js
2+
/** Tutorial rendering component */
3+
const document = obj.document;
4+
?>
5+
<div class="page-content">
6+
<div class="document">
7+
<?js /* Simplicity, at its finest! */ ?>
8+
<?js= document.content ?>
9+
</div>
10+
</div>

0 commit comments

Comments
 (0)