summaryrefslogtreecommitdiff
path: root/public/vendor/md-toc.js
blob: 3457d465e9f7936f38ab1a1871241f90dc409dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 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()
  }

  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)
      }
    }

    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])
      }
    }
  }

  Toc.prototype._createTocContent = function () {
    this._elTitleElementsLen = this.elTitleElements.length
    if (!this._elTitleElementsLen) return
    this.tocContent = ''
    this._tempLists = []

    for (var i = 0; i < this._elTitleElementsLen; i++) {
      var j = i + 1
      this._elTitleElement = this.elTitleElements[i]
      this._elTitleElementName = this._elTitleElement.tagName
      this._elTitleElementTitle = this._elTitleElement.textContent.replace(/"/g, '&quot;')
      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 + '" title="'+ this._elTitleElementTitle +'">' + this._elTitleElementText + '</a>'

      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>'
        }
      } else {
        if (this._tempLists.length) {
          this.tocContent += new Array(this._tempLists.length + 1).join('</li></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 {
      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)