Skip to content
Myr

toc.js

This file

Path

support/toc.js

44 lines · 1.5 KiB · JavaScript

Last changed by

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

History of this file

Take it

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

Fixed partials exampleTj Holowaychukbefore adoptiond5b7a40

lines 1–6why, one step back
1#!/usr/bin/env node
23var fs = require('fs'),4 file = process.argv[2];56if (file) {

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

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

Fixed partials exampleTj Holowaychukbefore adoptiond5b7a40

lines 32–34why, one step back
32}
3334function renderSections() {

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

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

Fixed partials exampleTj Holowaychukbefore adoptiond5b7a40

line 44why, one step back
44}