Skip to content

Commit 029dec3

Browse files
authored
Sidebar generator (#165)
* Script to generate sidebars * Now with 97% test coverage! * Generic sidebar var names The names of the sidebars that get exported from each sidebar.js file aren't actually relevant, moreover many of them were copy pasted and used the wrong name. This changes them to the generic "sidebar" or "sidebars" (for multi-sidebar files) for consistency. * Removed unused _category_.json files from guides Our deployment guides initially used auto-generated sidebars. Now that we're leveraging the sidebar files, these are no longer needed. * Capitalize 'Deployment Walkthrough' in our guide sidebars * Add a README for the sidebar tool
1 parent 4d162e8 commit 029dec3

File tree

72 files changed

+2212
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2212
-160
lines changed

Diff for: README.md

+37-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern
44
static website generator.
55

6-
## Docs Sourcer
6+
## Overview
7+
8+
### Docs Sourcer
79

810
All of the content that we see on the docs site is rendered to the top level
911
`docs/` folder by the
@@ -16,15 +18,15 @@ pull in tagged content from other files, located in other GitHub repos, pull in
1618
images and auto-correct links. This will allow content creators to have more
1719
features when authoring content.
1820

19-
## Content creation
21+
### Content creation
2022

2123
All manually created MarkDown source content lives in the `_docs-sources/`
2224
folder. The top level `docs/` folder contains the _generated_ output that
2325
results after the `docs-sourcer` processes the `_docs-sources/` directory.
2426
Grunts should **never edit any of the MarkDown files in the `docs/` folder
2527
directly**. We should always be editing the content in `_docs-sources/`.
2628

27-
## Installation of dependencies
29+
## Installing dependencies
2830

2931
```sh
3032
yarn
@@ -43,16 +45,7 @@ you edit a file in `_docs-sources/` then the `docs-sourcer` will automatically
4345
re-run to regenerate the output files. Docusaurus will then hot-reload that
4446
content so that to the end user, they have "live reloading" while authoring.
4547

46-
### Run production build locally
47-
48-
```sh
49-
yarn build && yarn serve
50-
```
51-
52-
This will create and serve a production build. This can be used to verify the
53-
expected behaviors in a production environment and view the Google analytics.
54-
55-
## Committing changes to docs
48+
### Committing changes
5649

5750
While authoring local content, you will exclusively be making your changes in
5851
the `_docs-sources/` folder. The `docs-sourcer` will then pre-process and
@@ -64,7 +57,16 @@ Generated content should be up to date if you are previewing locally while
6457
editing but you may wish to manually regenerate the output (see section below)
6558
to ensure it is totally up to date.
6659

67-
## Manually generating docs output
60+
### Running a production build locally
61+
62+
```sh
63+
yarn build && yarn serve
64+
```
65+
66+
This will create and serve a production build. This can be used to verify the
67+
expected behaviors in a production environment and view the Google analytics.
68+
69+
### Manually generating docs output
6870

6971
It's possible to manually regenerate output content from the sources in
7072
`_docs-sources/`:
@@ -76,11 +78,28 @@ yarn regenerate:local
7678
This command can be run at any time, regardless of whether `yarn start` is
7779
currently running.
7880

79-
## Build
81+
### Generating sidebars for guides
82+
83+
We utilitize explicit sidebar definitions for most of our docs content. Doing
84+
so enables authors to easily provide explicit titles for all category labels,
85+
as well as define the relative ordering of pages in a maintainable way.
86+
Long-form docs with many sections receive a dedicated sidebar to provide a
87+
focused reading experience (e.g. /guides/build-it-yourself/\*).
88+
89+
A tool is provided to automatically generate sidebars for any new guide. To
90+
run it, specify the path to the directory you wish to create a sidebar for in
91+
either `_docs-sources/` or the output `docs/` path, relative to your current
92+
working directory.
8093

8194
```sh
82-
yarn build
95+
yarn sidebar docs/guides/build-it-yourself/my-new-guide/
8396
```
8497

85-
This command generates static content into the `build` directory and can be
86-
served using any static contents hosting service.
98+
Once generated, you're free to adjust the names of each category, which only
99+
appear in the sidebar itself. You may also reorder any of the linked pages
100+
within each section, which will be reflected in the rendered output.
101+
102+
See the usage instructions with `yarn sidebar --help` to learn how to add a
103+
back button or specify an output file. The resulting file should be placed in
104+
the `sidebars/` directory. You'll also need to require this file in
105+
`sidebars.js`, and then re-run `yarn start` to see your changes.

Diff for: _docs-sources/guides/build-it-yourself/achieve-compliance/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/achieve-compliance/core-concepts/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/achieve-compliance/deployment-walkthrough/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/achieve-compliance/deployment-walkthrough/deploy-landing-zone-solution/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/achieve-compliance/production-grade-design/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/kubernetes-cluster/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/kubernetes-cluster/core-concepts/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/kubernetes-cluster/deployment-walkthrough/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/kubernetes-cluster/production-grade-design/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/landing-zone/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/landing-zone/core-concepts/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/landing-zone/deployment-walkthrough/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/landing-zone/production-grade-design/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/pipelines/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/pipelines/core-concepts/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/pipelines/deployment-walkthrough/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/pipelines/production-grade-design/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/vpc/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/vpc/core-concepts/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/vpc/deployment-walkthrough/_category_.json

-3
This file was deleted.

Diff for: _docs-sources/guides/build-it-yourself/vpc/production-grade-design/_category_.json

-3
This file was deleted.

Diff for: jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
module.exports = {
7+
clearMocks: true,
8+
collectCoverage: true,
9+
coverageDirectory: "coverage",
10+
coverageProvider: "v8",
11+
preset: "ts-jest",
12+
}

Diff for: package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
"write-heading-ids": "docusaurus write-heading-ids",
1515
"typecheck": "tsc",
1616
"regenerate": "env-cmd --silent docs-sourcer",
17-
"regenerate:local": "yarn regenerate --plugins local-copier"
17+
"regenerate:local": "yarn regenerate --plugins local-copier",
18+
"sidebar": "scripts/generate-sidebar.js",
19+
"test": "jest"
1820
},
1921
"dependencies": {
2022
"@docusaurus/core": "^2.0.0-beta.14",
2123
"@docusaurus/preset-classic": "^2.0.0-beta.14",
2224
"@mdx-js/react": "^1.6.21",
2325
"@svgr/webpack": "^5.5.0",
26+
"@types/jest": "^27.4.0",
2427
"clsx": "^1.1.1",
2528
"config": "^3.3.6",
2629
"env-cmd": "^10.1.0",
@@ -32,11 +35,13 @@
3235
"react": "^17.0.1",
3336
"react-dom": "^17.0.1",
3437
"react-modal": "^3.14.4",
38+
"ts-jest": "^27.1.3",
3539
"url-loader": "^4.1.1"
3640
},
3741
"devDependencies": {
3842
"@docusaurus/module-type-aliases": "^2.0.0-beta.14",
3943
"@tsconfig/docusaurus": "^1.0.4",
44+
"jest": "^27.4.7",
4045
"onchange": "^7.1.0",
4146
"typescript": "^4.3.5"
4247
},

Diff for: scripts/generate-sidebar.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env node
2+
3+
const yargs = require("yargs/yargs")
4+
const path = require("path")
5+
const fs = require("fs")
6+
const {
7+
generateSidebarFile,
8+
generateMultiSidebarFile,
9+
} = require("./sidebar-lib")
10+
11+
// when running with yarn, we need to prepend the initial
12+
// working directory to the path
13+
const resolveDir = (dir) => {
14+
if (process.env.INIT_CWD) {
15+
return path.resolve(path.join(process.env.INIT_CWD, dir))
16+
} else {
17+
return path.resolve(dir)
18+
}
19+
}
20+
21+
async function main() {
22+
const { hideBin } = require("yargs/helpers")
23+
24+
// parse command line args
25+
const argv = yargs(hideBin(process.argv))
26+
.usage("$0 [-bv] [-o file] [directory]")
27+
.option("back", {
28+
alias: "b",
29+
type: "string",
30+
description: "Include a back button with the specified label",
31+
})
32+
.option("output", {
33+
alias: "o",
34+
type: "string",
35+
description: "The path at which to write sidebar file",
36+
})
37+
.option("verbose", {
38+
alias: "v",
39+
type: "boolean",
40+
description: "Run with verbose logging",
41+
})
42+
.positional("directory", {
43+
description:
44+
"The directory to generate a sidebar for, e.g. a multi-page guide, or a list of directories to generate a multi-sidebar file",
45+
})
46+
.strictOptions()
47+
.version("1.0")
48+
.parse()
49+
50+
const opts = {
51+
backButton: argv.back,
52+
verbose: argv.verbose,
53+
}
54+
55+
let data
56+
57+
// generate a single or multi-sidebar file
58+
try {
59+
if (argv._.length < 2) {
60+
// operate on the working dir if not otherwise specified
61+
const inputDir = resolveDir(argv._[0] || ".")
62+
data = await generateSidebarFile(inputDir, opts)
63+
} else {
64+
const inputDirs = argv._.map(resolveDir)
65+
data = await generateMultiSidebarFile(inputDirs, opts)
66+
}
67+
} catch (err) {
68+
console.log(`ERROR: Failed to generate a sidebar — %o`, err.message)
69+
process.exit(1)
70+
}
71+
72+
if (opts.verbose) {
73+
console.log(
74+
"Done. Don't forget to place your sidebar file in /sidebars and update /sidebars.js to require it.\n"
75+
)
76+
}
77+
78+
// write to file or print to stdout
79+
if (argv.output) {
80+
fs.writeFile(argv.output, data, (err) => {
81+
if (err) {
82+
console.log("Error writing sidebar file: ", err)
83+
} else {
84+
console.log("Wrote sidebar file to %o", argv.output)
85+
}
86+
})
87+
} else {
88+
console.log(data)
89+
}
90+
}
91+
92+
main()

0 commit comments

Comments
 (0)