Readenobility.js
An implementation of the standalone version of the readability library used for Firefox Reader View for Deno. Implemented with Deno/TypeScript.
Installation
@TODO
Basic usage
To parse a document, you must create a new Readability
object from a DOM
document object, and then call the parse()
method. Hereās an
example:
var article = new Readability(document).parse();
Deno usage
Readenobility relies on external libraries like
deno-dom. Hereās an example using
deno-dom
to obtain a DOM document object:
import "https://deno.land/x/readenobility@0.5.1-rc.10/mod.ts";
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const doc = new DOMParser().parseFromString(
`
<h1>Hello World!</h1>
<p>Hello from <a href="https://deno.land/">Deno!</a></p>
`,
"text/html",
)!;
let reader = new Readability(document);
let article = reader.parse();
Remember to pass the pageās URI as the url
option in the deno-dom
parseFrag
(as shown in the example above), so that Readability can convert
relative URLs for images, hyperlinks, etc. to their absolute counterparts.
API Reference
new Readability(document, options)
The options
object accepts a number of properties, all optional:
debug
(boolean, defaultfalse
): whether to enable logging.maxElemsToParse
(number, default0
i.e. no limit): the maximum number of elements to parse.nbTopCandidates
(number, default5
): the number of top candidates to consider when analysing how tight the competition is among candidates.charThreshold
(number, default500
): the number of characters an article must have in order to return a result.classesToPreserve
(array): a set of classes to preserve on HTML elements when thekeepClasses
options is set tofalse
.keepClasses
(boolean, defaultfalse
): whether to preserve all classes on HTML elements. When set tofalse
only classes specified in theclassesToPreserve
array are kept.disableJSONLD
(boolean, defaultfalse
): when extracting page metadata, Readability gives precedence to Schema.org fields specified in the JSON-LD format. Set this option totrue
to skip JSON-LD parsing.serializer
(function, defaultel => el.innerHTML
) controls how thecontent
property returned by theparse()
method is produced from the root DOM element. It may be useful to specify theserializer
as the identity function (el => el
) to obtain a DOM element instead of a string forcontent
if you plan to process it further.allowedVideoRegex
(RegExp, defaultundefined
): a regular expression that matches video URLs that should be allowed to be included in the article content. Ifundefined
, the default regex is applied.
parse()
Returns an object containing the following properties:
title
: article title;content
: HTML string of processed article content;textContent
: text content of the article, with all the HTML tags removed;length
: length of an article, in characters;excerpt
: article description, or short excerpt from the content;byline
: author metadata;dir
: content direction;siteName
: name of the site;lang
: content language;publishedTime
: published time;
The parse()
method works by modifying the DOM. This removes some elements in
the web page, which may be undesirable. You can avoid this by passing the clone
of the document
object to the Readability
constructor:
var documentClone = document.cloneNode(true);
var article = new Readability(documentClone).parse();
isProbablyReaderable(document, options)
A quick-and-dirty way of figuring out if itās plausible that the contents of a given document are suitable for processing with Readability. It is likely to produce both false positives and false negatives. The reason it exists is to avoid bogging down a time-sensitive process (like loading and showing the user a webpage) with the complex logic in the core of Readability. Improvements to its logic (while not deteriorating its performance) are very welcome.
The options
object accepts a number of properties, all optional:
minContentLength
(number, default140
): the minimum node content length used to decide if the document is readerable;minScore
(number, default20
): the minimum cumulated āscoreā used to determine if the document is readerable;visibilityChecker
(function, defaultisNodeVisible
): the function used to determine if a node is visible;
The function returns a boolean corresponding to whether or not we suspect
Readability.parse()
will succeed at returning an article object. Hereās an
example:
/*
Only instantiate Readability if we suspect
the `parse()` method will produce a meaningful result.
*/
if (isProbablyReaderable(document)) {
let article = new Readability(document).parse();
}