Skip to content

Commit

Permalink
more sensible toc replacement
Browse files Browse the repository at this point in the history
- when replacing an existing toc all headers above it are ignored
- removed some unnecessary underscore usage
  • Loading branch information
thlorenz committed Mar 7, 2014
1 parent c5c5465 commit 03f8273
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
40 changes: 26 additions & 14 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function addAnchor(mode, header) {
}


function getHashedHeaders (_lines) {
function getHashedHeaders (lines) {
var inCodeBlock = false;

// Turn all headers into '## xxx' even if they were '## xxx ##'
Expand All @@ -33,7 +33,7 @@ function getHashedHeaders (_lines) {
}

// Find headers of the form '### xxxx xxx xx [###]'
return _lines
return lines
.filter(function (x) {
if (x.match(/^```/)) {
inCodeBlock = !inCodeBlock;
Expand All @@ -51,16 +51,15 @@ function getHashedHeaders (_lines) {
: null;
})
.filter(notNull)
.value();
}

function getUnderlinedHeaders (_lines) {
function getUnderlinedHeaders (lines) {
// Find headers of the form
// h1 h2
// == --

return _lines
.map(function (line, index, lines) {
return lines
.map(function (line, index, lines_) {
if (index === 0) return null;
var rank;

Expand All @@ -70,12 +69,11 @@ function getUnderlinedHeaders (_lines) {

return {
rank : rank,
name : lines[index - 1],
name : lines_[index - 1],
line : index - 1
};
})
.filter(notNull)
.value();
}

function countHeaders (headers) {
Expand All @@ -97,13 +95,28 @@ function countHeaders (headers) {
return headers;
}

function getLinesToToc (lines, currentToc, info) {
if (!currentToc) return lines;

var tocableStart = 0;

// when updating an existing toc, we only take the headers into account
// that are below the existing toc
if (info.hasEnd) tocableStart = info.endIdx;

return lines.slice(tocableStart);
}

exports = module.exports = function transform(content, mode) {
var lines = content.split('\n')
, _lines = _(lines).chain();

mode = mode || 'github.com';

var headers = getHashedHeaders(_lines).concat(getUnderlinedHeaders(_lines));
var lines = content.split('\n')
, info = updateSection.parse(lines, matchesStart, matchesEnd)

var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx).join('\n')
, linesToToc = getLinesToToc(lines, currentToc, info);

var headers = getHashedHeaders(linesToToc).concat(getUnderlinedHeaders(linesToToc));

headers.sort(function (a, b) {
return a.line - b.line;
Expand All @@ -115,6 +128,7 @@ exports = module.exports = function transform(content, mode) {

if (linkedHeaders.length === 0) return { transformed: false };


var toc =
'**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*'
+ '\n\n'
Expand All @@ -129,9 +143,7 @@ exports = module.exports = function transform(content, mode) {
+ '\n';

var wrappedToc = start + '\n' + toc + '\n' + end;
var info = updateSection.parse(lines, matchesStart, matchesEnd);

var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx).join('\n');
if (currentToc === toc) return { transformed: false };

var data = updateSection(lines.join('\n'), wrappedToc, matchesStart, matchesEnd, true);
Expand Down
22 changes: 16 additions & 6 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function check(md, anchors, mode) {
t.end()
})
}
//function check() {}

check(
[ '# My Module'
Expand Down Expand Up @@ -160,8 +161,9 @@ check(

test('transforming when old toc exists', function (t) {
var md = [
'## Header'
, 'some content'
'# Header above'
, ''
, 'The above header should be ignored since it is above the existing toc'
, ''
, '<!-- START doctoc generated TOC please keep comment here to allow auto update -->'
, '<!-- DON\'T EDIT THIS SECTION INSTEAD RE-RUN doctoc TO UPDATE -->'
Expand All @@ -170,11 +172,15 @@ test('transforming when old toc exists', function (t) {
, '- [OldHeader](#oldheader)'
, ''
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '## Header'
, 'some content'
, ''
].join('\n')

var res = transform(md)

t.ok(res.transformed, 'transforms it')

t.deepEqual(
res.toc.split('\n')
, [ '**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
Expand All @@ -199,17 +205,21 @@ test('transforming when old toc exists', function (t) {

t.deepEqual(
res.data.split('\n')
, [ '## Header',
'some content',
, [ '# Header above',
'',
'The above header should be ignored since it is above the existing toc',
'',
'<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
'**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*',
'',
'- [Header](#header)',
'',
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->' ]
, 'updates the content with the new toc'
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->',
'## Header',
'some content',
'' ]
, 'updates the content with the new toc and ignores header before existing toc'
)
t.end()
})
Expand Down

0 comments on commit 03f8273

Please sign in to comment.