Skip to content

Instantly share code, notes, and snippets.

@kobake
Created May 10, 2017 01:42
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/6e4695b981c65d6082410ce00c5430d3 to your computer and use it in GitHub Desktop.
Save kobake/6e4695b981c65d6082410ce00c5430d3 to your computer and use it in GitHub Desktop.
GitHub の Markdown に TOC (目次) を付けるブックマークレット(旧jQuery版。2017年5月現在以降はこのスクリプトは機能しなくなりました)
// 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