Skip to content

Commit

Permalink
Add escape to linkify() so it can handle CJK characters correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Tao Wang <twang2218@gmail.com>
  • Loading branch information
twang2218 committed Aug 27, 2016
1 parent 118a660 commit f4030e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,7 @@
*/

var utils = require('./lib/utils');
var querystring = require('querystring');

/**
* expose `toc`
Expand Down Expand Up @@ -182,6 +183,7 @@ function linkify(tok, opts) {
if (tok.seen) {
slug += '-' + tok.seen;
}
slug = querystring.escape(slug);
if (opts && typeof opts.linkify === 'function') {
return opts.linkify(tok, text, slug, opts);
}
Expand Down
2 changes: 1 addition & 1 deletion test/expected/insert-options.md
Expand Up @@ -9,7 +9,7 @@ name: Test

- [Quickstart](#quickstart)
- [Options](#options)
- [Usage examples](#usage examples)
- [Usage examples](#usage%20examples)
- [Contributing](#contributing)
- [Author](#author)

Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Expand Up @@ -82,6 +82,12 @@ describe('options: custom functions:', function() {
actual.content.should.equal('- [Foo & Bar](#foo--bar)');
});

it('should escape the CJK characters in linkify', function() {
toc('# 中文').content.should.equal('- [中文](#%E4%B8%AD%E6%96%87)');
toc('# かんじ').content.should.equal('- [かんじ](#%E3%81%8B%E3%82%93%E3%81%98)');
toc('# 한자').content.should.equal('- [한자](#%ED%95%9C%EC%9E%90)');
});

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

0 comments on commit f4030e4

Please sign in to comment.