Added support of toc syntax

This commit is contained in:
Wu Cheng-Han 2015-12-20 11:28:54 -06:00
parent 45ec05e2ed
commit 9eb23603f4
4 changed files with 48 additions and 3 deletions

View file

@ -341,6 +341,7 @@ function generateToc(id) {
'level': 3, 'level': 3,
'top': -1, 'top': -1,
'class': 'toc', 'class': 'toc',
'ulClass': 'nav',
'targetId': id 'targetId': id
}); });
if (target.text() == 'undefined') if (target.text() == 'undefined')
@ -460,6 +461,26 @@ function deduplicatedHeaderId(view) {
} }
} }
function renderTOC(view) {
var tocs = view.find('.toc').toArray();
for (var i = 0; i < tocs.length; i++) {
var toc = $(tocs[i]);
var id = 'toc' + i;
toc.attr('id', id);
var target = $('#' + id);
target.html('');
new Toc('doc', {
'level': 3,
'top': -1,
'class': 'toc',
'targetId': id
});
if (target.text() == 'undefined')
target.html('');
target.replaceWith(target.html());
}
}
function scrollToHash() { function scrollToHash() {
var hash = location.hash; var hash = location.hash;
location.hash = ""; location.hash = "";
@ -623,7 +644,18 @@ var mathjaxPlugin = new Plugin(
return '<span class="mathjax raw">' + match[0] + '</span>'; return '<span class="mathjax raw">' + match[0] + '</span>';
} }
); );
//TOC
var tocPlugin = new Plugin(
// regexp to match
/^\[TOC\]$/,
// this function will be called when something matches
function (match, utils) {
return '<div class="toc"></div>';
}
);
md.use(youtubePlugin); md.use(youtubePlugin);
md.use(vimeoPlugin); md.use(vimeoPlugin);
md.use(gistPlugin); md.use(gistPlugin);
md.use(mathjaxPlugin); md.use(mathjaxPlugin);
md.use(tocPlugin);

View file

@ -86,6 +86,10 @@ var supportReferrals = [
{ {
text: '![image text](url "title")', text: '![image text](url "title")',
search: '![]()' search: '![]()'
},
{
text: '[TOC]',
search: '[]'
} }
]; ];
var supportExternals = [ var supportExternals = [
@ -1876,6 +1880,7 @@ function updateViewInner() {
finishView(ui.area.view); finishView(ui.area.view);
autoLinkify(ui.area.view); autoLinkify(ui.area.view);
deduplicatedHeaderId(ui.area.view); deduplicatedHeaderId(ui.area.view);
renderTOC(ui.area.view);
generateToc('toc'); generateToc('toc');
generateToc('toc-affix'); generateToc('toc-affix');
generateScrollspy(); generateScrollspy();

View file

@ -6,6 +6,7 @@ $(document.body).show();
finishView(markdown); finishView(markdown);
autoLinkify(markdown); autoLinkify(markdown);
deduplicatedHeaderId(markdown); deduplicatedHeaderId(markdown);
renderTOC(markdown);
generateToc('toc'); generateToc('toc');
generateToc('toc-affix'); generateToc('toc-affix');
smoothHashScroll(); smoothHashScroll();

View file

@ -10,6 +10,7 @@
this.options = options || {}; this.options = options || {};
this.tocLevel = parseInt(options.level) || 0; this.tocLevel = parseInt(options.level) || 0;
this.tocClass = options['class'] || 'toc'; this.tocClass = options['class'] || 'toc';
this.ulClass = options['ulClass'];
this.tocTop = parseInt(options.top) || 0; this.tocTop = parseInt(options.top) || 0;
this.elChilds = this.el.children; this.elChilds = this.el.children;
if (!this.elChilds.length) return; if (!this.elChilds.length) return;
@ -80,7 +81,10 @@
this._tempLists.length = this._tempLists.length - y; this._tempLists.length = this._tempLists.length - y;
} else { } else {
this._tempLists.push(this._elTitleElement); this._tempLists.push(this._elTitleElement);
this.tocContent += '<ul class="nav">'; if (this.ulClass)
this.tocContent += '<ul class="' + this.ulClass + '">';
else
this.tocContent += '<ul>';
} }
} else { } else {
this.tocContent += '</li>'; this.tocContent += '</li>';
@ -93,7 +97,10 @@
} }
} }
} }
this.tocContent = '<ul class="nav">' + this.tocContent + '</ul>'; if (this.ulClass)
this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>';
else
this.tocContent = '<ul>' + this.tocContent + '</ul>';
}; };
Toc.prototype._showToc = function () { Toc.prototype._showToc = function () {