Created
June 8, 2018 04:01
-
-
Save davesag/4aea244fac895ecc702c23b73a0d333e to your computer and use it in GitHub Desktop.
A simple script to generate table of contents links for GitHub flavoured markdown docs
This file contains hidden or 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
/** | |
* A simple script to generate table of contents links for GitHub flavoured markdown docs | |
*/ | |
const makeAnchor = val => val.trim().toLowerCase().replace(/[^\w\- ]+/g, '').replace(/\s/g, '-').replace(/\-+$/, '') | |
// paths that end with / need a trailing - | |
const wrap = val => val.endsWith('/') | |
? `[\`${val}\`](#${makeAnchor(val)}-)` | |
: `[\`${val}\`](#${makeAnchor(val)})` | |
// put your headers in here | |
const routes = [ | |
'GET /', | |
'GET /ping', | |
'GET /api/v1/some/:excellent/:route', | |
].map(wrap) | |
console.log('* ' + routes.join('\n* ')) | |
/* | |
emits | |
* [`GET /`](#get-) | |
* [`GET /ping`](#get-ping) | |
* [`GET /api/v1/some/:excellent/:route`](#get-apiv1someexcellentroute) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment