// 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 = "