Skip to content

Commit

Permalink
add modifyAnchors option (#24)
Browse files Browse the repository at this point in the history
* add modifyAnchors option

* fix small bug and add tests for new feature

* fix style problem
  • Loading branch information
Pashokus authored and NoahDragon committed Apr 2, 2017
1 parent a85a54c commit f7a9a6e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ marked:
breaks: true
smartLists: true
smartypants: true
modifyAnchors: ''
```
- **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown)
Expand All @@ -35,6 +36,7 @@ marked:
- **breaks** - Enable GFM [line breaks](https://help.github.com/articles/github-flavored-markdown#newlines). This option requires the `gfm` option to be true.
- **smartLists** - Use smarter list behavior than the original markdown.
- **smartypants** - Use "smart" typograhic punctuation for things like quotes and dashes.
- **modifyAnchors** - Use for transform anchorIds. if 1 to lowerCase and if 2 to upperCase.

[Markdown]: http://daringfireball.net/projects/markdown/
[marked]: https://github.com/chjj/marked
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ hexo.config.marked = assign({
tables: true,
breaks: true,
smartLists: true,
smartypants: true
smartypants: true,
modifyAnchors: ''
}, hexo.config.marked);

hexo.extend.renderer.register('md', 'html', renderer, true);
Expand Down
7 changes: 4 additions & 3 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ require('util').inherits(Renderer, MarkedRenderer);

// Add id attribute to headings
Renderer.prototype.heading = function(text, level) {
var id = anchorId(stripHTML(text));
var transformOption = this.options.modifyAnchors;
var id = anchorId(stripHTML(text), transformOption);
var headingId = this._headingId;

// Add a number after id if repeated
Expand All @@ -32,8 +33,8 @@ Renderer.prototype.heading = function(text, level) {
return '<h' + level + ' id="' + id + '"><a href="#' + id + '" class="headerlink" title="' + stripHTML(text) + '"></a>' + text + '</h' + level + '>';
};

function anchorId(str) {
return util.slugize(str.trim());
function anchorId(str, transformOption) {
return util.slugize(str.trim(), {transform: transformOption});
}

marked.setOptions({
Expand Down
56 changes: 56 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,60 @@ describe('Marked renderer', function() {

result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
});

describe('modifyAnchors option tests', function() {
var body = [
'- [Example](#example)',
'',
'# Example'
].join('\n');

var renderer = require('../lib/renderer');

var ctx = {
config: {
marked: {
modifyAnchors: ''
}
}
};

it('should not modify anchors with default options', function() {
var r = renderer.bind(ctx);
var result = r({text: body});

result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"Example\"><a href=\"#Example\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});

it('should set anchors to upperCase in case of modifyAnchors option is 2', function() {
ctx.config.marked.modifyAnchors = 2;
var r = renderer.bind(ctx);
var result = r({text: body});

result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"EXAMPLE\"><a href=\"#EXAMPLE\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});

it('should set anchors to lowerCase in case of modifyAnchors option is 1', function() {
ctx.config.marked.modifyAnchors = 1;
var r = renderer.bind(ctx);
var result = r({text: body});

result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"example\"><a href=\"#example\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});
});
});

0 comments on commit f7a9a6e

Please sign in to comment.