diff --git a/common.mjs b/common.mjs index fab9508..139f5c3 100644 --- a/common.mjs +++ b/common.mjs @@ -54,8 +54,8 @@ export default async function( // const Properties = new Map([ - ["html-title", (article, singleLine, window) => - `

${escapeHTML(Properties.get("title")(article, singleLine, window), window.document)}

` + ["html-title", (article, singleLine, document) => + `

${escapeHTML(Properties.get("title")(article, singleLine, document), document)}

` ], ["title", (article, singleLine) => singleLine ? article.title.replace(/\n+/gm, ' ') : article.title @@ -69,7 +69,7 @@ export default async function( ["length", article => article.length], ["dir", article => article.dir], ["text-content", article => article.textContent], - ["html-content", (article, _, window) => article.content] + ["html-content", article => article.content] ]); const LowConfidenceMode = { @@ -83,7 +83,8 @@ export default async function( //backwards compat with old, comma-separated values function yargsCompatProperties(args) { if (args["properties"]) { - for (var i = 0; i < args["properties"].length; i++) { + let i; + for (i = 0; i < args["properties"].length; i++) { const property = args["properties"][i]; if (property.indexOf(',') > -1) { const split = args["properties"][i].split(','); @@ -113,7 +114,7 @@ export default async function( } - let args = yargs + const args = yargs .version(false) .command("* [source]", __`Process HTML input`, (yargs) => { yargs.positional("source", { @@ -224,7 +225,7 @@ export default async function( type: "boolean", desc: __`Print version` }) - .fail((msg, err, yargs) => { + .fail((msg, _err, _yargs) => { console.error(msg); setErrored(ExitCodes.badUsageCLI); }) @@ -352,7 +353,7 @@ export default async function( console.error(__`Warning: piping input with unknown URL. This means that relative links will be broken. Supply the --base parameter to fix.`) } const input = await read(process.stdin); - [document, window] = await parseDOM(result, documentURL); + [document, window] = await parseDOM(input, documentURL); } else { if (!args["quiet"]) console.error(__`Retrieving...`); @@ -365,7 +366,8 @@ export default async function( } [document, window] = await parseDOMPromise; } - } catch (error) { + } catch (e) { + let error = e if (error.error) { //Nested error? error = error.error; @@ -397,7 +399,7 @@ export default async function( //Taken from https://stackoverflow.com/a/22706073/5701177 function escapeHTML(string, document) { - var p = document.createElement("p"); + const p = document.createElement("p"); p.appendChild(document.createTextNode(string)); return p.innerHTML; } @@ -461,10 +463,10 @@ export default async function( return; } if (outputJSON) { - let result = {}; + const result = {}; if (wantedProperties) { for (propertyName of wantedProperties) - result[propertyName] = Properties.get(propertyName)(article, false, window); + result[propertyName] = Properties.get(propertyName)(article, false, document); } else { for (const [name, func] of Properties) { result[name] = func(article, false, window); @@ -474,7 +476,7 @@ export default async function( } else { if (wantedProperties) { for (propertyName of wantedProperties) - writeStream.write(Properties.get(propertyName)(article, true, window) + '\n'); + writeStream.write(Properties.get(propertyName)(article, true, document) + '\n'); } else { writeStream.write(` @@ -486,7 +488,7 @@ export default async function( `); } writeStream.write(` - ${escapeHTML(Properties.get("title")(article, false, window), document)} + ${escapeHTML(Properties.get("title")(article, false, document), document)} ` ); @@ -497,7 +499,7 @@ export default async function(
`); else @@ -505,9 +507,9 @@ export default async function( writeStream.write(`
-

${escapeHTML(Properties.get("title")(article, false, window), document)}

`); +

${escapeHTML(Properties.get("title")(article, false, document), document)}

`); - const author = Properties.get("byline")(article, false, window); + const author = Properties.get("byline")(article, false, document); if (author) { writeStream.write(`
${escapeHTML(author, document)}
`); @@ -522,7 +524,7 @@ export default async function(
` ); - const html = Properties.get("html-content")(article, false, window); + const html = Properties.get("html-content")(article, false, document); if (!args["insane"]) writeStream.write(await sanitizeHTML(html, window)); else @@ -535,15 +537,15 @@ export default async function( ); } else { writeStream.write("\n\n"); - writeStream.write(Properties.get("html-title")(article, false, window)); + writeStream.write(Properties.get("html-title")(article, false, document)); writeStream.write('\n'); - const author = Properties.get("byline")(article, false, window); + const author = Properties.get("byline")(article, false, document); if (author) { writeStream.write(`

${escapeHTML(author, document)}

`); } writeStream.write("\n
\n"); - const html = Properties.get("html-content")(article, false, window); + const html = Properties.get("html-content")(article, false, document); if (!args["insane"]) writeStream.write(await sanitizeHTML(html, window)); else diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..8990fab --- /dev/null +++ b/deno.json @@ -0,0 +1,7 @@ +{ + "lint": { + "files": { + "exclude": ["node_modules/"] + } + } +} diff --git a/index.js b/index.js index e4ab672..d13968c 100755 --- a/index.js +++ b/index.js @@ -59,11 +59,11 @@ async function parseDOMFromURL(url, proxy, strictSSL, userAgent) { const dom = await JSDOM.fromURL(url, { resources: resourceLoader - }) + }); return [dom.window.document, dom.window]; } -async function parseDOM(html, url) { +function parseDOM(html, url) { const { JSDOM } = require("jsdom"); const dom = new JSDOM(html, { url: url }); return [dom.window.document, dom.window]; @@ -75,17 +75,17 @@ async function parseDOMFromFile(file, url) { url: url, // workaround for https://gitlab.com/gardenappl/readability-cli/-/issues/9 contentType: "text/html; charset=utf-8" - }) + }); return [dom.window.document, dom.window]; } -async function sanitizeHTML(html, window) { +function sanitizeHTML(html, window) { const createDOMPurify = require("dompurify"); const DOMPurify = createDOMPurify(window); return DOMPurify.sanitize(html); } -async function sanitizeDOM(document, window) { +function sanitizeDOM(document, window) { const createDOMPurify = require("dompurify"); const DOMPurify = createDOMPurify(window); DOMPurify.sanitize(document, {IN_PLACE: true, WHOLE_DOCUMENT: true}); diff --git a/readable.ts b/readable.ts index a7a7859..1e19ba9 100644 --- a/readable.ts +++ b/readable.ts @@ -38,7 +38,7 @@ function printVersion() { console.log(`Deno ${Deno.version.deno}`) } -async function parseDOMFromURL(url: string, proxy?: string, strictSSL?: boolean, userAgent?: string) { +async function parseDOMFromURL(url: string, _proxy: string, _strictSSL: boolean, userAgent: string) { const initParserPromise = initParser() const userAgentString = userAgent ?? new UserAgent({ deviceCategory: "desktop" }).toString() @@ -67,22 +67,22 @@ async function parseDOMFromURL(url: string, proxy?: string, strictSSL?: boolean, async function parseDOM(html: string, url?: string, mimeType?: DOMParserMimeType) { await initParser() - const document = new DOMParser().parseFromString(html, mimeType ?? "text/html")!! + const document = new DOMParser().parseFromString(html, mimeType ?? "text/html")! const baseURLString = document.getElementsByTagName("base")[0]?.getAttribute("href") ?? url if (baseURLString) { const baseURL = new URL(baseURLString) const nodes: Element[] = [] - nodes.push(document.documentElement!!) + nodes.push(document.documentElement!) while (nodes.length > 0) { - const element = nodes.pop()!! + const element = nodes.pop()! const href = element.getAttribute("href") if (href) { try { // Try to parse absolute URL new URL(href) - } catch (e) { + } catch { // Assume href is a relative URL element.setAttribute("href", new URL(href, baseURL)) } @@ -105,7 +105,7 @@ async function sanitizeHTML(html: string) { } async function sanitizeDOM(document: Document) { - return sanitizeHTML(document.documentElement!.outerHTML) + return await sanitizeHTML(document.documentElement!.outerHTML) }