From 01276e726bd36855cd7cf4c9b385b6b0956aa500 Mon Sep 17 00:00:00 2001 From: gardenapple Date: Wed, 15 Jul 2020 15:42:22 +0300 Subject: [PATCH] v1.0.3: better stability & performance + no URL warning --- index.js | 34 ++++++++++++++++++++++++++-------- package-lock.json | 7 +------ package.json | 3 +-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index cd622b7..2335e5d 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ - const Readability = require("readability"); const JSDOM = require("jsdom").JSDOM; const parseArgs = require("minimist"); -const fs = require("fs"); -const he = require("he"); const ExitCodes = { @@ -193,11 +190,24 @@ if (args.properties) { } } - +async function read(stream) { + const chunks = []; + for await (const chunk of stream){ + chunks.push(chunk); + } + return Buffer.concat(chunks).toString('utf8'); +} if (inputIsFromStdin) { - onLoadDOM(new JSDOM(fs.readFileSync(0, 'utf-8')), { - url: documentURL + if (!args["quiet"]) { + console.error("Reading..."); + if (!documentURL) + console.error("Note: piping input with unknown " + + "URL. This means that relative links will " + + "be broken. Supply the --url parameter to fix.") + } + read(process.stdin).then(result => { + onLoadDOM(new JSDOM(result, { url: documentURL })); }); } else { if (!args["quiet"]) @@ -221,8 +231,15 @@ if (inputIsFromStdin) { promiseGetHTML.then(onLoadDOM, onLoadDOMError) } +//Taken from https://stackoverflow.com/a/22706073/5701177 +function escapeHTML(string, document){ + var p = document.createElement("p"); + p.appendChild(document.createTextNode(string)); + return p.innerHTML; +} + function onLoadDOM(dom) { - const document = dom.window.document + const document = dom.window.document; if (!args["quiet"]) console.error("Parsing..."); let reader = new Readability(document); @@ -235,6 +252,7 @@ function onLoadDOM(dom) { let writeStream; if (outputArg) { + const fs = require("fs"); writeStream = fs.createWriteStream(outputArg); } else { writeStream = process.stdout; @@ -256,7 +274,7 @@ function onLoadDOM(dom) { writeStream.write(`Direction: ${article.dir}\n`); } if (wantedProperties.includes(Properties.htmlTitle)) { - writeStream.write(`

${he.escape(article.title)}

\n`); + writeStream.write(`

${escapeHTML(article.title, document)}

\n`); } if (wantedProperties.includes(Properties.htmlContent)) { writeStream.write(article.content); diff --git a/package-lock.json b/package-lock.json index d1d880e..f6fe0c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@gardenapple/readability-cli", - "version": "1.0.0", + "version": "1.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -264,11 +264,6 @@ "har-schema": "^2.0.0" } }, - "he": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", - "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==" - }, "html-encoding-sniffer": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-2.0.1.tgz", diff --git a/package.json b/package.json index 6f4cf6c..d856391 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gardenapple/readability-cli", - "version": "1.0.2", + "version": "1.0.3", "description": "Firefox Reader Mode in your terminal - get useful text from a web page using Mozilla's Readability library", "main": "index.js", "bin": { @@ -21,7 +21,6 @@ ], "license": "GPL-3.0-only", "dependencies": { - "he": "^1.2.0", "jsdom": "^16.3.0", "minimist": "^1.2.5", "readability": "github:mozilla/readability#master"