Skip to content

Commit c08c622

Browse files
curtismitchellDynalon
authored andcommitted
Introduce more flexible pageMenu
see Dynalon#65
1 parent 046a639 commit c08c622

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

index.tmpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#md-page-menu a.active {
2929
/* background-color: rgba(0, 0, 0, 0.01); */
3030
font-weight: bold;
31-
padding-left: 6px;
32-
3331
}
3432
@media (min-width: 992px) {
3533
#md-page-menu.affix {

js/basic_skeleton.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,25 @@
193193

194194
// adds a link to the navigation at the top of the page
195195
function addJumpLinkToTOC($heading) {
196-
if($.md.config.useSideMenu === false) return;
197-
if($heading.prop("tagName") !== 'H2') return;
196+
if($.md.config.pageMenu && $.md.config.pageMenu.disable !== false) return;
197+
198+
function supportedHeading(heading) {
199+
var autoAnchors = $.md.config.pageMenu.useHeadings.split(',');
200+
var supported = false;
201+
202+
$(autoAnchors).each(function(i,e){
203+
if(heading.toLowerCase() === e.toLowerCase()) {
204+
supported = true;
205+
}
206+
});
207+
208+
return supported;
209+
}
210+
211+
if(!supportedHeading($heading.prop("tagName"))) return;
212+
213+
var c = $.md.config.pageMenu.returnAnchor;
198214

199-
var c = $.md.config.tocAnchor;
200215
if (c === '')
201216
return;
202217

js/bootstrap.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,9 @@
240240
}
241241

242242
function createPageContentMenu () {
243-
244243
// assemble the menu
245-
var $headings = $('#md-content').find('h2').clone();
246-
// we dont want the text of any child nodes
244+
var $headings = $('#md-content').find($.md.config.pageMenu.useHeadings);
245+
247246
$headings.children().remove();
248247

249248
if ($headings.length <= 1) {
@@ -300,10 +299,10 @@
300299
var $ul = $pannel.find("ul");
301300
affixDiv.append($pannel);
302301

303-
$headings.each(function(i,e) {
304-
var $heading = $(e);
305-
var $li = $('<li class="list-group-item" />');
306-
var $a = $('<a />');
302+
function createMenuItem(heading, className) {
303+
var $heading = $(heading);
304+
var $a = $('<a class="list-group-item" />');
305+
$a.addClass(className);
307306
$a.attr('href', $.md.util.getInpageAnchorHref($heading.toptext()));
308307
$a.click(function(ev) {
309308
ev.preventDefault();
@@ -313,8 +312,15 @@
313312
$.md.scrollToInPageAnchor(anchortext);
314313
});
315314
$a.text($heading.toptext());
316-
$li.append($a);
317-
$ul.append($li);
315+
return $a;
316+
}
317+
318+
$($headings).each(function(i,e) {
319+
var hClass = $(e).prop('tagName');
320+
var currLevel = parseInt(hClass.substr(1,1), 10);
321+
var $hli = createMenuItem(e, hClass.toLowerCase() + '-nav');
322+
323+
$ul.append($hli);
318324
});
319325

320326
$(window).resize(function () {

js/init.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
// default config
1919
$.md.config = {
2020
title: null,
21-
useSideMenu: true,
2221
lineBreaks: 'gfm',
2322
additionalFooterText: '',
2423
anchorCharacter: '&para;',
25-
tocAnchor: '[ &uarr; ]'
24+
pageMenu: {
25+
disable: false,
26+
returnAnchor: "[top]",
27+
useHeadings: "h2"
28+
}
2629
};
2730

2831

0 commit comments

Comments
 (0)