Skip to content

Commit

Permalink
Fix Cheerio 1.0.0 type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kfranqueiro committed Oct 9, 2024
1 parent 828079a commit 1f74470
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
9 changes: 4 additions & 5 deletions 11ty/CustomLiquid.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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<Element>) {
function expandTechniqueLink($el: CheerioAnyNode) {
const href = $el.attr("href");
if (!href) throw new Error("expandTechniqueLink: non-link element encountered");
const id = resolveTechniqueIdFromHref(href);
Expand Down Expand Up @@ -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<Element>) => {
const extractTermName = ($el: CheerioAnyNode) => {
const name = $el
.text()
.toLowerCase()
Expand All @@ -432,7 +431,7 @@ export class CustomLiquid extends Liquid {
});
} else if (scope.isUnderstanding) {
const $termsList = $("section#key-terms dl");
const extractTermNames = ($links: Cheerio<Element>) =>
const extractTermNames = ($links: CheerioAnyNode) =>
compact(uniq($links.toArray().map((el) => extractTermName($(el)))));

if ($termLinks.length) {
Expand Down
3 changes: 3 additions & 0 deletions 11ty/cheerio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof load>>;

/** Convenience function that combines readFile and load. */
export const loadFromFile = async (
inputPath: string,
Expand Down
6 changes: 3 additions & 3 deletions 11ty/guidelines.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<Element>) => {
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();
Expand Down

0 comments on commit 1f74470

Please sign in to comment.