Skip to content

Commit

Permalink
Markdown renderer: Transform non-ASCII characters into unicode in hea…
Browse files Browse the repository at this point in the history
…ding
  • Loading branch information
tommy351 committed Dec 28, 2013
1 parent edb2b2c commit 3037755
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/plugins/renderer/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@ var marked = require('marked'),
highlight = util.highlight,
htmlTag = util.html_tag;

var anchorId = function(str){
return str
.replace(/\s+/g, '_')
.replace(/\./g, '-')
.replace(/[^0-9a-zA-Z_-]/g, function(match){
return '-' + match.charCodeAt().toString(16) + '-';
})
.replace(/-{2,}/g, '-');
};

module.exports = function(data, options){
var r = new marked.Renderer(),
headingId = {};

// Add id attribute to headings
r.heading = function(text, level){
var id = anchorId(text);

// Add a number after id if repeated
if (headingId[text]){
id = text + '-' + headingId[text];
if (headingId.hasOwnProperty(id)){
id += '-' + headingId[id];
headingId[text]++;
} else {
id = text;
headingId[text] = 1;
headingId[id] = 1;
}

return htmlTag('h' + level, {id: id}, text) + '\n';
Expand Down

0 comments on commit 3037755

Please sign in to comment.