Skip to content

Instantly share code, notes, and snippets.

@kobake
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobake/63a6f99805640086928f to your computer and use it in GitHub Desktop.
Save kobake/63a6f99805640086928f to your computer and use it in GitHub Desktop.
GitHub の Markdown に TOC (目次) を付けるブックマークレット ref: http://qiita.com/kobake@github/items/3f29a0e20ae4c49e0c81
javascript:(function(){/* style */var style = (function () {/*.toc{border: 1px solid #aaa;padding: 4px 12px;margin-bottom: 12px;position: relative;padding-top: 30px;}.toc-title{display: inline-block;width: auto;background-color: #ccc;position: absolute;left: 0px;top: 0px;font-size: 8pt;padding: 2px 4px;padding-right: 8px;color: #666;}.toc-h{font-size: 11pt;}.toc-h1{}.toc-h2{margin-left: 20px;}.toc-h3{margin-left: 40px;}*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];$('.toc-style').remove();$('body').append($('<style class="toc-style">\n' + style + '\n</style>'));/* toc frame */$('.toc').remove();var $toc = $('<div class="toc"><div class="toc-title">TOC</div></div>');$toc.prependTo($('.markdown-body'));/* each links */$('.markdown-body h1, .markdown-body h2, .markdown-body h3').each(function(){/* level */var tagName = $(this).prop('tagName').toLowerCase();/* link div */var $div = $('<div class="toc-h toc-' + tagName + '"></div>');/* link */var $a = $('<a href=""></a>');$a.appendTo($div);$a.text($(this).text());$a.attr('href', $(this).find('a:first').attr('href'));/* append */$toc.append($div);});})();
// style
var style = (function () {/*
.toc{
border: 1px solid #aaa;
padding: 4px 12px;
margin-bottom: 12px;
position: relative;
padding-top: 30px;
}
.toc-title{
display: inline-block;
width: auto;
background-color: #ccc;
position: absolute;
left: 0px;
top: 0px;
font-size: 8pt;
padding: 2px 4px;
padding-right: 8px;
color: #666;
}
.toc-h{
font-size: 11pt;
}
.toc-h1{
}
.toc-h2{
margin-left: 20px;
}
.toc-h3{
margin-left: 40px;
}
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
$('.toc-style').remove();
$('body').append($('<style class="toc-style">\n' + style + '\n</style>'));
// toc frame
$('.toc').remove();
var $toc = $('<div class="toc"><div class="toc-title">TOC</div></div>');
$toc.prependTo($('.markdown-body'));
// each links
$('.markdown-body h1, .markdown-body h2, .markdown-body h3').each(function(){
// level
var tagName = $(this).prop('tagName').toLowerCase();
// link div
var $div = $('<div class="toc-h toc-' + tagName + '"></div>');
// link
var $a = $('<a href=""></a>');
$a.appendTo($div);
$a.text($(this).text());
$a.attr('href', $(this).find('a:first').attr('href'));
// append
$toc.append($div);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment