summaryrefslogtreecommitdiff
path: root/website/showdown_toc.js
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2019-09-16 23:25:32 +0200
committerBert Belder <bertbelder@gmail.com>2019-09-16 23:30:56 +0200
commit02cb34d8ad320360c800b9af7b941990c48b9742 (patch)
treef918c80c2ac5036bcda5ec739ff404c35b6be33b /website/showdown_toc.js
parentc6afe87febc92ca64e81c1d99a62daacf75dba0a (diff)
Fix and enable linting of deno_typescript/*, tools/*, website/* (#2962)
Diffstat (limited to 'website/showdown_toc.js')
-rw-r--r--website/showdown_toc.js45
1 files changed, 23 insertions, 22 deletions
diff --git a/website/showdown_toc.js b/website/showdown_toc.js
index ba1f65c71..886036443 100644
--- a/website/showdown_toc.js
+++ b/website/showdown_toc.js
@@ -25,13 +25,14 @@
}
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];
+ // 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")));
}
@@ -40,14 +41,14 @@
function getHeaderEntriesInBrowser(sourceHtml) {
// Generate dummy element
- var source = document.createElement("div");
+ const 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];
+ 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));
}
@@ -65,8 +66,8 @@
if (this.children.length === 0) {
return "";
}
- var result = "<ul>\n";
- for (var i = 0; i < this.children.length; i++) {
+ let result = "<ul>\n";
+ for (let i = 0; i < this.children.length; i++) {
result += this.children[i].toString();
}
result += "</ul>\n";
@@ -74,7 +75,7 @@
};
TocEntry.prototype.toString = function() {
- var result = "<li>";
+ let result = "<li>";
if (this.text) {
result += '<a href="#' + this.anchor + '">' + this.text + "</a>";
}
@@ -85,9 +86,8 @@
function sortHeader(tocEntries, level) {
level = level || 1;
- var tagName = "H" + level,
- result = [],
- currentTocEntry;
+ const tagName = "H" + level;
+ const result = [];
function push(tocEntry) {
if (tocEntry !== undefined) {
@@ -98,8 +98,9 @@
}
}
- for (var i = 0; i < tocEntries.length; i++) {
- var tocEntry = tocEntries[i];
+ 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();
@@ -118,7 +119,7 @@
return {
type: "output",
filter: function(sourceHtml) {
- var headerList = getHeaderEntries(sourceHtml);
+ let headerList = getHeaderEntries(sourceHtml);
// No header found
if (headerList.length === 0) {
@@ -134,7 +135,7 @@
}
// Build result and replace all [toc]
- var result =
+ const result =
'<div class="toc">\n<ul>\n' + headerList.join("") + "</ul>\n</div>\n";
return sourceHtml.replace(/\[toc\]/gi, result);
}