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.
This commit is contained in:
Eric Huss
2025-08-25 15:15:47 -07:00
parent 3cd055c508
commit 3e421d353d
3 changed files with 56 additions and 26 deletions

View File

@@ -10,29 +10,32 @@ class MDBookSidebarScrollbox extends HTMLElement {
connectedCallback() { connectedCallback() {
this.innerHTML = '{{#toc}}{{/toc}}'; this.innerHTML = '{{#toc}}{{/toc}}';
// Set the current, active page, and reveal it if it's hidden // Set the current, active page, and reveal it if it's hidden
let current_page = document.location.href.toString().split("#")[0].split("?")[0]; let current_page = document.location.href.toString().split('#')[0].split('?')[0];
if (current_page.endsWith("/")) { if (current_page.endsWith('/')) {
current_page += "index.html"; current_page += 'index.html';
} }
var links = Array.prototype.slice.call(this.querySelectorAll("a")); const links = Array.prototype.slice.call(this.querySelectorAll('a'));
var l = links.length; const l = links.length;
for (var i = 0; i < l; ++i) { for (let i = 0; i < l; ++i) {
var link = links[i]; const link = links[i];
var href = link.getAttribute("href"); const href = link.getAttribute('href');
if (href && !href.startsWith("#") && !/^(?:[a-z+]+:)?\/\//.test(href)) { if (href && !href.startsWith('#') && !/^(?:[a-z+]+:)?\/\//.test(href)) {
link.href = path_to_root + href; link.href = path_to_root + href;
} }
// The "index" page is supposed to alias the first chapter in the book. // The 'index' page is supposed to alias the first chapter in the book.
if (link.href === current_page || (i === 0 && path_to_root === "" && current_page.endsWith("/index.html"))) { if (link.href === current_page
link.classList.add("active"); || i === 0
var parent = link.parentElement; && path_to_root === ''
if (parent && parent.classList.contains("chapter-item")) { && current_page.endsWith('/index.html')) {
parent.classList.add("expanded"); link.classList.add('active');
let parent = link.parentElement;
if (parent && parent.classList.contains('chapter-item')) {
parent.classList.add('expanded');
} }
while (parent) { while (parent) {
if (parent.tagName === "LI" && parent.previousElementSibling) { if (parent.tagName === 'LI' && parent.previousElementSibling) {
if (parent.previousElementSibling.classList.contains("chapter-item")) { if (parent.previousElementSibling.classList.contains('chapter-item')) {
parent.previousElementSibling.classList.add("expanded"); parent.previousElementSibling.classList.add('expanded');
} }
} }
parent = parent.parentElement; parent = parent.parentElement;
@@ -40,31 +43,32 @@ class MDBookSidebarScrollbox extends HTMLElement {
} }
} }
// Track and set sidebar scroll position // Track and set sidebar scroll position
this.addEventListener('click', function(e) { this.addEventListener('click', e => {
if (e.target.tagName === 'A') { if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', this.scrollTop); sessionStorage.setItem('sidebar-scroll', this.scrollTop);
} }
}, { passive: true }); }, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll'); const sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll'); sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) { if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar // preserve sidebar scroll position when navigating via links within sidebar
this.scrollTop = sidebarScrollTop; this.scrollTop = sidebarScrollTop;
} else { } else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons // scroll sidebar to current active section when navigating via
var activeSection = document.querySelector('#mdbook-sidebar .active'); // 'next/previous chapter' buttons
const activeSection = document.querySelector('#mdbook-sidebar .active');
if (activeSection) { if (activeSection) {
activeSection.scrollIntoView({ block: 'center' }); activeSection.scrollIntoView({ block: 'center' });
} }
} }
// Toggle buttons // Toggle buttons
var sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle'); const sidebarAnchorToggles = document.querySelectorAll('#mdbook-sidebar a.toggle');
function toggleSection(ev) { function toggleSection(ev) {
ev.currentTarget.parentElement.classList.toggle('expanded'); ev.currentTarget.parentElement.classList.toggle('expanded');
} }
Array.from(sidebarAnchorToggles).forEach(function (el) { Array.from(sidebarAnchorToggles).forEach(el => {
el.addEventListener('click', toggleSection); el.addEventListener('click', toggleSection);
}); });
} }
} }
window.customElements.define("mdbook-sidebar-scrollbox", MDBookSidebarScrollbox); window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox);

View File

@@ -1,5 +1,27 @@
import { defineConfig, globalIgnores } from "eslint/config"; import { defineConfig, globalIgnores } from "eslint/config";
// Custom preprocessor to strip Handlebars templates.
const handlebarsPreprocessor = {
processors: {
"handlebars-js": {
preprocess(text, filename) {
if (filename.endsWith('.hbs')) {
// This is a really dumb strip, which will likely not work
// for more complex expressions, but for our use is good
// enough for now.
return [text.replace(/\{\{.*?\}\}/g, '')];
}
return [text];
},
postprocess(messages, filename) {
// Ideally this would update the locations so that they would
// compensate for the removed ranges.
return [].concat(...messages);
},
},
},
};
export default defineConfig([ export default defineConfig([
globalIgnores(["**/**min.js", "**/highlight.js", "**/playground_editor/*"]), globalIgnores(["**/**min.js", "**/highlight.js", "**/playground_editor/*"]),
{ {
@@ -69,4 +91,8 @@ export default defineConfig([
eqeqeq: "error", eqeqeq: "error",
}, },
}, },
{
files: ["**/*.js.hbs"],
processor: handlebarsPreprocessor.processors["handlebars-js"],
},
]); ]);

View File

@@ -4,7 +4,7 @@
"eslint": "^9.34.0" "eslint": "^9.34.0"
}, },
"scripts": { "scripts": {
"lint": "eslint --no-warn-ignored crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js", "lint": "eslint --no-warn-ignored crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js crates/mdbook-html/front-end/**/*js.hbs",
"lint-fix": "eslint --fix crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js" "lint-fix": "eslint --fix crates/mdbook-html/front-end/*js crates/mdbook-html/front-end/**/*js"
} }
} }