toc.js
This file
Path
support/toc.js
46 lines · 1.7 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than e562adc point at it.
Settings docsTj Holowaychukbefore adoptione562adc
lines 1–46why, one step back1#!/usr/bin/env node23var fs = require('fs'),4 file = process.argv[2];56if (file) {7 var js = fs.readFileSync(file, 'utf8'),8 headers = js.match(/<h3 id="(.*?)">(.*?)<\/h3>/g),9 toc = ['<ul id="toc">'],10 sections = {};11 if (js.indexOf('id="toc"') < 0) {12 headers.forEach(function(header){13 var captures = header.match(/id="(.*?)">(.*?)</),14 id = captures[1],15 title = captures[2].replace(/\(.*?\)/, '()')16 if (~title.indexOf('.')) {17 var parts = title.split('.'),18 recv = parts.shift(),19 method = parts.shift();20 if (recv == 'app') recv = 'Server';21 if (recv == 'req') recv = 'Request';22 if (recv == 'res') recv = 'Response';23 sections[recv] = sections[recv] || [];24 sections[recv].push('<li><a href="#' + id + '">' + method + '</a></li>');25 } else {26 toc.push('<li><a href="#' + id + '">' + title + '</a></li>');27 }28 });29 toc.push(renderSections());30 toc.push('</ul>');31 js = js.replace('<div id="container">', '<div id="container">' + toc.join('\n'));32 fs.writeFileSync(file, js);33 }34}3536function renderSections() {37 var buf = [];38 Object.keys(sections).forEach(function(section){39 var methods = sections[section],40 a = '<a href="#" class="toggle">+</a> <a class="section-title" href="#">' + section + '</a>';41 buf.push('<li>' + a + '<ul class="section" id="section-' + section + '">');42 buf.push(methods.join('\n'));43 buf.push('</ul></li>');44 });45 return buf.join('\n');46}