Skip to content

Commit 3ff2c8b

Browse files
committed
1 parent 0ad917e commit 3ff2c8b

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ toc.insert = require('./lib/insert');
4444

4545
function generate(options) {
4646
var opts = utils.merge({firsth1: true, maxdepth: 6}, options);
47-
var seen = opts.seen = {};
4847
var stripFirst = opts.firsth1 === false;
4948
if (opts.linkify !== false) opts.linkify = true;
5049

5150
return function(md) {
5251
md.renderer.render = function(tokens) {
5352
tokens = tokens.slice();
53+
var seen = {};
5454
var len = tokens.length, i = 0, num = 0;
5555
var tocstart = -1;
5656
var arr = [];
@@ -92,7 +92,7 @@ function generate(options) {
9292
seen[val]++;
9393
}
9494

95-
tok.seen = seen[val];
95+
tok.seen = opts.num = seen[val];
9696
tok.slug = utils.slugify(val, opts);
9797
res.json.push(utils.pick(tok, ['content', 'slug', 'lvl', 'i', 'seen']));
9898
if (opts.linkify) tok = linkify(tok, opts);
@@ -178,13 +178,12 @@ function highest(arr) {
178178
* Turn headings into anchors
179179
*/
180180

181-
function linkify(tok, opts) {
181+
function linkify(tok, options) {
182+
var opts = utils.merge({}, options);
182183
if (tok && tok.content) {
184+
opts.num = tok.seen;
183185
var text = titleize(tok.content, opts);
184186
var slug = utils.slugify(tok.content, opts);
185-
if (tok.seen) {
186-
slug += '-' + tok.seen;
187-
}
188187
slug = querystring.escape(slug);
189188
if (opts && typeof opts.linkify === 'function') {
190189
return opts.linkify(tok, text, slug, opts);
@@ -212,7 +211,8 @@ function titleize(str, opts) {
212211
}
213212
str = utils.getTitle(str);
214213
str = str.split(/<\/?[^>]+>/).join('');
215-
return str.replace(/[ \t]+/g, ' ').trim();
214+
str = str.split(/[ \t]+/).join(' ');
215+
return str.trim();
216216
}
217217

218218
/**

lib/utils.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ require = utils;
1313
* Lazily required module dependencies
1414
*/
1515

16-
require('minimist');
17-
require('remarkable', 'Remarkable');
18-
require('repeat-string', 'repeat');
19-
require('markdown-link', 'mdlink');
2016
require('concat-stream', 'concat');
2117
require('gray-matter', 'matter');
22-
require('object.pick', 'pick');
23-
require('mixin-deep', 'merge');
2418
require('list-item', 'li');
19+
require('markdown-link', 'mdlink');
20+
require('minimist');
21+
require('mixin-deep', 'merge');
22+
require('object.pick', 'pick');
23+
require('remarkable', 'Remarkable');
24+
require('repeat-string', 'repeat');
2525
require('strip-color');
2626
require = fn;
2727

@@ -58,12 +58,15 @@ utils.slugify = function(str, options) {
5858
str = utils.stripColor(str);
5959
str = str.toLowerCase();
6060
// `.split()` is often (but not always) faster than `.replace()`
61-
str = str.split(/ /).join('-');
61+
str = str.split(' ').join('-');
6262
str = str.split(/\t/).join('--');
6363
str = str.split(/<\/?[^>]+>/).join('');
6464
str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
6565
str = str.split(/[ ]/).join('');
6666
str = diacritics.removeDiacritics(str);
67+
if (options.num) {
68+
str += '-' + options.num;
69+
}
6770
return str;
6871
};
6972

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ describe('toc', function() {
153153
].join('\n'));
154154
});
155155

156+
it('should increment duplicate headings:', function() {
157+
assert.equal(toc('### AAA\n### AAA\n### AAA').json[0].slug, 'aaa');
158+
assert.equal(toc('### AAA\n### AAA\n### AAA').json[1].slug, 'aaa-1');
159+
assert.equal(toc('### AAA\n### AAA\n### AAA').json[2].slug, 'aaa-2');
160+
});
161+
156162
it('should allow and ignore empty headings:', function() {
157163
assert.equal(toc('#\n# \n# AAA\n# BBB\nfoo\nbar\nbaz#\n').content, [
158164
'- [AAA](#aaa)',

0 commit comments

Comments
 (0)