Skip to content

Commit

Permalink
use diacritics-map lib
Browse files Browse the repository at this point in the history
(I benchmarked this locally, it was 30x faster than the current code)

also allow users to disable stripping HTML tags with `stripHeadingtags` option
  • Loading branch information
jonschlinkert committed Jan 15, 2017
1 parent ce2ad54 commit 389e1e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 105 deletions.
102 changes: 0 additions & 102 deletions lib/diacritics.js

This file was deleted.

15 changes: 12 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies
*/

var diacritics = require('./diacritics');
var diacritics = require('diacritics-map');
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
Expand Down Expand Up @@ -57,19 +57,28 @@ utils.slugify = function(str, options) {
str = utils.getTitle(str);
str = utils.stripColor(str);
str = str.toLowerCase();

// `.split()` is often (but not always) faster than `.replace()`
str = str.split(' ').join('-');
str = str.split(/\t/).join('--');
str = str.split(/<\/?[^>]+>/).join('');
if (options.stripHeadingTags !== false) {
str = str.split(/<\/?[^>]+>/).join('');
}
str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
str = str.split(/[。?!,、;:“”【】()〔〕[]﹃﹄“ ”‘’﹁﹂—…-~《》〈〉「」]/).join('');
str = diacritics.removeDiacritics(str);
str = replaceDiacritics(str);
if (options.num) {
str += '-' + options.num;
}
return str;
};

function replaceDiacritics(str) {
return str.replace(/[À-ž]/g, function(ch) {
return diacritics[ch] || ch;
});
}

/**
* Expose `utils` modules
*/
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ describe('options: custom functions:', function() {
assert.equal(toc('# Foo <test>').content, '- [Foo](#foo-)');
});

it('should not strip HTML tags from headings when `stripHeadingTags` is false', function() {
var opts = {stripHeadingTags: false};
assert.equal(toc('# <test>Foo', opts).content, '- [Foo](#testfoo)');
assert.equal(toc('# <test> Foo', opts).content, '- [Foo](#test-foo)');
assert.equal(toc('# <test> Foo ', opts).content, '- [Foo](#test-foo)');
assert.equal(toc('# <div> Foo </div>', opts).content, '- [Foo](#div-foo-div)');
assert.equal(toc('# Foo <test>', opts).content, '- [Foo](#foo-test)');
});

it('should condense spaces in the heading text', function() {
var actual = toc('# Some Article');
assert.equal(actual.content, '- [Some Article](#some----article)');
Expand Down

0 comments on commit 389e1e7

Please sign in to comment.