From af74a4aa9b0adc79972dfeff6056e22278fa2ab1 Mon Sep 17 00:00:00 2001 From: Daniel Bowring Date: Sun, 20 Oct 2024 14:51:10 +1100 Subject: [PATCH] Add hack to monkeypatch mozilla/readability#918 --- src/hacks.ts | 21 +++++++++++++++++++++ src/index.ts | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 src/hacks.ts diff --git a/src/hacks.ts b/src/hacks.ts new file mode 100644 index 0000000..9920b24 --- /dev/null +++ b/src/hacks.ts @@ -0,0 +1,21 @@ +import validateNames from "jsdom/lib/jsdom/living/helpers/validate-names.js"; + +const applyJSDOMInvalidARgumentHack = () => { + // See: https://github.com/mozilla/readability/pull/918 + const originalNameValidator = validateNames.name; + // @ts-expect-error + validateNames.name = (...args) => { + try { + originalNameValidator(...args); + } catch (ex) { + // @ts-expect-error + if (ex?.name !== "InvalidCharacterError") { + throw ex; + } + } + }; +}; + +export const applyHacks = () => { + applyJSDOMInvalidARgumentHack(); +}; diff --git a/src/index.ts b/src/index.ts index 7b88b95..a452d8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { JSDOM } from "jsdom"; import { parseArgs } from "./cli"; +import { applyHacks } from "./hacks"; import { readData, writeData } from "./io"; import { getReadability } from "./readability"; import { sanitizeHtml } from "./sanitize"; @@ -20,6 +21,7 @@ const getResult = async (data: string | Buffer, options: Options) => { }; const main = async () => { + applyHacks(); const options = parseArgs(); const data = await readData(options.cli.path, options.cli.encoding); const result = await getResult(data, options);