toc.js
This file
Path
support/toc.js
46 lines · 1.7 KiB · JavaScript
Last changed by
Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
History of this fileTake it
The walk stopped after 200 commits: lines older than 0bb7b7c point at it.
Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 1–8why, 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),Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
lines 9–10why, one step back9 toc = ['<ul id="toc">'],10 sections = {};Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 11–14why, one step back11 if (js.indexOf('id="toc"') < 0) {12 headers.forEach(function(header){13 var captures = header.match(/id="(.*?)">(.*?)</),14 id = captures[1],Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
lines 15–27why, one step back15 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 }Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
line 28why, one step back28 });Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
line 29why, one step back29 toc.push(renderSections());Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 30–33why, one step back30 toc.push('</ul>');31 js = js.replace('<div id="container">', '<div id="container">' + toc.join('\n'));32 fs.writeFileSync(file, js);33 }Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809
lines 34–45why, one step back34}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');Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
line 46why, one step back46}