From 9eb23603f4d30a0822afde7fbd234976557f575a Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Sun, 20 Dec 2015 11:28:54 -0600 Subject: Added support of toc syntax --- public/js/extra.js | 34 +++++++++++++++++++++++++++++++++- public/js/index.js | 5 +++++ public/js/pretty.js | 1 + public/vendor/md-toc.js | 11 +++++++++-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/public/js/extra.js b/public/js/extra.js index ef1f7e35..12342f09 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -341,6 +341,7 @@ function generateToc(id) { 'level': 3, 'top': -1, 'class': 'toc', + 'ulClass': 'nav', 'targetId': id }); 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() { var hash = location.hash; location.hash = ""; @@ -623,7 +644,18 @@ var mathjaxPlugin = new Plugin( return '' + match[0] + ''; } ); +//TOC +var tocPlugin = new Plugin( + // regexp to match + /^\[TOC\]$/, + + // this function will be called when something matches + function (match, utils) { + return '
'; + } +); md.use(youtubePlugin); md.use(vimeoPlugin); md.use(gistPlugin); -md.use(mathjaxPlugin); \ No newline at end of file +md.use(mathjaxPlugin); +md.use(tocPlugin); \ No newline at end of file diff --git a/public/js/index.js b/public/js/index.js index 046c5407..03f5e587 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -86,6 +86,10 @@ var supportReferrals = [ { text: '![image text](url "title")', search: '![]()' + }, + { + text: '[TOC]', + search: '[]' } ]; var supportExternals = [ @@ -1876,6 +1880,7 @@ function updateViewInner() { finishView(ui.area.view); autoLinkify(ui.area.view); deduplicatedHeaderId(ui.area.view); + renderTOC(ui.area.view); generateToc('toc'); generateToc('toc-affix'); generateScrollspy(); diff --git a/public/js/pretty.js b/public/js/pretty.js index ad5655d6..ff393cac 100644 --- a/public/js/pretty.js +++ b/public/js/pretty.js @@ -6,6 +6,7 @@ $(document.body).show(); finishView(markdown); autoLinkify(markdown); deduplicatedHeaderId(markdown); +renderTOC(markdown); generateToc('toc'); generateToc('toc-affix'); smoothHashScroll(); diff --git a/public/vendor/md-toc.js b/public/vendor/md-toc.js index e46afad9..abdd9f56 100755 --- a/public/vendor/md-toc.js +++ b/public/vendor/md-toc.js @@ -10,6 +10,7 @@ this.options = options || {}; this.tocLevel = parseInt(options.level) || 0; this.tocClass = options['class'] || 'toc'; + this.ulClass = options['ulClass']; this.tocTop = parseInt(options.top) || 0; this.elChilds = this.el.children; if (!this.elChilds.length) return; @@ -80,7 +81,10 @@ this._tempLists.length = this._tempLists.length - y; } else { this._tempLists.push(this._elTitleElement); - this.tocContent += '