summaryrefslogtreecommitdiff
path: root/website/showdown_toc.js
blob: ba1f65c719847cedc3ef702eef6cc75f91e5a16c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Extension loading compatible with AMD and CommonJs
(function(extension) {
  "use strict";

  if (typeof showdown === "object") {
    // global (browser or nodejs global)
    showdown.extension("toc", extension());
  } else if (typeof define === "function" && define.amd) {
    // AMD
    define("toc", extension());
  } else if (typeof exports === "object") {
    // Node, CommonJS-like
    module.exports = extension();
  } else {
    // showdown was not found so we throw
    throw Error("Could not find showdown library");
  }
})(function() {
  function getHeaderEntries(sourceHtml) {
    if (typeof window === "undefined") {
      return getHeaderEntriesInNodeJs(sourceHtml);
    } else {
      return getHeaderEntriesInBrowser(sourceHtml);
    }
  }

  function getHeaderEntriesInNodeJs(sourceHtml) {
    var cheerio = require("cheerio");
    var $ = cheerio.load(sourceHtml);
    var headers = $("h1, h2, h3, h4, h5, h6");

    var headerList = [];
    for (var i = 0; i < headers.length; i++) {
      var el = headers[i];
      headerList.push(new TocEntry(el.name, $(el).text(), $(el).attr("id")));
    }

    return headerList;
  }

  function getHeaderEntriesInBrowser(sourceHtml) {
    // Generate dummy element
    var source = document.createElement("div");
    source.innerHTML = sourceHtml;

    // Find headers
    var headers = source.querySelectorAll("h1, h2, h3, h4, h5, h6");
    var headerList = [];
    for (var i = 0; i < headers.length; i++) {
      var el = headers[i];
      headerList.push(new TocEntry(el.tagName, el.textContent, el.id));
    }

    return headerList;
  }

  function TocEntry(tagName, text, anchor) {
    this.tagName = tagName;
    this.text = text;
    this.anchor = anchor;
    this.children = [];
  }

  TocEntry.prototype.childrenToString = function() {
    if (this.children.length === 0) {
      return "";
    }
    var result = "<ul>\n";
    for (var i = 0; i < this.children.length; i++) {
      result += this.children[i].toString();
    }
    result += "</ul>\n";
    return result;
  };

  TocEntry.prototype.toString = function() {
    var result = "<li>";
    if (this.text) {
      result += '<a href="#' + this.anchor + '">' + this.text + "</a>";
    }
    result += this.childrenToString();
    result += "</li>\n";
    return result;
  };

  function sortHeader(tocEntries, level) {
    level = level || 1;
    var tagName = "H" + level,
      result = [],
      currentTocEntry;

    function push(tocEntry) {
      if (tocEntry !== undefined) {
        if (tocEntry.children.length > 0) {
          tocEntry.children = sortHeader(tocEntry.children, level + 1);
        }
        result.push(tocEntry);
      }
    }

    for (var i = 0; i < tocEntries.length; i++) {
      var tocEntry = tocEntries[i];
      if (tocEntry.tagName.toUpperCase() !== tagName) {
        if (currentTocEntry === undefined) {
          currentTocEntry = new TocEntry();
        }
        currentTocEntry.children.push(tocEntry);
      } else {
        push(currentTocEntry);
        currentTocEntry = tocEntry;
      }
    }

    push(currentTocEntry);
    return result;
  }

  return {
    type: "output",
    filter: function(sourceHtml) {
      var headerList = getHeaderEntries(sourceHtml);

      // No header found
      if (headerList.length === 0) {
        return sourceHtml;
      }

      // Sort header
      headerList = sortHeader(headerList);

      // Skip the title.
      if (headerList.length == 1) {
        headerList = headerList[0].children;
      }

      // Build result and replace all [toc]
      var result =
        '<div class="toc">\n<ul>\n' + headerList.join("") + "</ul>\n</div>\n";
      return sourceHtml.replace(/\[toc\]/gi, result);
    }
  };
});