Skip to content

Commit f6330b9

Browse files
committed
Add support for .webdocignore and "category" tutorials
1 parent 9237438 commit f6330b9

File tree

6 files changed

+72
-9
lines changed

6 files changed

+72
-9
lines changed

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"build-next": "cd .. && webdoc && cd example",
2828
"build-pixi-api": "cd ../../pixi-api && webdoc && cd ../webdoc/example",
2929
"build-pixi-api-prod": "cd ../../pixi-api && webdoc --site-root pixi-api && cd ../webdoc/example",
30-
"build-pixi-api-gcp": "cd ../../pixi-api && webdoc && cd ../webdoc/example"
30+
"build-pixi-api-gcp": "cd ../../pixi-api && webdoc --tutorials ./projects/guides --verbose && cd ../webdoc/example"
3131
},
3232
"bugs": {
3333
"url": "https://github.com/SukantPal/webdoc/issues"

packages/webdoc-cli/src/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
2-
import {log, tags} from "missionlog";
2+
3+
import {log, tag} from "missionlog";
34
import fs from "fs";
45
import merge from "lodash.merge";
56

@@ -102,13 +103,15 @@ export function loadConfig(file: string): ConfigSchema {
102103
let config;
103104

104105
if (!fs.existsSync(file)) {
105-
log.warn(tags.Config, `Configuration file not found at: ${file}`);
106+
log.warn(tag.Config, `Configuration file not found at: ${file}`);
106107
config = defaultConfig;
107108
} else {
108109
const userConfig = JSON.parse(fs.readFileSync(file, "utf8"));
109110
config = merge(defaultConfig, userConfig);
110111
}
111112

113+
log.info(tag.CLI, "Loaded configuration");
114+
112115
return config;
113116
}
114117

packages/webdoc-cli/src/index.js

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

66+
6667
if (argv.siteRoot) {
6768
config.template.siteRoot = argv.siteRoot;
6869
}
@@ -105,7 +106,8 @@ async function main(argv: yargs.Argv) {
105106
}
106107

107108
try {
108-
parse(sourceFiles, documentTree);
109+
if (!argv.skipAPI)
110+
parse(sourceFiles, documentTree);
109111
} catch (e) {
110112
// Make sure we get that API structure out so the user can debug the problem!
111113
if (config.opts && config.opts.export) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type {Tutorial} from "@webdoc/types";
44
import fs from "fs";
55
import globby from "globby";
6+
import {log, tag} from "missionlog";
67
import merge from "lodash.merge";
78
import {morphTutorials} from "./morph-tutorials";
89
import path from "path";
@@ -20,23 +21,51 @@ export function loadTutorials(tutorialsDir?: string): Tutorial[] {
2021
return [];
2122
}
2223

24+
if (!path.isAbsolute(tutorialsDir)) {
25+
tutorialsDir = path.join(process.cwd(), tutorialsDir);
26+
}
27+
2328
const tutorialFiles = globby.sync(path.join(tutorialsDir, "./**/*"));
29+
const webdocIgnoreExists = fs.existsSync(path.join(tutorialsDir, "./.webdocignore"));
30+
const webdocIgnorePatterns: $ReadOnlyArray<string> = webdocIgnoreExists
31+
? fs.readFileSync(path.join(tutorialsDir, ".webdocignore"), 'utf8')
32+
.split('\n')
33+
.map((line) => line.trim())
34+
.filter((line) => !line.startsWith('#'))
35+
: [];
36+
37+
if (webdocIgnoreExists) {
38+
log.info(tag.CLI, "Found .webdocignore in the tutorials directory");
39+
}
40+
2441
let tutorialConf = null;
2542
let tutorials: Tutorial[] = [];
2643

2744
for (let i = 0; i < tutorialFiles.length; i++) {
2845
let fileName = tutorialFiles[i];
2946

47+
const relativePath = path.relative(tutorialsDir, fileName);
48+
49+
// Really simple check to ignore files using .webdocignore (undocumented feature)
50+
if (webdocIgnorePatterns.some(
51+
(pattern) => relativePath.startsWith(pattern)
52+
)) {
53+
continue;
54+
}
55+
3056
if (fileName.endsWith(".json")) {
3157
const fileJSON = JSON.parse(fs.readFileSync(fileName, "utf8"));
3258

3359
tutorialConf = merge(tutorialConf || {}, fileJSON);
60+
log.info(tag.CLI, "Found tutorial configuration: " + relativePath);
61+
3462
continue;
3563
}
3664

3765
if (!fileName.endsWith(".html") && !fileName.endsWith(".htm") && !fileName.endsWith(".md")) {
3866
continue;
3967
}
68+
log.info(tag.CLI, "Found tutorial " + relativePath);
4069

4170
let fileContent = fs.readFileSync(fileName, "utf8");
4271

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import type {TutorialDoc} from "@webdoc/types";
4+
import {createTutorialDoc} from "@webdoc/model";
45

56
// This file morphs the tutorials hiearchy to that specified by the tutorial JSON configurations
67
// provided by the user.
@@ -9,6 +10,7 @@ type TutorialReference = string;
910

1011
type TutorialConfigurator = {
1112
"title": ?string,
13+
"kind"?: "category",
1214
"children": TutorialConfigurator[] | {
1315
[id: string]: TutorialReference
1416
}
@@ -90,14 +92,15 @@ function resolveConfigurator(
9092

9193
// Fetch the tutorial-doc based off its name.
9294
function tutorialDoc(name: string, configurator?: TutorialConfigurator): TutorialDoc {
93-
if (!tutorialDB.has(name)) {
95+
let tdoc = tutorialDB.get(name);
96+
97+
if (!tdoc && configurator?.kind !== "category") {
9498
throw new Error("There is no such tutorial '" + name + "'");
9599
}
96-
97-
const tdoc = tutorialDB.get(name);
98-
99100
if (!tdoc) {
100-
throw new Error("NOT FOUND");
101+
// category tutorial
102+
tdoc = createTutorialDoc(name, '');
103+
tutorialDB.set(name, tdoc);
101104
}
102105
if (configurator) {
103106
tdoc.title = configurator.title || name;

packages/webdoc-model/src/doc.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
DocType,
88
PackageDoc,
99
RootDoc,
10+
TutorialDoc,
1011
} from "@webdoc/types";
1112
import {nanoid} from "nanoid";
1213

@@ -129,6 +130,31 @@ export function createPackageDoc(
129130
};
130131
}
131132

133+
/**
134+
* Creates a {@link TutorialDoc}.
135+
*
136+
* @param title - The title of the tutorial.
137+
* @param content - The text-encoded content of the tutorial.
138+
* @param data - Additional data about the tutorial.
139+
*/
140+
export function createTutorialDoc(
141+
title: string,
142+
content: string,
143+
data?: $Shape<TutorialDoc>,
144+
): TutorialDoc {
145+
return {
146+
id: `tutorial-${title}`,
147+
title,
148+
content,
149+
name: title,
150+
path: title,
151+
stack: [title],
152+
members: [],
153+
type: "TutorialDoc",
154+
...data,
155+
}
156+
}
157+
132158
/**
133159
* Searches for the doc named {@code lname} in the given scoped documentation.
134160
*

0 commit comments

Comments
 (0)