Created
October 6, 2020 10:21
-
-
Save zbettenbuk/4076a0a42fdc1db747e4ef49f536dd74 to your computer and use it in GitHub Desktop.
Generates a directory .md page of all the .md pages in a folder (e.g. for a GitHub wiki)
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'); | |
/** | |
* Change this line to match the base URL of your pages (e.g. your github wiki). | |
*/ | |
const BASE_URL = 'https://github.com/githubuser/githubproject/wiki/'; | |
/** | |
* Filter for file names in the folder. | |
*/ | |
const PAGE_FILTER = /^.+\.md$/i; | |
/** | |
* Function to generate the page title based on the file name. | |
* | |
* @param {string} fileName - The full file name of the .md file. | |
*/ | |
const getPageTitle = (fileName) => { | |
let pageTitle = fileName | |
.replace(/-/g, ' ') | |
.replace(/.md$/, '') | |
.trim(); | |
return `${pageTitle.charAt(0).toUpperCase()}${pageTitle.slice(1)}`; | |
}; | |
/** | |
* Function to generate the page url (path, actually) based on the file name. | |
* | |
* @param {string} fileName - The full file name of the .md file. | |
*/ | |
const getPageURL = (fileName) => { | |
return `${BASE_URL}${encodeURI(fileName.replace(/.md$/, ''))}`; | |
} | |
(async () => { | |
const dir = await fs.promises.opendir('.'); | |
for await (const dirEntry of dir) { | |
const { name } = dirEntry; | |
if (name.match(PAGE_FILTER)) { | |
console.log(`* [${getPageTitle(name)}](${getPageURL(name)})`); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment