diff --git a/customize.dist/src/less2/include/markdown.less b/customize.dist/src/less2/include/markdown.less index e98c0920c..a1735ae34 100644 --- a/customize.dist/src/less2/include/markdown.less +++ b/customize.dist/src/less2/include/markdown.less @@ -9,6 +9,35 @@ blockquote { margin: 0; } } // todo ul, ol + // TOC + div.cp-md-toc { + background: #f3f3f3; + padding: 20px; + float: right; + margin: 5px; + margin-right: 0; + & > p { + &.cp-md-toc-1 { + margin-left: 0; + } + &.cp-md-toc-2 { + margin-left: 25px; + } + &.cp-md-toc-3 { + margin-left: 50px; + } + &.cp-md-toc-4 { + margin-left: 75px; + } + &.cp-md-toc-5 { + margin-left: 100px; + } + &.cp-md-toc-6 { + margin-left: 125px; + } + margin: 0; + } + } } .markdown_preformatted-code (@color: #333) { diff --git a/www/common/diffMarked.js b/www/common/diffMarked.js index 93a079e4d..994e35e62 100644 --- a/www/common/diffMarked.js +++ b/www/common/diffMarked.js @@ -3,12 +3,14 @@ define([ '/bower_components/marked/marked.min.js', '/common/common-hash.js', '/common/common-util.js', + '/common/hyperscript.js', '/common/media-tag.js', '/common/highlight/highlight.pack.js', + '/customize/messages.js', '/bower_components/diff-dom/diffDOM.js', '/bower_components/tweetnacl/nacl-fast.min.js', 'css!/common/highlight/styles/github.css' -],function ($, Marked, Hash, Util, MediaTag, Highlight) { +],function ($, Marked, Hash, Util, h, MediaTag, Highlight, Messages) { var DiffMd = {}; var DiffDOM = window.diffDOM; @@ -33,16 +35,56 @@ define([ highlight: highlighter(), }); - + var toc = []; + var getTOC = function () { + var content = [h('h2', Messages.markdown_toc)]; + toc.forEach(function (obj) { + // Only include level 2 headings + var level = obj.level - 1; + if (level < 1) { return; } + var a = h('a.cp-md-toc-link', { + href: '#', + 'data-href': obj.id, + }, obj.title); + content.push(h('p.cp-md-toc-'+level, a)); + }); + return h('div.cp-md-toc', content).outerHTML; + }; DiffMd.render = function (md, sanitize) { - return Marked(md, { + var r = Marked(md, { sanitize: sanitize }); + + // Add Table of Content + r = r.replace(/
)?\[[xX]\](<\/p>)?\s*/; var uncheckedTaskItemPtn = /^\s*(
)?\[ ?\](<\/p>)?\s*/; @@ -97,6 +139,9 @@ define([ }; renderer.paragraph = function (p) { + if (p === '[TOC]') { + return '
'; + } return /' + p + '
\n'; }; @@ -249,6 +294,15 @@ define([ characterData: false }); }); + // Fix Table of contents links + $content.find('a.cp-md-toc-link').off('click').click(function (e) { + e.preventDefault(); + e.stopPropagation(); + var $a = $(this); + if (!$a.attr('data-href')) { return; } + var target = document.getElementById($a.attr('data-href')); + if (target) { target.scrollIntoView(); } + }); } };