Skip to content

Commit 3e421d3

Browse files
committed
Lint HBS JS templates
This updates eslint to lint on the toc.js.hbs file. This file uses a small amount of hbs templating which eslint can't handle. This adds a hacky preprocessor which will strips out the handlebars tags so that the file can be linted.
1 parent 3cd055c commit 3e421d3

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

crates/mdbook-html/front-end/templates/toc.js.hbs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,65 @@ class MDBookSidebarScrollbox extends HTMLElement {
1010
connectedCallback() {
1111
this.innerHTML = '{{#toc}}{{/toc}}';
1212
// Set the current, active page, and reveal it if it's hidden
13-
let current_page = document.location.href.toString().split("#")[0].split("?")[0];
14-
if (current_page.endsWith("/")) {
15-
current_page += "index.html";
13+
let current_page = document.location.href.toString().split('#')[0].split('?')[0];
14+
if (current_page.endsWith('/')) {
15+
current_page += 'index.html';
1616
}
17-
var links = Array.prototype.slice.call(this.querySelectorAll("a"));
18-
var l = links.length;
19-
for (var i = 0; i < l; ++i) {
20-
var link = links[i];
21-
var href = link.getAttribute("href");
22-
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) {
17+
const links = Array.prototype.slice.call(this.querySelectorAll('a'));
18+
const l = links.length;
19+
for (let i = 0; i < l; ++i) {
20+
const link = links[i];
21+
const href = link.getAttribute('href');
22+
if (href && !href.startsWith('#') && !/^(?:[a-z+]+:)?\/\//.test(href)) {
2323
link.href = path_to_root + href;
2424
}
25-
// The "index" page is supposed to alias the first chapter in the book.
26-
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) {
27-
link.classList.add("active");
28-
var parent = link.parentElement;
29-
if (parent && parent.classList.contains("chapter-item")) {
30-
parent.classList.add("expanded");
25+
// The 'index' page is supposed to alias the first chapter in the book.
26+
if (link.href === current_page
27+
|| i === 0
28+
&& path_to_root === ''
29+
&& current_page.endsWith('/index.html')) {
30+
link.classList.add('active');
31+
let parent = link.parentElement;
32+
if (parent && parent.classList.contains('chapter-item')) {
33+
parent.classList.add('expanded');
3134
}
3235
while (parent) {
33-
if (parent.tagName === "LI" && parent.previousElementSibling) {
34-
if (parent.previousElementSibling.classList.contains("chapter-item")) {
35-
parent.previousElementSibling.classList.add("expanded");
36+
if (parent.tagName === 'LI' && parent.previousElementSibling) {
37+
if (parent.previousElementSibling.classList.contains('chapter-item')) {
38+
parent.previousElementSibling.classList.add('expanded');
3639
}
3740
}
3841
parent = parent.parentElement;
3942
}
4043
}
4144
}
4245
// Track and set sidebar scroll position
43-
this.addEventListener('click', function(e) {
46+
this.addEventListener('click', e => {
4447
if (e.target.tagName === 'A') {
4548
sessionStorage.setItem('sidebar-scroll', this.scrollTop);
4649
}
4750
}, { passive: true });
48-
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
51+
const sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
4952
sessionStorage.removeItem('sidebar-scroll');
5053
if (sidebarScrollTop) {
5154
// preserve sidebar scroll position when navigating via links within sidebar
5255
this.scrollTop = sidebarScrollTop;
5356
} else {
54-
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
55-
var activeSection = document.querySelector('#mdbook-sidebar .active');
57+
// scroll sidebar to current active section when navigating via
58+
// 'next/previous chapter' buttons
59+
const activeSection = document.querySelector('#mdbook-sidebar .active');
5660
if (activeSection) {
5761
activeSection.scrollIntoView({ block: 'center' });
5862
}
5963
}
6064
// Toggle buttons
61-
var sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
65+
const sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
6266
function toggleSection(ev) {
6367
ev.currentTarget.parentElement.classList.toggle('expanded');
6468
}
65-
Array.from(sidebarAnchorToggles).forEach(function (el) {
69+
Array.from(sidebarAnchorToggles).forEach(el => {
6670
el.addEventListener('click', toggleSection);
6771
});
6872
}
6973
}
70-
window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox);
74+
window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox);

eslint.config.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
import { defineConfig, globalIgnores } from "eslint/config";
22

3+
// Custom preprocessor to strip Handlebars templates.
4+
const handlebarsPreprocessor = {
5+
processors: {
6+
"handlebars-js": {
7+
preprocess(text, filename) {
8+
if (filename.endsWith('.hbs')) {
9+
// This is a really dumb strip, which will likely not work
10+
// for more complex expressions, but for our use is good
11+
// enough for now.
12+
return [text.replace(/\{\{.*?\}\}/g, '')];
13+
}
14+
return [text];
15+
},
16+
postprocess(messages, filename) {
17+
// Ideally this would update the locations so that they would
18+
// compensate for the removed ranges.
19+
return [].concat(...messages);
20+
},
21+
},
22+
},
23+
};
24+
325
export default defineConfig([
426
globalIgnores(["**/**min.js", "**/highlight.js", "**/playground_editor/*"]),
527
{
@@ -69,4 +91,8 @@ export default defineConfig([
6991
eqeqeq: "error",
7092
},
7193
},
94+
{
95+
files: ["**/*.js.hbs"],
96+
processor: handlebarsPreprocessor.processors["handlebars-js"],
97+
},
7298
]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"eslint": "^9.34.0"
55
},
66
"scripts": {
7-
"lint": "eslint --no-warn-ignored crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js",
7+
"lint": "eslint --no-warn-ignored crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js crates/mdbook-html/front-end/**/*js.hbs",
88
"lint-fix": "eslint --fix crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js"
99
}
1010
}

0 commit comments

Comments
 (0)