Skip to content
Myr

toc.js

This file

Path

support/toc.js

44 lines · 1.5 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 9028cac point at it.

Fixed; ignore body on 304. Closes #701Tj Holowaychukbefore adoption9028cac

lines 1–44why, one step back
1#!/usr/bin/env node
23var fs = require('fs'),4 file = process.argv[2];56if (file) {7 var html = fs.readFileSync(file, 'utf8')8 , toc = ['<ul id="toc">']9 , sections = {};1011 html = html.replace(/<h3>(.*?)<\/h3>/g, function(_, title){12 var id = title.toLowerCase().replace(' ', '-').replace(/\(.*?\)/, '()');13 if (~title.indexOf('.')) {14 var parts = title.split('.')15 , recv = parts.shift()16 , method = parts.shift().replace(/\(.*?\)/, '()');17 if (recv == 'app') recv = 'Server';18 if (recv == 'req') recv = 'Request';19 if (recv == 'res') recv = 'Response';20 sections[recv] = sections[recv] || [];21 sections[recv].push('<li><a href="#' + id + '">' + method + '</a></li>');22 } else {23 toc.push('<li><a href="#' + id + '">' + title + '</a></li>');24 }25 return '<h3 id="' + id + '">' + title + '</h3>';26 });2728 toc.push(renderSections());29 toc.push('</ul>');30 html = html.replace('<div id="container">', '<div id="container">' + toc.join('\n'));31 fs.writeFileSync(file, html);32}3334function renderSections() {35 var buf = [];36 Object.keys(sections).forEach(function(section){37 var methods = sections[section],38 a = '<a href="#" class="toggle">+</a> <a class="section-title" href="#">' + section + '</a>';39 buf.push('<li>' + a + '<ul class="section" id="section-' + section + '">');40 buf.push(methods.join('\n'));41 buf.push('</ul></li>');42 });43 return buf.join('\n');44}