1
0
Fork 0
forked from forgejo/forgejo

add anchor

This commit is contained in:
slene 2014-03-20 17:25:48 +08:00
parent 8ef71343df
commit c3532718a7
2 changed files with 76 additions and 4 deletions

View file

@ -66,9 +66,28 @@ var Gogits = {
// render markdown
Gogits.renderMarkdown = function () {
var $pre = $('.markdown').find('pre > code').parent();
var $md = $('.markdown');
var $pre = $md.find('pre > code').parent();
$pre.addClass("prettyprint");
prettyPrint();
// Set anchor.
var headers = {};
$md.find('h1, h2, h3, h4, h5, h6').each(function () {
var node = $(this);
var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
var name = val;
if(headers[val] > 0){
name = val + '-' + headers[val];
}
if(headers[val] == undefined){
headers[val] = 1;
}else{
headers[val] += 1;
}
node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
});
}
})(jQuery);