Skip to content

Commit b15c59e

Browse files
committed
added an option to set the TOC depth directly inside the [toc] tag
Signed-off-by: Gereon Dusella <[email protected]>
1 parent 56e01fa commit b15c59e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

public/js/extra.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,14 +1015,19 @@ export function renderTOC (view) {
10151015
target.html('')
10161016
/* eslint-disable no-unused-vars */
10171017

1018+
const specificDepth = parseInt(toc.data('toc-depth'))
1019+
10181020
var tocOptions = md.meta.toc || {}
1019-
var maxLevel = (typeof tocOptions.maxLevel === 'number' && tocOptions.maxLevel > 0) ? tocOptions.maxLevel : window.defaultTocDepth
1021+
var yamlMaxDepth = (typeof tocOptions.maxLevel === 'number' && tocOptions.maxLevel > 0) ? tocOptions.maxLevel : window.defaultTocDepth
1022+
1023+
var maxLevel = specificDepth || yamlMaxDepth
10201024

10211025
const TOC = new window.Toc('doc', {
10221026
level: maxLevel,
10231027
top: -1,
10241028
class: 'toc',
10251029
targetId: id,
1030+
data: { tocDepth: specificDepth },
10261031
process: getHeaderContent
10271032
})
10281033
/* eslint-enable no-unused-vars */
@@ -1268,9 +1273,12 @@ const gistPlugin = new Plugin(
12681273
// TOC
12691274
const tocPlugin = new Plugin(
12701275
// regexp to match
1271-
/^\[TOC\]$/i,
1276+
/^\[TOC(|\s*maxLevel=\d+?)\]$/i,
12721277

1273-
(match, utils) => '<div class="toc"></div>'
1278+
(match, utils) => {
1279+
const tocDepth = match[1].split(/[?&=]+/)[1]
1280+
return `<div class="toc" data-toc-depth="${tocDepth}"></div>`
1281+
}
12741282
)
12751283
// slideshare
12761284
const slidesharePlugin = new Plugin(

public/vendor/md-toc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/**
33
* md-toc.js v1.0.2
44
* https://github.com/yijian166/md-toc.js
5+
*
6+
* Adapted to accept data attributes
57
*/
68

79
(function (window) {
@@ -15,6 +17,7 @@
1517
this.tocTop = parseInt(options.top) || 0
1618
this.elChilds = this.el.children
1719
this.process = options['process']
20+
this.data = options.data || {}
1821
if (!this.elChilds.length) return
1922
this._init()
2023
}
@@ -123,6 +126,9 @@
123126
this.toc = document.createElement('div')
124127
this.toc.innerHTML = this.tocContent
125128
this.toc.setAttribute('class', this.tocClass)
129+
if (this.data.tocDepth) {
130+
this.toc.dataset.tocDepth = this.data.tocDepth
131+
}
126132
if (!this.options.targetId) {
127133
this.el.appendChild(this.toc)
128134
} else {

0 commit comments

Comments
 (0)