Skip to content

Commit 389e1e7

Browse files
committed
use diacritics-map lib
(I benchmarked this locally, it was 30x faster than the current code) also allow users to disable stripping HTML tags with `stripHeadingtags` option
1 parent ce2ad54 commit 389e1e7

File tree

3 files changed

+21
-105
lines changed

3 files changed

+21
-105
lines changed

lib/diacritics.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

lib/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Module dependencies
55
*/
66

7-
var diacritics = require('./diacritics');
7+
var diacritics = require('diacritics-map');
88
var utils = require('lazy-cache')(require);
99
var fn = require;
1010
require = utils;
@@ -57,19 +57,28 @@ utils.slugify = function(str, options) {
5757
str = utils.getTitle(str);
5858
str = utils.stripColor(str);
5959
str = str.toLowerCase();
60+
6061
// `.split()` is often (but not always) faster than `.replace()`
6162
str = str.split(' ').join('-');
6263
str = str.split(/\t/).join('--');
63-
str = str.split(/<\/?[^>]+>/).join('');
64+
if (options.stripHeadingTags !== false) {
65+
str = str.split(/<\/?[^>]+>/).join('');
66+
}
6467
str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
6568
str = str.split(/[ ]/).join('');
66-
str = diacritics.removeDiacritics(str);
69+
str = replaceDiacritics(str);
6770
if (options.num) {
6871
str += '-' + options.num;
6972
}
7073
return str;
7174
};
7275

76+
function replaceDiacritics(str) {
77+
return str.replace(/[À-ž]/g, function(ch) {
78+
return diacritics[ch] || ch;
79+
});
80+
}
81+
7382
/**
7483
* Expose `utils` modules
7584
*/

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ describe('options: custom functions:', function() {
100100
assert.equal(toc('# Foo <test>').content, '- [Foo](#foo-)');
101101
});
102102

103+
it('should not strip HTML tags from headings when `stripHeadingTags` is false', function() {
104+
var opts = {stripHeadingTags: false};
105+
assert.equal(toc('# <test>Foo', opts).content, '- [Foo](#testfoo)');
106+
assert.equal(toc('# <test> Foo', opts).content, '- [Foo](#test-foo)');
107+
assert.equal(toc('# <test> Foo ', opts).content, '- [Foo](#test-foo)');
108+
assert.equal(toc('# <div> Foo </div>', opts).content, '- [Foo](#div-foo-div)');
109+
assert.equal(toc('# Foo <test>', opts).content, '- [Foo](#foo-test)');
110+
});
111+
103112
it('should condense spaces in the heading text', function() {
104113
var actual = toc('# Some Article');
105114
assert.equal(actual.content, '- [Some Article](#some----article)');

0 commit comments

Comments
 (0)