Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 0bb7b7c point at it.

Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c

lines 1–8why, one step back
1#!/usr/bin/env node
23var 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 back
9 toc = ['<ul id="toc">'],
10 sections = {};

Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c

lines 11–14why, one step back
11 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 back
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 }

Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c

line 28why, one step back
28 });

Merge branch 'guide'Tj Holowaychukbefore adoptionf4a0809

line 29why, one step back
29 toc.push(renderSections());

Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c

lines 30–33why, one step back
30 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 back
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');

Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c

line 46why, one step back
46}