Skip to content

Commit 8b6c347

Browse files
authored
Merge pull request #54 from mike-rambil/changeTOC
refactor: streamline subcommand documentation generation by introducing generateSubtocFile function and removing redundant code
2 parents de93317 + 9a81217 commit 8b6c347

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

scripts/generate-readme.js

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -196,31 +196,6 @@ function renderCommand(command) {
196196
}
197197
}
198198

199-
function renderSubtoc(subtoc) {
200-
if (!subtoc || !subtoc.length) return '';
201-
let out = '\n---\n\n### Subcommands\n';
202-
subtoc.forEach((sub) => {
203-
out += `#### ${sub.Name}\n`;
204-
if (sub.short_description) out += `${sub.short_description}\n`;
205-
if (sub.command) out += renderCommand(sub.command);
206-
if (sub.flags) out += renderFlags(sub.flags);
207-
if (sub.examples) out += renderExamples(sub.examples);
208-
if (sub.steps) out += renderSteps(sub.steps);
209-
if (sub.prerequisites) out += renderPrerequisites(sub.prerequisites);
210-
if (sub.warnings) out += renderWarnings(sub.warnings);
211-
if (sub.protips) out += renderProTips(sub.protips);
212-
if (sub.links) out += renderLinks(sub.links);
213-
if (sub.tags) out += renderTags(sub.tags);
214-
if (sub.related_commands)
215-
out += renderRelatedCommands(sub.related_commands);
216-
if (sub.output_example) out += renderOutputExample(sub.output_example);
217-
if (sub.author) out += renderAuthor(sub.author);
218-
if (sub.last_updated) out += renderLastUpdated(sub.last_updated);
219-
out += '\n';
220-
});
221-
return out;
222-
}
223-
224199
function conciseMetaLine(author, lastUpdated, tags) {
225200
let meta = [];
226201
if (author) meta.push(`Author: ${author}`);
@@ -278,6 +253,44 @@ function writeIfChanged(filePath, content) {
278253
fs.writeFileSync(filePath, content, 'utf8');
279254
}
280255

256+
function generateSubtocFile(obj, idx, tocData) {
257+
obj.subtoc.forEach((sub, subIdx) => {
258+
const subPath = path.join(CONTENTS_DIR, `${slugify(sub.Name)}.md`);
259+
let subMd = '';
260+
// Always add Back to parent
261+
subMd += `[⬅️ Back to ${obj.Name}](./${slugify(obj.Name)}.md)\n\n`;
262+
if (subIdx > 0) {
263+
const prev = obj.subtoc[subIdx - 1];
264+
subMd += `[⬆️ Previous Step: ${prev.Name}](./${slugify(
265+
prev.Name
266+
)}.md)\n\n`;
267+
}
268+
subMd += `# ${sub.Name}\n\n`;
269+
if (sub.category) subMd += renderCategory(sub.category);
270+
if (sub.short_description) subMd += `> ${sub.short_description}\n\n`;
271+
if (sub.long_description) subMd += `${sub.long_description}\n\n`;
272+
if (sub.command) subMd += renderCommand(sub.command);
273+
if (sub.flags) subMd += renderFlags(sub.flags);
274+
if (sub.examples) subMd += renderExamples(sub.examples);
275+
if (sub.steps) subMd += renderSteps(sub.steps);
276+
if (sub.prerequisites) subMd += renderPrerequisites(sub.prerequisites);
277+
if (sub.warnings) subMd += renderWarnings(sub.warnings);
278+
if (sub.protips) subMd += renderProTips(sub.protips);
279+
if (sub.links) subMd += renderLinks(sub.links);
280+
if (sub.related_commands)
281+
subMd += renderRelatedCommands(sub.related_commands);
282+
if (sub.output_example) subMd += renderOutputExample(sub.output_example);
283+
if (subIdx < obj.subtoc.length - 1) {
284+
const next = obj.subtoc[subIdx + 1];
285+
subMd += `\n[➡️ See the Next Step: ${next.Name}](./${slugify(
286+
next.Name
287+
)}.md)\n`;
288+
}
289+
subMd += conciseMetaLine(sub.author, sub.last_updated, sub.tags);
290+
writeIfChanged(subPath, subMd);
291+
});
292+
}
293+
281294
function main() {
282295
const tocData = JSON.parse(fs.readFileSync(TOC_JSON, 'utf8'));
283296

@@ -298,42 +311,7 @@ function main() {
298311
writeIfChanged(filePath, content);
299312
// Also create subtoc files if needed
300313
if (obj.subtoc && obj.subtoc.length) {
301-
obj.subtoc.forEach((sub, subIdx) => {
302-
const subPath = path.join(CONTENTS_DIR, `${slugify(sub.Name)}.md`);
303-
let subMd = '';
304-
// Always add Back to parent
305-
subMd += `[⬅️ Back to ${obj.Name}](./${slugify(obj.Name)}.md)\n\n`;
306-
if (subIdx > 0) {
307-
const prev = obj.subtoc[subIdx - 1];
308-
subMd += `[⬆️ Previous Step: ${prev.Name}](./${slugify(
309-
prev.Name
310-
)}.md)\n\n`;
311-
}
312-
subMd += `# ${sub.Name}\n\n`;
313-
if (sub.category) subMd += renderCategory(sub.category);
314-
if (sub.short_description) subMd += `> ${sub.short_description}\n\n`;
315-
if (sub.long_description) subMd += `${sub.long_description}\n\n`;
316-
if (sub.command) subMd += renderCommand(sub.command);
317-
if (sub.flags) subMd += renderFlags(sub.flags);
318-
if (sub.examples) subMd += renderExamples(sub.examples);
319-
if (sub.steps) subMd += renderSteps(sub.steps);
320-
if (sub.prerequisites) subMd += renderPrerequisites(sub.prerequisites);
321-
if (sub.warnings) subMd += renderWarnings(sub.warnings);
322-
if (sub.protips) subMd += renderProTips(sub.protips);
323-
if (sub.links) subMd += renderLinks(sub.links);
324-
if (sub.related_commands)
325-
subMd += renderRelatedCommands(sub.related_commands);
326-
if (sub.output_example)
327-
subMd += renderOutputExample(sub.output_example);
328-
if (subIdx < obj.subtoc.length - 1) {
329-
const next = obj.subtoc[subIdx + 1];
330-
subMd += `\n[➡️ See the Next Step: ${next.Name}](./${slugify(
331-
next.Name
332-
)}.md)\n`;
333-
}
334-
subMd += conciseMetaLine(sub.author, sub.last_updated, sub.tags);
335-
writeIfChanged(subPath, subMd);
336-
});
314+
generateSubtocFile(obj, idx, tocData);
337315
}
338316
});
339317
console.log('Markdown files updated with improved formatting.');

0 commit comments

Comments
 (0)