Skip to content

Instantly share code, notes, and snippets.

@htruong
Last active July 26, 2021 05:20
Show Gist options
  • Save htruong/692b1bca7b94db20051b601c89a44de9 to your computer and use it in GitHub Desktop.
Save htruong/692b1bca7b94db20051b601c89a44de9 to your computer and use it in GitHub Desktop.
Zenreader server
export default (title, content) => `
<html>
<head>
<title>${title}</title>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/combine/npm/[email protected]/build/base-min.css,npm/[email protected]/build/grids-min.css,npm/[email protected]/build/forms-min.css"
/>
<style>
img {
max-width: 100%;
}
</style>
</head>
<body>
<div class="pure-g">
<div class="pure-u-1-3">
<h1>${title}</h1>
${content}
</div>
</div>
</body>
</html>
`
/*
Copyright 2021 Huan Truong <[email protected]> & Chisomo Sakala
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Full text of license: GPLv2: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
import { Readability } from '@mozilla/readability'
import striptags from 'striptags'
import request from 'request'
import { JSDOM } from 'jsdom'
import Feed from 'rss-to-json'
import jsonFilter from 'node-json-filter'
import express from 'express'
import template from './template.js'
const app = express()
const port = 3000
const filter = new jsonFilter({
items: [{ url: '$string', title: '$string' }],
})
function make_readable(body, url) {
const doc = new JSDOM(body, {url})
const reader = new Readability(doc.window.document)
return reader.parse()
}
function article(req, res) {
// console.log(req);
//res.send('Hello World!')
const url = req.query.url
const contentType = req.query.contentType
console.log(`GET /article/ url=${url}`)
request.get(url, (error, response, body) => {
// callback(make_readable(body), true)
const { content, title } = make_readable(body)
//console.log(obj);
if (contentType !== 'html') {
res.setHeader('Content-Type', 'text/plain')
let txt = 'ERROR!'
if (content !== null) {
txt = content
}
//txt = striptags(striptags(txt, ['p']), '', '\n');
//txt = striptags(txt, ['p']);
txt = txt.replace(/\n/gm, ' ')
txt = striptags(txt, ['p'], '')
//console.log(txt);
txt = striptags(txt, [], '\n')
const blankLines = new RegExp(/(^[ \t]*)/, 'gm')
txt = txt.replace(blankLines, '')
txt = txt.replace(/^(\n){2,}/gm, '\n')
//txt = txt.replace(/ /g, '-');
res.send(`${title}\n==============\n\n${txt}`)
} else {
res.send(template(title, content))
}
})
}
///////////////
app.get('/article/', article)
app.get('/feed/', (req, res) => {
const url = req.query.url
console.log(`GET /feed/ url=${url}`)
Feed.load(url, (err, { items }) => {
const rss_trunc_items = items.slice(0, 15)
const rss_trunc = {}
rss_trunc.items = rss_trunc_items
res.setHeader('Content-Type', 'text/json')
res.send(JSON.stringify(filter.apply(rss_trunc), null, 3))
})
})
app.listen(port, '0.0.0.0', () => {
console.log(`Example app listening at http://localhost:${port}`)
})
@andrewstuart
Copy link

Yes! Honestly this is some of the least-hacky "hacky" code I've seen. You've got a readable, usable solution in ~100 lines and that'll get you a lot further than perfectly-organized overly-abstracted code, which is almost always harder to follow. Love it!

@tracker1
Copy link

@htruong,

I'm with @andrewstuart on this one... not so hacky at all... I've seen stuff that could make your eyes bleed by comparison. Really decent work here... working is better than not and often it's just a matter of getting things done. We all want to do better, no need to get hung up.

For similar stuff handing copy/pasted of rich text / html, I've used html->markdown, then strip certain tags/properties, then minor cleanup and back to html. Not sure if that approach may work a little faster/better than jsdom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment