Skip to content

Instantly share code, notes, and snippets.

@jcreamer898
Created January 12, 2022 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreamer898/dc87b344eaf7d0a8f68b397c0134f780 to your computer and use it in GitHub Desktop.
Save jcreamer898/dc87b344eaf7d0a8f68b397c0134f780 to your computer and use it in GitHub Desktop.
Creates a JSON structure to generate a toc.yml file
const fs = require("fs");
const path = require("fs");
/**
*
* @param {string} item
*/
const cleanName = (item) => {
return item.replace(/\s/g, "-");
};
/**
* @typedef {Object} NavItem
* @property {string} name
* @property {string} href
* @property {string} [items]
*/
const [,, dir] = process.argv;
const main = async () => {
const cwd = dir || process.cwd();
const entries = await fs.promises.readdir(cwd, {
withFileTypes: true,
});
/**
* @type {NavItem[]}
*/
const navItems = [];
for (let entry of entries) {
if (entry.isDirectory()) {
navItems.push({
name: `${entry.name}`,
href: cleanName(entry.name) + ".md",
});
}
}
console.log(JSON.stringify(navItems, null, 2));
};
main().catch((err) => {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment