An open API service indexing awesome lists of open source software.

https://github.com/masylum/cheer-reader

A port of readability.js for cheerio
https://github.com/masylum/cheer-reader

Last synced: 6 months ago
JSON representation

A port of readability.js for cheerio

Awesome Lists containing this project

README

          

![cheer-reader](https://github.com/masylum/cheer-reader/blob/main/img/cheer-reader.png?raw=true)

This library is a port of the [readability.js](https://github.com/mozilla/readability) library
that uses [cheerio](https://cheerio.js.org) instead of native DOM APIs.

The goals of this port are the following:

- **Portability**: Most runtimes and Web Workers don't support DOM apis.
By using cheerio, we ensure that you can run readability wherever you want.
If your goal is to run it in the main thread of the browser, you can stick to the original
implementation.
- **Performance**: For non-native runtimes, we rely in JSDOM to simulate native DOM APIs. JSDOM
does a lot besides parsing HTML, and is pretty heavy and slow. **cheerio** instead is lightweight
and fast. Overall, I've experienced that this port performs 6-8x times faster and consumes a lot
less memory than the original one. You can even use [htmlparser2](https://github.com/fb55/htmlparser2)
if you need more performance.
- **Compatibility**: In order to avoid using JSOM, you could use readability's [JSDOMParser](https://github.com/mozilla/readability/blob/main/JSDOMParser.js),
[Happy Dom](https://github.com/capricorn86/happy-dom) or [Linkedom](https://github.com/WebReflection/linkedom).
I had compatibility problems using those alternative implementations, but **cheerio** worked flawlesly even
with the most broken html documents that you can find on the internet. You even have the option to use
[htmlparser2](https://github.com/fb55/htmlparser2) instead of **cheerio**'s default [parse5](https://github.com/inikulin/parse5)
for less strict parsing.
- **Extensability**: The original implementation is pretty hard to read and maintain. While I didn't deviate
much from it, I tried to modernize the code base a little bit. I hope people can help me out, specially with
the `_grabArticle` gigantic method with nested while loops. I also saw a lot of issues on the repo for top pages
that didn't seem taken care of. It is my intention to try to fix those as I move forward.

The implementation aims to be the same and it keeps the same test set to ensure backwards compatibility.

## Installation

`cheer-reader` is available on jsr:

```bash
npx jsr add @paoramen/cheer-reader
```

## Basic usage

To parse a document, you must create a new `Readability` object from a DOM document object, and then call the [`parse()`](#parse) method. Here's an example:

```javascript
import { Readability } from 'cheer-reader'

const $ = load('html>

yo