Skip to content

Commit

Permalink
converting tabs to spaces and defaulting to h4 as smallest linked htm…
Browse files Browse the repository at this point in the history
…l header
  • Loading branch information
thlorenz committed Apr 23, 2014
1 parent 9e4846f commit 1f48ce3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
19 changes: 12 additions & 7 deletions lib/get-html-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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;
})
Expand Down
39 changes: 30 additions & 9 deletions test/transform-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@
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')
, [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
'',
'- [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'
)

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()
})
28 changes: 14 additions & 14 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
)

Expand All @@ -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('')
)

Expand All @@ -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('')
)

Expand All @@ -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('')
)

Expand Down Expand Up @@ -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'
)

0 comments on commit 1f48ce3

Please sign in to comment.