diff --git a/11ty/CustomLiquid.ts b/11ty/CustomLiquid.ts index 72f5357ceb..6b1a4394b6 100644 --- a/11ty/CustomLiquid.ts +++ b/11ty/CustomLiquid.ts @@ -1,4 +1,3 @@ -import type { Cheerio, Element } from "cheerio"; import { Liquid, type Template } from "liquidjs"; import type { RenderOptions } from "liquidjs/dist/liquid-options"; import compact from "lodash-es/compact"; @@ -8,7 +7,7 @@ import { basename } from "path"; import type { GlobalData } from "eleventy.config"; -import { flattenDom, load } from "./cheerio"; +import { flattenDom, load, type CheerioAnyNode } from "./cheerio"; import { generateId } from "./common"; import { getAcknowledgementsForVersion, getTermsMap } from "./guidelines"; import { resolveTechniqueIdFromHref, understandingToTechniqueLinkSelector } from "./techniques"; @@ -61,7 +60,7 @@ const normalizeTocLabel = (label: string) => * expand to a link with the full technique ID and title. * @param $el a $()-wrapped link element */ -function expandTechniqueLink($el: Cheerio) { +function expandTechniqueLink($el: CheerioAnyNode) { const href = $el.attr("href"); if (!href) throw new Error("expandTechniqueLink: non-link element encountered"); const id = resolveTechniqueIdFromHref(href); @@ -407,7 +406,7 @@ export class CustomLiquid extends Liquid { // Process defined terms within #render, // where we have access to global data and the about box's HTML const $termLinks = $(termLinkSelector); - const extractTermName = ($el: Cheerio) => { + const extractTermName = ($el: CheerioAnyNode) => { const name = $el .text() .toLowerCase() @@ -432,7 +431,7 @@ export class CustomLiquid extends Liquid { }); } else if (scope.isUnderstanding) { const $termsList = $("section#key-terms dl"); - const extractTermNames = ($links: Cheerio) => + const extractTermNames = ($links: CheerioAnyNode) => compact(uniq($links.toArray().map((el) => extractTermName($(el))))); if ($termLinks.length) { diff --git a/11ty/cheerio.ts b/11ty/cheerio.ts index f1292be14b..72f3c65008 100644 --- a/11ty/cheerio.ts +++ b/11ty/cheerio.ts @@ -5,6 +5,9 @@ import { dirname, resolve } from "path"; export { load } from "cheerio"; +/** Superset of the type returned by any Cheerio $() call. */ +export type CheerioAnyNode = ReturnType>; + /** Convenience function that combines readFile and load. */ export const loadFromFile = async ( inputPath: string, diff --git a/11ty/guidelines.ts b/11ty/guidelines.ts index e033c0964e..b4501461bb 100644 --- a/11ty/guidelines.ts +++ b/11ty/guidelines.ts @@ -1,11 +1,11 @@ import axios from "axios"; -import type { Cheerio, CheerioAPI, Element } from "cheerio"; +import type { CheerioAPI } from "cheerio"; import { glob } from "glob"; import { readFile } from "fs/promises"; import { basename } from "path"; -import { flattenDomFromFile, load } from "./cheerio"; +import { flattenDomFromFile, load, type CheerioAnyNode } from "./cheerio"; import { generateId } from "./common"; export type WcagVersion = "20" | "21" | "22"; @@ -107,7 +107,7 @@ const contentIgnores = [ * Returns HTML content used for Understanding guideline/SC boxes. * @param $el Cheerio element of the full section from flattened guidelines/index.html */ -const getContentHtml = ($el: Cheerio) => { +const getContentHtml = ($el: CheerioAnyNode) => { // Load HTML into a new instance, remove elements we don't want, then return the remainder const $ = load($el.html()!, null, false); $(contentIgnores.join(", ")).remove();