Skip to content

Instantly share code, notes, and snippets.

@davesag
Created June 8, 2018 04:01
Show Gist options
  • Save davesag/4aea244fac895ecc702c23b73a0d333e to your computer and use it in GitHub Desktop.
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
/**
* 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