2017-03-08 18:41:05 +00:00
|
|
|
/* eslint-env browser, jquery */
|
2015-07-01 16:10:20 +00:00
|
|
|
/**
|
|
|
|
* md-toc.js v1.0.2
|
|
|
|
* https://github.com/yijian166/md-toc.js
|
2020-06-03 16:32:48 +00:00
|
|
|
*
|
|
|
|
* Adapted to accept data attributes
|
2015-07-01 16:10:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
(function (window) {
|
2017-03-08 18:41:05 +00:00
|
|
|
function Toc (id, options) {
|
|
|
|
this.el = document.getElementById(id)
|
|
|
|
if (!this.el) return
|
|
|
|
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
|
|
|
|
this.process = options['process']
|
2020-06-03 16:32:48 +00:00
|
|
|
this.data = options.data || {}
|
2017-03-08 18:41:05 +00:00
|
|
|
if (!this.elChilds.length) return
|
|
|
|
this._init()
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
Toc.prototype._init = function () {
|
|
|
|
this._collectTitleElements()
|
|
|
|
this._createTocContent()
|
|
|
|
this._showToc()
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
Toc.prototype._collectTitleElements = function () {
|
|
|
|
this._elTitlesNames = []
|
|
|
|
this.elTitleElements = []
|
2018-07-04 09:20:59 +00:00
|
|
|
for (var i = 1; i < 6; i++) {
|
2017-03-08 18:41:05 +00:00
|
|
|
if (this.el.getElementsByTagName('h' + i).length) {
|
|
|
|
this._elTitlesNames.push('h' + i)
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
this._elTitlesNames.length = this._elTitlesNames.length > this.tocLevel ? this.tocLevel : this._elTitlesNames.length
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2017-03-08 18:41:05 +00:00
|
|
|
for (var j = 0; j < this.elChilds.length; j++) {
|
|
|
|
this._elChildName = this.elChilds[j].tagName.toLowerCase()
|
|
|
|
if (this._elTitlesNames.toString().match(this._elChildName)) {
|
|
|
|
this.elTitleElements.push(this.elChilds[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2018-11-07 16:03:45 +00:00
|
|
|
Toc.prototype._createTocContent = function recursiveToc(level = 0, titleElements = [], titleNames = [], ulClass = undefined, index = 0) {
|
2018-07-04 00:22:48 +00:00
|
|
|
// Inititalize our elements from the toc object
|
|
|
|
// which is only available on level 0
|
|
|
|
if (level === 0) {
|
|
|
|
titleElements = this.elTitleElements
|
|
|
|
titleNames = this._elTitlesNames
|
|
|
|
ulClass = this.ulClass
|
|
|
|
}
|
|
|
|
// No need to do anything for an empty ToC
|
2018-07-04 19:08:57 +00:00
|
|
|
if (!titleElements.length) return
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2018-07-04 00:22:48 +00:00
|
|
|
var content = '<ul'
|
|
|
|
if (ulClass) {
|
|
|
|
content += ' class="' + ulClass + '"'
|
|
|
|
}
|
2018-07-04 19:08:57 +00:00
|
|
|
content += '>\n'
|
2018-07-04 00:22:48 +00:00
|
|
|
var iterTag = titleNames[level]
|
|
|
|
var recurse = false
|
2018-07-04 19:08:57 +00:00
|
|
|
var openTag = false
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2018-07-04 00:22:48 +00:00
|
|
|
for (var element; element = titleElements.shift();) {
|
|
|
|
var elementTag = element.tagName.toLowerCase()
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2018-07-04 00:22:48 +00:00
|
|
|
// We only care about tags on our level to add them as list item
|
|
|
|
if (elementTag == iterTag) {
|
|
|
|
// Let's do some cleaning
|
|
|
|
var elementTitle = element.textContent.replace(/"/g, '"')
|
|
|
|
var elementText = (typeof this.process === 'function' ? this.process(element) : element.innerHTML).replace(/<(?:.|\n)*?>/gm, '')
|
|
|
|
var id = element.getAttribute('id')
|
|
|
|
if (!id) {
|
2018-11-07 16:03:45 +00:00
|
|
|
element.setAttribute('id', 'tip' + ++index)
|
|
|
|
id = '#tip' + index
|
2017-03-08 18:41:05 +00:00
|
|
|
} else {
|
2018-07-04 00:22:48 +00:00
|
|
|
id = '#' + id
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2018-07-04 19:08:57 +00:00
|
|
|
if (openTag) {
|
|
|
|
content += '</li>\n'
|
|
|
|
openTag = false
|
|
|
|
}
|
2018-07-04 00:22:48 +00:00
|
|
|
content += '<li><a href="' + id + '" title="'+ elementTitle +'">' + elementText + '</a>'
|
|
|
|
// Reset recursion. We need it for the next subsections
|
|
|
|
recurse = false
|
2018-07-04 19:08:57 +00:00
|
|
|
openTag = true
|
2018-07-04 00:22:48 +00:00
|
|
|
// Check if the current element has a lower level than ours, if so, we have to go down the rabbithole!
|
|
|
|
} else if (!recurse && titleNames.indexOf(elementTag.toLowerCase()) > level) {
|
|
|
|
recurse = true
|
2018-07-04 19:08:57 +00:00
|
|
|
if (!openTag) {
|
|
|
|
content += '<li class="invisable-node">'
|
|
|
|
openTag = true
|
|
|
|
}
|
2018-07-04 00:22:48 +00:00
|
|
|
// This element is for the lower lever, we have to re-add it before we send the list down there.
|
|
|
|
titleElements.unshift(element)
|
|
|
|
// Let's call ourself and get to the next level
|
2018-11-07 16:03:45 +00:00
|
|
|
content += recursiveToc(level + 1, titleElements, titleNames, ulClass, index)
|
2017-03-08 18:41:05 +00:00
|
|
|
} else {
|
2018-07-04 00:22:48 +00:00
|
|
|
// When we end up here, met a higher level element
|
|
|
|
// This is not our business so back into the list with the element and let's end this loop
|
|
|
|
titleElements.unshift(element)
|
|
|
|
break
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-04 00:22:48 +00:00
|
|
|
|
2018-07-04 19:08:57 +00:00
|
|
|
if (openTag) {
|
|
|
|
content += '</li>\n'
|
|
|
|
}
|
|
|
|
content += '</ul>\n'
|
2018-07-04 00:22:48 +00:00
|
|
|
|
|
|
|
// Set ToC content of the level 0 everything else pass things to the upper level!
|
|
|
|
if (level === 0) {
|
|
|
|
this.tocContent = content
|
|
|
|
} else {
|
|
|
|
return content
|
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Toc.prototype._showToc = function () {
|
|
|
|
this.toc = document.createElement('div')
|
|
|
|
this.toc.innerHTML = this.tocContent
|
|
|
|
this.toc.setAttribute('class', this.tocClass)
|
2020-06-03 16:32:48 +00:00
|
|
|
if (this.data.tocDepth) {
|
|
|
|
this.toc.dataset.tocDepth = this.data.tocDepth
|
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
if (!this.options.targetId) {
|
|
|
|
this.el.appendChild(this.toc)
|
|
|
|
} else {
|
|
|
|
document.getElementById(this.options.targetId).appendChild(this.toc)
|
|
|
|
}
|
|
|
|
var self = this
|
|
|
|
if (this.tocTop > -1) {
|
|
|
|
window.onscroll = function () {
|
|
|
|
var t = document.documentElement.scrollTop || document.body.scrollTop
|
|
|
|
if (t < self.tocTop) {
|
|
|
|
self.toc.setAttribute('style', 'position:absolute;top:' + self.tocTop + 'px;')
|
|
|
|
} else {
|
|
|
|
self.toc.setAttribute('style', 'position:fixed;top:10px;')
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2017-03-08 18:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.Toc = Toc
|
|
|
|
})(window)
|