Skip to content

Commit 09b2889

Browse files
authored
Fix toc.yaml for V2 reference docs (#1097)
toc.ts is a short script to generate `toc.yaml` based on the generated markdown file.
1 parent fd0e372 commit 09b2889

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

docgen/toc.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Forked of https://github.com/firebase/firebase-js-sdk/blob/5ce06766303b92fea969c58172a7c1ab8695e21e/repo-scripts/api-documenter/src/toc.ts.
20+
*/
21+
import { writeFileSync } from 'fs';
22+
import { resolve } from 'path';
23+
24+
import yargs from 'yargs';
25+
import * as yaml from 'js-yaml';
26+
import { FileSystem } from '@rushstack/node-core-library';
27+
28+
export interface TocGenerationOptions {
29+
inputFolder: string;
30+
outputFolder: string;
31+
g3Path: string;
32+
}
33+
34+
interface TocItem {
35+
title: string;
36+
path: string;
37+
section?: TocItem[];
38+
status?: 'experimental';
39+
}
40+
41+
function fileExt(f: string) {
42+
const parts = f.split('.');
43+
if (parts.length < 2) {
44+
return '';
45+
}
46+
return parts.pop();
47+
}
48+
49+
export function generateToc({
50+
inputFolder,
51+
g3Path,
52+
outputFolder,
53+
}: TocGenerationOptions) {
54+
const asObj = FileSystem.readFolder(inputFolder)
55+
.filter((f) => fileExt(f) === 'md')
56+
.reduce((acc, f) => {
57+
const parts = f.split('.');
58+
parts.pop(); // Get rid of file extenion (.md)
59+
60+
let cursor = acc;
61+
for (const p of parts) {
62+
cursor[p] = cursor[p] || {};
63+
cursor = cursor[p];
64+
}
65+
return acc;
66+
}, {} as any);
67+
68+
function toToc(obj, prefix = ''): TocItem[] {
69+
const toc: TocItem[] = [];
70+
for (const key of Object.keys(obj)) {
71+
const path = prefix?.length ? `${prefix}.${key}` : key;
72+
const section = toToc(obj[key], path);
73+
const tic: TocItem = {
74+
title: key,
75+
path: `${g3Path}/${path}.md`,
76+
};
77+
if (section.length > 0) {
78+
tic.section = section;
79+
}
80+
toc.push(tic);
81+
}
82+
return toc;
83+
}
84+
85+
const toc: TocItem[] = [
86+
{
87+
title: 'firebase-functions',
88+
status: 'experimental',
89+
path: `${g3Path}/firebase-functions.md`,
90+
},
91+
...toToc(asObj['firebase-functions'], 'firebase-functions'),
92+
];
93+
94+
writeFileSync(
95+
resolve(outputFolder, 'toc.yaml'),
96+
yaml.dump(
97+
{ toc },
98+
{
99+
quotingType: '"',
100+
}
101+
)
102+
);
103+
}
104+
105+
const { input, output, path } = yargs(process.argv.slice(2))
106+
.option('input', {
107+
alias: 'i',
108+
describe: 'input folder containing the *.api.json files to be processed.',
109+
default: './input',
110+
})
111+
.option('output', {
112+
alias: 'o',
113+
describe: 'destination for the generated toc content.',
114+
default: './toc',
115+
})
116+
.option('path', {
117+
alias: 'p',
118+
describe: 'specifies the path where the reference docs resides (e.g. g3)',
119+
default: '/',
120+
})
121+
.help().argv;
122+
123+
FileSystem.ensureFolder(output);
124+
generateToc({ inputFolder: input, g3Path: path, outputFolder: output });

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@
161161
"docgen:v1:gen": "api-documenter-fire markdown -i docgen/v1 -o docgen/v1/markdown && api-documenter-fire toc -i docgen/v1 -o docgen/v1/markdown/toc -p /docs/reference/functions",
162162
"docgen:v1": "npm run build && npm run docgen:v1:extract && npm run docgen:v1:gen",
163163
"docgen:v2:extract": "api-extractor run -c docgen/api-extractor.v2.json --local",
164-
"docgen:v2:gen": "api-documenter-fire markdown -i docgen/v2 -o docgen/v2/markdown && api-documenter-fire toc -i docgen/v2 -o docgen/v2/markdown/toc -p /docs/reference/functions/v2",
164+
"docgen:v2:toc": "ts-node docgen/toc.ts --input docgen/v2/markdown --output docgen/v2/markdown/toc --path /docs/functions/beta/reference",
165+
"docgen:v2:gen": "api-documenter-fire markdown -i docgen/v2 -o docgen/v2/markdown && npm run docgen:v2:toc",
165166
"docgen:v2": "npm run build && npm run docgen:v2:extract && npm run docgen:v2:gen",
166167
"build:pack": "rm -rf lib && npm install && tsc -p tsconfig.release.json && npm pack",
167168
"build:release": "npm ci --production && npm install --no-save typescript firebase-admin && tsc -p tsconfig.release.json",
@@ -196,6 +197,7 @@
196197
"@types/node": "^8.10.50",
197198
"@types/node-fetch": "^3.0.3",
198199
"@types/sinon": "^7.0.13",
200+
"api-extractor-model-me": "^0.1.1",
199201
"chai": "^4.2.0",
200202
"chai-as-promised": "^7.1.1",
201203
"child-process-promise": "^2.2.1",

0 commit comments

Comments
 (0)