deno lint

main
gardenapple 2 years ago
parent 247425c6f5
commit 08533d2562
No known key found for this signature in database
GPG Key ID: CAF17E9ABE789268

@ -54,8 +54,8 @@ export default async function(
// //
const Properties = new Map([ const Properties = new Map([
["html-title", (article, singleLine, window) => ["html-title", (article, singleLine, document) =>
`<h1>${escapeHTML(Properties.get("title")(article, singleLine, window), window.document)}</h1>` `<h1>${escapeHTML(Properties.get("title")(article, singleLine, document), document)}</h1>`
], ],
["title", (article, singleLine) => ["title", (article, singleLine) =>
singleLine ? article.title.replace(/\n+/gm, ' ') : article.title singleLine ? article.title.replace(/\n+/gm, ' ') : article.title
@ -69,7 +69,7 @@ export default async function(
["length", article => article.length], ["length", article => article.length],
["dir", article => article.dir], ["dir", article => article.dir],
["text-content", article => article.textContent], ["text-content", article => article.textContent],
["html-content", (article, _, window) => article.content] ["html-content", article => article.content]
]); ]);
const LowConfidenceMode = { const LowConfidenceMode = {
@ -83,7 +83,8 @@ export default async function(
//backwards compat with old, comma-separated values //backwards compat with old, comma-separated values
function yargsCompatProperties(args) { function yargsCompatProperties(args) {
if (args["properties"]) { 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]; const property = args["properties"][i];
if (property.indexOf(',') > -1) { if (property.indexOf(',') > -1) {
const split = args["properties"][i].split(','); const split = args["properties"][i].split(',');
@ -113,7 +114,7 @@ export default async function(
} }
let args = yargs const args = yargs
.version(false) .version(false)
.command("* [source]", __`Process HTML input`, (yargs) => { .command("* [source]", __`Process HTML input`, (yargs) => {
yargs.positional("source", { yargs.positional("source", {
@ -224,7 +225,7 @@ export default async function(
type: "boolean", type: "boolean",
desc: __`Print version` desc: __`Print version`
}) })
.fail((msg, err, yargs) => { .fail((msg, _err, _yargs) => {
console.error(msg); console.error(msg);
setErrored(ExitCodes.badUsageCLI); 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.`) 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); const input = await read(process.stdin);
[document, window] = await parseDOM(result, documentURL); [document, window] = await parseDOM(input, documentURL);
} else { } else {
if (!args["quiet"]) if (!args["quiet"])
console.error(__`Retrieving...`); console.error(__`Retrieving...`);
@ -365,7 +366,8 @@ export default async function(
} }
[document, window] = await parseDOMPromise; [document, window] = await parseDOMPromise;
} }
} catch (error) { } catch (e) {
let error = e
if (error.error) { if (error.error) {
//Nested error? //Nested error?
error = error.error; error = error.error;
@ -397,7 +399,7 @@ export default async function(
//Taken from https://stackoverflow.com/a/22706073/5701177 //Taken from https://stackoverflow.com/a/22706073/5701177
function escapeHTML(string, document) { function escapeHTML(string, document) {
var p = document.createElement("p"); const p = document.createElement("p");
p.appendChild(document.createTextNode(string)); p.appendChild(document.createTextNode(string));
return p.innerHTML; return p.innerHTML;
} }
@ -461,10 +463,10 @@ export default async function(
return; return;
} }
if (outputJSON) { if (outputJSON) {
let result = {}; const result = {};
if (wantedProperties) { if (wantedProperties) {
for (propertyName of wantedProperties) for (propertyName of wantedProperties)
result[propertyName] = Properties.get(propertyName)(article, false, window); result[propertyName] = Properties.get(propertyName)(article, false, document);
} else { } else {
for (const [name, func] of Properties) { for (const [name, func] of Properties) {
result[name] = func(article, false, window); result[name] = func(article, false, window);
@ -474,7 +476,7 @@ export default async function(
} else { } else {
if (wantedProperties) { if (wantedProperties) {
for (propertyName of wantedProperties) for (propertyName of wantedProperties)
writeStream.write(Properties.get(propertyName)(article, true, window) + '\n'); writeStream.write(Properties.get(propertyName)(article, true, document) + '\n');
} else { } else {
writeStream.write(`<!DOCTYPE html> writeStream.write(`<!DOCTYPE html>
<html> <html>
@ -486,7 +488,7 @@ export default async function(
<link rel="stylesheet" href="${cssHref}" type="text/css">`); <link rel="stylesheet" href="${cssHref}" type="text/css">`);
} }
writeStream.write(` writeStream.write(`
<title>${escapeHTML(Properties.get("title")(article, false, window), document)}</title> <title>${escapeHTML(Properties.get("title")(article, false, document), document)}</title>
</head> </head>
` `
); );
@ -497,7 +499,7 @@ export default async function(
<body class="light sans-serif loaded" style="--font-size:14pt; --content-width:40em;"> <body class="light sans-serif loaded" style="--font-size:14pt; --content-width:40em;">
<div class="container" ` <div class="container" `
); );
const contentDir = Properties.get("dir")(article, false, window); const contentDir = Properties.get("dir")(article, false, document);
if (contentDir) if (contentDir)
writeStream.write(`dir="${contentDir}">`); writeStream.write(`dir="${contentDir}">`);
else else
@ -505,9 +507,9 @@ export default async function(
writeStream.write(` writeStream.write(`
<div class="header reader-header reader-show-element"> <div class="header reader-header reader-show-element">
<h1 class="reader-title">${escapeHTML(Properties.get("title")(article, false, window), document)}</h1>`); <h1 class="reader-title">${escapeHTML(Properties.get("title")(article, false, document), document)}</h1>`);
const author = Properties.get("byline")(article, false, window); const author = Properties.get("byline")(article, false, document);
if (author) { if (author) {
writeStream.write(` writeStream.write(`
<div class="credits reader-credits">${escapeHTML(author, document)}</div>`); <div class="credits reader-credits">${escapeHTML(author, document)}</div>`);
@ -522,7 +524,7 @@ export default async function(
<div class="moz-reader-content reader-show-element"> <div class="moz-reader-content reader-show-element">
` `
); );
const html = Properties.get("html-content")(article, false, window); const html = Properties.get("html-content")(article, false, document);
if (!args["insane"]) if (!args["insane"])
writeStream.write(await sanitizeHTML(html, window)); writeStream.write(await sanitizeHTML(html, window));
else else
@ -535,15 +537,15 @@ export default async function(
); );
} else { } else {
writeStream.write("\n<body>\n"); writeStream.write("\n<body>\n");
writeStream.write(Properties.get("html-title")(article, false, window)); writeStream.write(Properties.get("html-title")(article, false, document));
writeStream.write('\n'); writeStream.write('\n');
const author = Properties.get("byline")(article, false, window); const author = Properties.get("byline")(article, false, document);
if (author) { if (author) {
writeStream.write(`<p><i>${escapeHTML(author, document)}</i></p>`); writeStream.write(`<p><i>${escapeHTML(author, document)}</i></p>`);
} }
writeStream.write("\n<hr>\n"); writeStream.write("\n<hr>\n");
const html = Properties.get("html-content")(article, false, window); const html = Properties.get("html-content")(article, false, document);
if (!args["insane"]) if (!args["insane"])
writeStream.write(await sanitizeHTML(html, window)); writeStream.write(await sanitizeHTML(html, window));
else else

@ -0,0 +1,7 @@
{
"lint": {
"files": {
"exclude": ["node_modules/"]
}
}
}

@ -59,11 +59,11 @@ async function parseDOMFromURL(url, proxy, strictSSL, userAgent) {
const dom = await JSDOM.fromURL(url, { const dom = await JSDOM.fromURL(url, {
resources: resourceLoader resources: resourceLoader
}) });
return [dom.window.document, dom.window]; return [dom.window.document, dom.window];
} }
async function parseDOM(html, url) { function parseDOM(html, url) {
const { JSDOM } = require("jsdom"); const { JSDOM } = require("jsdom");
const dom = new JSDOM(html, { url: url }); const dom = new JSDOM(html, { url: url });
return [dom.window.document, dom.window]; return [dom.window.document, dom.window];
@ -75,17 +75,17 @@ async function parseDOMFromFile(file, url) {
url: url, url: url,
// workaround for https://gitlab.com/gardenappl/readability-cli/-/issues/9 // workaround for https://gitlab.com/gardenappl/readability-cli/-/issues/9
contentType: "text/html; charset=utf-8" contentType: "text/html; charset=utf-8"
}) });
return [dom.window.document, dom.window]; return [dom.window.document, dom.window];
} }
async function sanitizeHTML(html, window) { function sanitizeHTML(html, window) {
const createDOMPurify = require("dompurify"); const createDOMPurify = require("dompurify");
const DOMPurify = createDOMPurify(window); const DOMPurify = createDOMPurify(window);
return DOMPurify.sanitize(html); return DOMPurify.sanitize(html);
} }
async function sanitizeDOM(document, window) { function sanitizeDOM(document, window) {
const createDOMPurify = require("dompurify"); const createDOMPurify = require("dompurify");
const DOMPurify = createDOMPurify(window); const DOMPurify = createDOMPurify(window);
DOMPurify.sanitize(document, {IN_PLACE: true, WHOLE_DOCUMENT: true}); DOMPurify.sanitize(document, {IN_PLACE: true, WHOLE_DOCUMENT: true});

@ -38,7 +38,7 @@ function printVersion() {
console.log(`Deno ${Deno.version.deno}`) 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 initParserPromise = initParser()
const userAgentString = userAgent ?? new UserAgent({ deviceCategory: "desktop" }).toString() 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) { async function parseDOM(html: string, url?: string, mimeType?: DOMParserMimeType) {
await initParser() 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 const baseURLString = document.getElementsByTagName("base")[0]?.getAttribute("href") ?? url
if (baseURLString) { if (baseURLString) {
const baseURL = new URL(baseURLString) const baseURL = new URL(baseURLString)
const nodes: Element[] = [] const nodes: Element[] = []
nodes.push(document.documentElement!!) nodes.push(document.documentElement!)
while (nodes.length > 0) { while (nodes.length > 0) {
const element = nodes.pop()!! const element = nodes.pop()!
const href = element.getAttribute("href") const href = element.getAttribute("href")
if (href) { if (href) {
try { try {
// Try to parse absolute URL // Try to parse absolute URL
new URL(href) new URL(href)
} catch (e) { } catch {
// Assume href is a relative URL // Assume href is a relative URL
element.setAttribute("href", new URL(href, baseURL)) element.setAttribute("href", new URL(href, baseURL))
} }
@ -105,7 +105,7 @@ async function sanitizeHTML(html: string) {
} }
async function sanitizeDOM(document: Document) { async function sanitizeDOM(document: Document) {
return sanitizeHTML(document.documentElement!.outerHTML) return await sanitizeHTML(document.documentElement!.outerHTML)
} }

Loading…
Cancel
Save