Created
January 12, 2022 22:10
-
-
Save jcreamer898/dc87b344eaf7d0a8f68b397c0134f780 to your computer and use it in GitHub Desktop.
Creates a JSON structure to generate a toc.yml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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