summaryrefslogtreecommitdiff
path: root/website/showdown_toc.js
diff options
context:
space:
mode:
authorRy Dahl <ry@tinyclouds.org>2019-10-24 16:14:05 -0400
committerGitHub <noreply@github.com>2019-10-24 16:14:05 -0400
commit1d8f3cc896ac417ceed4a28e4a001a758ab49185 (patch)
treef5e7e9317ced50a5183445a0159e3be763c5556f /website/showdown_toc.js
parentf96aaa802b245c8b3aeb5d57b031f8a55bb07de2 (diff)
Remove old website (#3194)
Move manual.md and style_guide.md into //std so they can be accessed from https://deno.land/std/manual.md Code for new website is https://github.com/denoland/deno_website2 Co-authored-by: Christian Moritz <chrmoritz@gmail.com>
Diffstat (limited to 'website/showdown_toc.js')
-rw-r--r--website/showdown_toc.js143
1 files changed, 0 insertions, 143 deletions
diff --git a/website/showdown_toc.js b/website/showdown_toc.js
deleted file mode 100644
index 886036443..000000000
--- a/website/showdown_toc.js
+++ /dev/null
@@ -1,143 +0,0 @@
-// 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) {
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const cheerio = require("cheerio");
- const $ = cheerio.load(sourceHtml);
- const headers = $("h1, h2, h3, h4, h5, h6");
-
- const headerList = [];
- for (let i = 0; i < headers.length; i++) {
- const el = headers[i];
- headerList.push(new TocEntry(el.name, $(el).text(), $(el).attr("id")));
- }
-
- return headerList;
- }
-
- function getHeaderEntriesInBrowser(sourceHtml) {
- // Generate dummy element
- const source = document.createElement("div");
- source.innerHTML = sourceHtml;
-
- // Find headers
- const headers = source.querySelectorAll("h1, h2, h3, h4, h5, h6");
- const headerList = [];
- for (let i = 0; i < headers.length; i++) {
- const 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 "";
- }
- let result = "<ul>\n";
- for (let i = 0; i < this.children.length; i++) {
- result += this.children[i].toString();
- }
- result += "</ul>\n";
- return result;
- };
-
- TocEntry.prototype.toString = function() {
- let 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;
- const tagName = "H" + level;
- const result = [];
-
- function push(tocEntry) {
- if (tocEntry !== undefined) {
- if (tocEntry.children.length > 0) {
- tocEntry.children = sortHeader(tocEntry.children, level + 1);
- }
- result.push(tocEntry);
- }
- }
-
- let currentTocEntry;
- for (let i = 0; i < tocEntries.length; i++) {
- const 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) {
- let 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]
- const result =
- '<div class="toc">\n<ul>\n' + headerList.join("") + "</ul>\n</div>\n";
- return sourceHtml.replace(/\[toc\]/gi, result);
- }
- };
-});