Some JS functions used in Yakread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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