Skip to content

Commit 9eb2360

Browse files
committed
Added support of toc syntax
1 parent 45ec05e commit 9eb2360

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

public/js/extra.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ function generateToc(id) {
341341
'level': 3,
342342
'top': -1,
343343
'class': 'toc',
344+
'ulClass': 'nav',
344345
'targetId': id
345346
});
346347
if (target.text() == 'undefined')
@@ -460,6 +461,26 @@ function deduplicatedHeaderId(view) {
460461
}
461462
}
462463

464+
function renderTOC(view) {
465+
var tocs = view.find('.toc').toArray();
466+
for (var i = 0; i < tocs.length; i++) {
467+
var toc = $(tocs[i]);
468+
var id = 'toc' + i;
469+
toc.attr('id', id);
470+
var target = $('#' + id);
471+
target.html('');
472+
new Toc('doc', {
473+
'level': 3,
474+
'top': -1,
475+
'class': 'toc',
476+
'targetId': id
477+
});
478+
if (target.text() == 'undefined')
479+
target.html('');
480+
target.replaceWith(target.html());
481+
}
482+
}
483+
463484
function scrollToHash() {
464485
var hash = location.hash;
465486
location.hash = "";
@@ -623,7 +644,18 @@ var mathjaxPlugin = new Plugin(
623644
return '<span class="mathjax raw">' + match[0] + '</span>';
624645
}
625646
);
647+
//TOC
648+
var tocPlugin = new Plugin(
649+
// regexp to match
650+
/^\[TOC\]$/,
651+
652+
// this function will be called when something matches
653+
function (match, utils) {
654+
return '<div class="toc"></div>';
655+
}
656+
);
626657
md.use(youtubePlugin);
627658
md.use(vimeoPlugin);
628659
md.use(gistPlugin);
629-
md.use(mathjaxPlugin);
660+
md.use(mathjaxPlugin);
661+
md.use(tocPlugin);

public/js/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ var supportReferrals = [
8686
{
8787
text: '![image text](url "title")',
8888
search: '![]()'
89+
},
90+
{
91+
text: '[TOC]',
92+
search: '[]'
8993
}
9094
];
9195
var supportExternals = [
@@ -1876,6 +1880,7 @@ function updateViewInner() {
18761880
finishView(ui.area.view);
18771881
autoLinkify(ui.area.view);
18781882
deduplicatedHeaderId(ui.area.view);
1883+
renderTOC(ui.area.view);
18791884
generateToc('toc');
18801885
generateToc('toc-affix');
18811886
generateScrollspy();

public/js/pretty.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $(document.body).show();
66
finishView(markdown);
77
autoLinkify(markdown);
88
deduplicatedHeaderId(markdown);
9+
renderTOC(markdown);
910
generateToc('toc');
1011
generateToc('toc-affix');
1112
smoothHashScroll();

public/vendor/md-toc.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
this.options = options || {};
1111
this.tocLevel = parseInt(options.level) || 0;
1212
this.tocClass = options['class'] || 'toc';
13+
this.ulClass = options['ulClass'];
1314
this.tocTop = parseInt(options.top) || 0;
1415
this.elChilds = this.el.children;
1516
if (!this.elChilds.length) return;
@@ -80,7 +81,10 @@
8081
this._tempLists.length = this._tempLists.length - y;
8182
} else {
8283
this._tempLists.push(this._elTitleElement);
83-
this.tocContent += '<ul class="nav">';
84+
if (this.ulClass)
85+
this.tocContent += '<ul class="' + this.ulClass + '">';
86+
else
87+
this.tocContent += '<ul>';
8488
}
8589
} else {
8690
this.tocContent += '</li>';
@@ -93,7 +97,10 @@
9397
}
9498
}
9599
}
96-
this.tocContent = '<ul class="nav">' + this.tocContent + '</ul>';
100+
if (this.ulClass)
101+
this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>';
102+
else
103+
this.tocContent = '<ul>' + this.tocContent + '</ul>';
97104
};
98105

99106
Toc.prototype._showToc = function () {

0 commit comments

Comments
 (0)