toc.js
This file
Path
support/toc.js
21 lines · 706 bytes · JavaScript
Last changed by
More docsTj Holowaychukbefore adoptionde81ee7
History of this fileTake it
123456789101112131415161718192021#!/usr/bin/env node
var fs = require('fs'), file = process.argv[2];
if (file) { var js = fs.readFileSync(file, 'utf8'), headers = js.match(/<h3 id="(.*?)">(.*?)<\/h3>/g), toc = ['<ul id="toc">']; if (js.indexOf('id="toc"') < 0) { headers.forEach(function(header){ var captures = header.match(/id="(.*?)">(.*?)</), id = captures[1], title = captures[2].replace(/\(.*?\)/, '()'); toc.push('<li><a href="#' + id + '">' + title + '</a></li>'); }); toc.push('</ul>'); js = js.replace('<div id="container">', '<div id="container">' + toc.join('\n')); fs.writeFileSync(file, js); }}