From 1f48ce31506a3de7fbaa34bdcd80165ee22f7079 Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Wed, 23 Apr 2014 16:31:15 -0400 Subject: [PATCH] converting tabs to spaces and defaulting to h4 as smallest linked html header --- lib/get-html-headers.js | 19 ++++++++++++------- lib/transform.js | 7 ++++--- test/transform-html.js | 39 ++++++++++++++++++++++++++++++--------- test/transform.js | 28 ++++++++++++++-------------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/lib/get-html-headers.js b/lib/get-html-headers.js index b38c54a..e1eba9a 100644 --- a/lib/get-html-headers.js +++ b/lib/get-html-headers.js @@ -18,14 +18,18 @@ function addLinenos(lines, headers) { }) } -function rankify(headers) { - return headers.map(function (x) { - x.rank = parseInt(x.tag.slice(1), 10); - return x; - }) +function rankify(headers, max) { + return headers + .map(function (x) { + x.rank = parseInt(x.tag.slice(1), 10); + return x; + }) + .filter(function (x) { + return x.rank <= max; + }) } -var go = module.exports = function (lines) { +var go = module.exports = function (lines, maxHeaderNo) { var md = lines.join('\n'); var headers = [], grabbing = null, text = []; @@ -54,6 +58,7 @@ var go = module.exports = function (lines) { parser.end(); headers = addLinenos(lines, headers) - headers = rankify(headers); + // consider anything past h4 to small to warrant a link, may be made configurable in the future + headers = rankify(headers, maxHeaderNo); return headers; } diff --git a/lib/transform.js b/lib/transform.js index 3acabea..ae8892a 100644 --- a/lib/transform.js +++ b/lib/transform.js @@ -112,8 +112,9 @@ function getLinesToToc (lines, currentToc, info) { return lines.slice(tocableStart); } -exports = module.exports = function transform(content, mode) { +exports = module.exports = function transform(content, mode, maxHeaderNo) { mode = mode || 'github.com'; + maxHeaderNo = maxHeaderNo || 4; var lines = content.split('\n') , info = updateSection.parse(lines, matchesStart, matchesEnd) @@ -123,7 +124,7 @@ exports = module.exports = function transform(content, mode) { var headers = getHashedHeaders(linesToToc) .concat(getUnderlinedHeaders(linesToToc)) - .concat(getHtmlHeaders(linesToToc)) + .concat(getHtmlHeaders(linesToToc, maxHeaderNo)) headers.sort(function (a, b) { return a.line - b.line; @@ -141,7 +142,7 @@ exports = module.exports = function transform(content, mode) { + linkedHeaders .map(function (x) { var indent = _(_.range(x.rank - lowestRank)) - .reduce(function (acc, x) { return acc + '\t'; }, ''); + .reduce(function (acc, x) { return acc + ' '; }, ''); return indent + '- ' + x.anchor; }) diff --git a/test/transform-html.js b/test/transform-html.js index 67cc223..5006d38 100644 --- a/test/transform-html.js +++ b/test/transform-html.js @@ -4,9 +4,9 @@ var test = require('tap').test , transform = require('../lib/transform') -test('\ngiven a file that includes html with header tags', function (t) { +test('\ngiven a file that includes html with header tags and maxHeaderNo 8', function (t) { var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-html.md', 'utf8'); - var headers = transform(content); + var headers = transform(content, 'github.com', 8); t.deepEqual( headers.toc.split('\n') @@ -14,13 +14,13 @@ test('\ngiven a file that includes html with header tags', function (t) { '', '- [Installation](#installation)', '- [API](#api)', - '\t\t- [dockops::Containers(docker) → {Object}](#dockopscontainersdocker-→-{object})', - '\t\t\t- [Parameters:](#parameters)', - '\t\t\t- [Returns:](#returns)', - '\t\t- [dockops::Containers::activePorts(cb)](#dockopscontainersactiveportscb)', - '\t\t\t- [Parameters:](#parameters-1)', - '\t\t- [dockops::Containers::clean(id, cb)](#dockopscontainerscleanid-cb)', - '\t\t\t- [Parameters:](#parameters-2)', + ' - [dockops::Containers(docker) → {Object}](#dockopscontainersdocker-→-{object})', + ' - [Parameters:](#parameters)', + ' - [Returns:](#returns)', + ' - [dockops::Containers::activePorts(cb)](#dockopscontainersactiveportscb)', + ' - [Parameters:](#parameters-1)', + ' - [dockops::Containers::clean(id, cb)](#dockopscontainerscleanid-cb)', + ' - [Parameters:](#parameters-2)', '- [License](#license)', '' ] , 'generates correct toc for non html and html headers' @@ -28,3 +28,24 @@ test('\ngiven a file that includes html with header tags', function (t) { t.end() }) + +test('\ngiven a file that includes html with header tags using default maxHeaderNo', function (t) { + var content = require('fs').readFileSync(__dirname + '/fixtures/readme-with-html.md', 'utf8'); + var headers = transform(content); + + t.deepEqual( + headers.toc.split('\n') + , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*', + '', + '- [Installation](#installation)', + '- [API](#api)', + ' - [dockops::Containers(docker) → {Object}](#dockopscontainersdocker-→-{object})', + ' - [dockops::Containers::activePorts(cb)](#dockopscontainersactiveportscb)', + ' - [dockops::Containers::clean(id, cb)](#dockopscontainerscleanid-cb)', + '- [License](#license)', + '' ] + , 'generates correct toc for non html and html headers omitting headers larger than maxHeaderNo' + ) + + t.end() +}) diff --git a/test/transform.js b/test/transform.js index 55b94e2..16224ec 100644 --- a/test/transform.js +++ b/test/transform.js @@ -46,10 +46,10 @@ check( ].join('\n') , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n' , '- [My Module](#my-module)\n' - , '\t- [API](#api)\n' - , '\t\t- [Method One](#method-one)\n' - , '\t\t- [Method Two](#method-two)\n' - , '\t\t\t- [Main Usage](#main-usage)\n\n\n' + , ' - [API](#api)\n' + , ' - [Method One](#method-one)\n' + , ' - [Method Two](#method-two)\n' + , ' - [Main Usage](#main-usage)\n\n\n' ].join('') ) @@ -65,10 +65,10 @@ check( ].join('\r\n') , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n' , '- [My Module using \\r\\n line endings](#my-module-using-\\r\\n-line-endings)\n' - , '\t- [API](#api)\n' - , '\t\t- [Method One](#method-one)\n' - , '\t\t- [Method Two](#method-two)\n' - , '\t\t\t- [Main Usage](#main-usage)\n\n\n' + , ' - [API](#api)\n' + , ' - [Method One](#method-one)\n' + , ' - [Method Two](#method-two)\n' + , ' - [Main Usage](#main-usage)\n\n\n' ].join('') ) @@ -81,7 +81,7 @@ check( ].join('\n') , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n' , '- [My Module](#my-module)\n' - , '\t- [API](#api)\n\n\n' + , ' - [API](#api)\n\n\n' ].join('') ) @@ -92,7 +92,7 @@ check( ].join('\n') , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n' , '- [My Module](#my-module)\n' - , '\t- [API](#api)\n\n\n' + , ' - [API](#api)\n\n\n' ].join('') ) @@ -237,10 +237,10 @@ check( ].join('\n') , [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*\n\n' , '- [My Module](#markdown-header-my-module)\n' - , '\t- [API](#markdown-header-api)\n' - , '\t\t- [Method One](#markdown-header-method-one)\n' - , '\t\t- [Method Two](#markdown-header-method-two)\n' - , '\t\t\t- [Main Usage](#markdown-header-main-usage)\n\n\n' + , ' - [API](#markdown-header-api)\n' + , ' - [Method One](#markdown-header-method-one)\n' + , ' - [Method Two](#markdown-header-method-two)\n' + , ' - [Main Usage](#markdown-header-main-usage)\n\n\n' ].join('') , 'bitbucket.org' )