Skip to content

Instantly share code, notes, and snippets.

@jacobobryant
Last active February 25, 2023 02:08
Show Gist options
  • Save jacobobryant/1cd4088336a4cbcb431ba17d37cc4ab0 to your computer and use it in GitHub Desktop.
Save jacobobryant/1cd4088336a4cbcb431ba17d37cc4ab0 to your computer and use it in GitHub Desktop.
Some JS functions used in Yakread
// A serverless function for Readability that I'm currently using
let juice = require('juice');
function main(opts) {
let html = juice(opts['html'], { webResources: { images: false } });
return { body: { html } };
}
exports.main = main;
// A serverless function for Readability that I'm currently using
let { Readability } = require('@mozilla/readability');
let { JSDOM } = require('jsdom');
function main(opts) {
let doc = new JSDOM(opts['html'], {url: opts['url']});
let reader = new Readability(doc.window.document);
let article = reader.parse();
return {body: article};
}
exports.main = main;
// This file was previously used in Yakread. I opened it in a subprocess and communicated via pipes.
const readline = require('readline');
var juice;
var Readability;
function callJuice(opts) {
let parsed = juice(opts['html'], {webResources: {images: false}});
return parsed;
}
function callReadability(opts) {
try {
let doc = new JSDOM(opts['html'], { url: opts['url'] });
let reader = new Readability(doc.window.document);
let article = reader.parse();
return article;
} catch (error) {
return {};
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
var opts = JSON.parse(line);
var ret;
switch (opts['command']) {
case 'juice':
if (juice == null) {
juice = require('juice');
}
ret = callJuice(opts);
break;
case 'readability':
if (Readability == null) {
Readability = require('@mozilla/readability')['Readability'];
JSDOM = require('jsdom')['JSDOM'];
}
ret = callReadability(opts);
break;
default:
ret = {error: `command not recognized: ${opts['command']}`};
};
process.stdout.write(JSON.stringify(ret));
process.stdout.write('\n');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment