From 1334c96aaaf1cef077ac473de6adabad47a746e4 Mon Sep 17 00:00:00 2001 From: D Date: Wed, 17 Nov 2021 16:27:38 +0100 Subject: [PATCH] Update processing and add try-catch per page --- src/scripts/index.ts | 38 ++++++++++++++++++++-------------- src/scripts/pages/chemistry.ts | 4 ++-- src/scripts/pages/food.ts | 2 ++ src/scripts/pages/generic.ts | 2 +- src/scripts/pages/global.ts | 10 +++++++-- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/scripts/index.ts b/src/scripts/index.ts index a95e405..ea22f46 100644 --- a/src/scripts/index.ts +++ b/src/scripts/index.ts @@ -18,22 +18,30 @@ export const PAGE_VERSIONS = { const MAX_WIDTH = 440; export function processHTML(root: HTMLElement, docname: string): void { - processGlobal(root, docname); + try { + processGlobal(root, docname); + } catch (e) { + console.error(`Error processing page: ${docname}`); + } - switch (docname) { - case "Guide_to_chemistry": - processChemistry(root); - break; - case "Infections": - processVirology(root); - break; - case "Guide_to_food": - processFood(root); - break; - case "Guide_to_drinks": - processDrinks(root); - break; - default: + try { + switch (docname) { + case "Guide_to_chemistry": + processChemistry(root); + break; + case "Infections": + processVirology(root); + break; + case "Guide_to_food": + processFood(root); + break; + case "Guide_to_drinks": + processDrinks(root); + break; + default: + } + } catch (e) { + console.error(`Error processing page: ${docname} (specific enhancements)`); } } diff --git a/src/scripts/pages/chemistry.ts b/src/scripts/pages/chemistry.ts index a679649..4d55a34 100644 --- a/src/scripts/pages/chemistry.ts +++ b/src/scripts/pages/chemistry.ts @@ -204,11 +204,11 @@ export function chemistryScript(root: HTMLElement): void { ); registerSearchEntries( - el.map((element, id) => ({ + Array.from(root.querySelectorAll("table.wikitable > tbody > tr:not(:first-child) th .reagent-header")).map((element, id) => ({ page: "Guide_to_chemistry", name: element - .querySelector("th .reagent-header") .textContent.trim() + .replace(/\n.+$/gm, "") .replace("▮", ""), element, alignment: "center", diff --git a/src/scripts/pages/food.ts b/src/scripts/pages/food.ts index 4875ffd..bdadd54 100644 --- a/src/scripts/pages/food.ts +++ b/src/scripts/pages/food.ts @@ -41,6 +41,7 @@ export function processFood(root: HTMLElement): void { ]; baseFoodTables.forEach(({ selector, title, process }) => { const table = root.querySelector(`${selector} .wikitable`); + if (!table) return; const foods = parseTable(table).map((row) => { const foodBlock = document.createElement("td"); foodBlock.innerHTML = `
@@ -108,6 +109,7 @@ export function processFood(root: HTMLElement): void { ]; foodRecipesTables.forEach((selector) => { const table = root.querySelector(`${selector} .wikitable`); + if (!table) return; const recipes = parseTable(table).map((row) => { const foodBlock = document.createElement("td"); foodBlock.innerHTML = ` diff --git a/src/scripts/pages/generic.ts b/src/scripts/pages/generic.ts index f21d8bd..c96c4ff 100644 --- a/src/scripts/pages/generic.ts +++ b/src/scripts/pages/generic.ts @@ -2,7 +2,7 @@ import { registerSearchEntries } from "../search"; export function genericScript(root: HTMLElement, docname: string): void { const el = Array.from( - root.querySelectorAll("div.mw-headline-cont[id][data-name]") + root.querySelectorAll(".mw-headline-cont[id][data-name]") ); // Init fuzzy search with headlines diff --git a/src/scripts/pages/global.ts b/src/scripts/pages/global.ts index 71e037a..089ac03 100644 --- a/src/scripts/pages/global.ts +++ b/src/scripts/pages/global.ts @@ -75,7 +75,10 @@ export function processGlobal(root: HTMLElement, docname: string): void { if (toc) { const tocHeader = toc.querySelector("h2"); toc.parentNode.insertBefore(tocHeader, toc); - toc.removeChild(toc.querySelector("#toctitle")); + const tocTitle = toc.querySelector("#toctitle") + if (tocTitle != null) { + toc.removeChild(tocTitle); + } } // Group headers and content so stickies don't overlap @@ -99,10 +102,13 @@ export function processGlobal(root: HTMLElement, docname: string): void { const container = findParent(span, (el) => el.classList.contains("mw-headline-cont") ); - if (container) { + if (container && container.querySelectorAll(".mw-headline").length === 1) { container.id = span.id; span.id += "-span"; container.dataset.name = span.textContent; + } else { + span.dataset.name = span.textContent; + span.classList.add("mw-headline-cont"); } }); }