Skip to content

Instantly share code, notes, and snippets.

@zbettenbuk
Created October 6, 2020 10:21
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 zbettenbuk/4076a0a42fdc1db747e4ef49f536dd74 to your computer and use it in GitHub Desktop.
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)
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