summaryrefslogtreecommitdiff
path: root/public/vendor/md-toc.js
diff options
context:
space:
mode:
authorBoHong Li2017-03-13 02:09:52 +0800
committerBoHong Li2017-03-13 02:09:52 +0800
commitbefa89be8b69ed1b951099828c5dbc7e550d4c68 (patch)
tree835b840c390029c596756a59abc919bf17d14b54 /public/vendor/md-toc.js
parent747da6772d339329ed130f5a0dbc896be5391ef8 (diff)
parent5bc642d02e8955b200bb21cf30e863fdf0c53765 (diff)
Merge branch 'use_javascript_standard_style'
Introduce JavaScript Standard Style Signed-off-by: BoHong Li <a60814billy@gmail.com>
Diffstat (limited to 'public/vendor/md-toc.js')
-rwxr-xr-xpublic/vendor/md-toc.js214
1 files changed, 104 insertions, 110 deletions
diff --git a/public/vendor/md-toc.js b/public/vendor/md-toc.js
index 200275a5..f93f7921 100755
--- a/public/vendor/md-toc.js
+++ b/public/vendor/md-toc.js
@@ -1,129 +1,123 @@
+/* eslint-env browser, jquery */
/**
* md-toc.js v1.0.2
* https://github.com/yijian166/md-toc.js
*/
(function (window) {
- 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'];
- if (!this.elChilds.length) return;
- this._init();
- }
+ 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']
+ if (!this.elChilds.length) return
+ this._init()
+ }
- Toc.prototype._init = function () {
- this._collectTitleElements();
- this._createTocContent();
- this._showToc();
- };
+ Toc.prototype._init = function () {
+ this._collectTitleElements()
+ this._createTocContent()
+ this._showToc()
+ }
- Toc.prototype._collectTitleElements = function () {
- this._elTitlesNames = [],
- this.elTitleElements = [];
- for (var i = 1; i < 7; i++) {
- if (this.el.getElementsByTagName('h' + i).length) {
- this._elTitlesNames.push('h' + i);
- }
- }
+ Toc.prototype._collectTitleElements = function () {
+ this._elTitlesNames = []
+ this.elTitleElements = []
+ for (var i = 1; i < 7; i++) {
+ if (this.el.getElementsByTagName('h' + i).length) {
+ this._elTitlesNames.push('h' + i)
+ }
+ }
- this._elTitlesNames.length = this._elTitlesNames.length > this.tocLevel ? this.tocLevel : this._elTitlesNames.length;
+ this._elTitlesNames.length = this._elTitlesNames.length > this.tocLevel ? this.tocLevel : this._elTitlesNames.length
- 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]);
- }
- }
- };
+ 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])
+ }
+ }
+ }
- Toc.prototype._createTocContent = function () {
- this._elTitleElementsLen = this.elTitleElements.length;
- if (!this._elTitleElementsLen) return;
- this.tocContent = '';
- this._tempLists = [];
+ Toc.prototype._createTocContent = function () {
+ this._elTitleElementsLen = this.elTitleElements.length
+ if (!this._elTitleElementsLen) return
+ this.tocContent = ''
+ this._tempLists = []
- var url = location.origin + location.pathname;
- for (var i = 0; i < this._elTitleElementsLen; i++) {
- var j = i + 1;
- this._elTitleElement = this.elTitleElements[i];
- this._elTitleElementName = this._elTitleElement.tagName;
- this._elTitleElementText = (typeof this.process === 'function' ? this.process(this._elTitleElement) : this._elTitleElement.innerHTML).replace(/<(?:.|\n)*?>/gm, '');
- var id = this._elTitleElement.getAttribute('id');
- if (!id) {
- this._elTitleElement.setAttribute('id', 'tip' + i);
- id = '#tip' + i;
- } else {
- id = '#' + id;
- }
+ for (var i = 0; i < this._elTitleElementsLen; i++) {
+ var j = i + 1
+ this._elTitleElement = this.elTitleElements[i]
+ this._elTitleElementName = this._elTitleElement.tagName
+ this._elTitleElementText = (typeof this.process === 'function' ? this.process(this._elTitleElement) : this._elTitleElement.innerHTML).replace(/<(?:.|\n)*?>/gm, '')
+ var id = this._elTitleElement.getAttribute('id')
+ if (!id) {
+ this._elTitleElement.setAttribute('id', 'tip' + i)
+ id = '#tip' + i
+ } else {
+ id = '#' + id
+ }
- this.tocContent += '<li><a href="' + id + '">' + this._elTitleElementText + '</a>';
+ this.tocContent += '<li><a href="' + id + '">' + this._elTitleElementText + '</a>'
- if (j != this._elTitleElementsLen) {
- this._elNextTitleElementName = this.elTitleElements[j].tagName;
- if (this._elTitleElementName != this._elNextTitleElementName) {
- var checkColse = false,
- y = 1;
- for (var t = this._tempLists.length - 1; t >= 0; t--) {
- if (this._tempLists[t].tagName == this._elNextTitleElementName) {
- checkColse = true;
- break;
- }
- y++;
- }
- if (checkColse) {
- this.tocContent += new Array(y + 1).join('</li></ul>');
- this._tempLists.length = this._tempLists.length - y;
- } else {
- this._tempLists.push(this._elTitleElement);
- if (this.ulClass)
- this.tocContent += '<ul class="' + this.ulClass + '">';
- else
- this.tocContent += '<ul>';
- }
- } else {
- this.tocContent += '</li>';
- }
- } else {
- if (this._tempLists.length) {
- this.tocContent += new Array(this._tempLists.length + 1).join('</li></ul>');
- } else {
- this.tocContent += '</li>';
- }
+ if (j !== this._elTitleElementsLen) {
+ this._elNextTitleElementName = this.elTitleElements[j].tagName
+ if (this._elTitleElementName !== this._elNextTitleElementName) {
+ var checkColse = false
+ var y = 1
+ for (var t = this._tempLists.length - 1; t >= 0; t--) {
+ if (this._tempLists[t].tagName === this._elNextTitleElementName) {
+ checkColse = true
+ break
}
+ y++
+ }
+ if (checkColse) {
+ this.tocContent += new Array(y + 1).join('</li></ul>')
+ this._tempLists.length = this._tempLists.length - y
+ } else {
+ this._tempLists.push(this._elTitleElement)
+ if (this.ulClass) { this.tocContent += '<ul class="' + this.ulClass + '">' } else { this.tocContent += '<ul>' }
+ }
+ } else {
+ this.tocContent += '</li>'
}
- if (this.ulClass)
- this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>';
- else
- this.tocContent = '<ul>' + this.tocContent + '</ul>';
- };
-
- Toc.prototype._showToc = function () {
- this.toc = document.createElement('div');
- this.toc.innerHTML = this.tocContent;
- this.toc.setAttribute('class', this.tocClass);
- if (!this.options.targetId) {
- this.el.appendChild(this.toc);
+ } else {
+ if (this._tempLists.length) {
+ this.tocContent += new Array(this._tempLists.length + 1).join('</li></ul>')
} else {
- document.getElementById(this.options.targetId).appendChild(this.toc);
+ this.tocContent += '</li>'
}
- 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;');
- }
- }
+ }
+ }
+ if (this.ulClass) { this.tocContent = '<ul class="' + this.ulClass + '">' + this.tocContent + '</ul>' } else { this.tocContent = '<ul>' + this.tocContent + '</ul>' }
+ }
+
+ Toc.prototype._showToc = function () {
+ this.toc = document.createElement('div')
+ this.toc.innerHTML = this.tocContent
+ this.toc.setAttribute('class', this.tocClass)
+ 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;')
}
- };
- window.Toc = Toc;
-})(window); \ No newline at end of file
+ }
+ }
+ }
+ window.Toc = Toc
+})(window)