readability README

The project's README from github.com/mozilla/readability. As one markdown file: README.md.

Readability.js

A standalone version of the readability library used for Firefox Reader View.

Installation

Readability is available on npm:

npm install @mozilla/readability

You can then require() it, or for web-based projects, load the Readability.js script from your webpage.

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();

If you use Readability in a web browser, you will likely be able to use a document reference from elsewhere (e.g. fetched via XMLHttpRequest, in a same-origin <iframe> you have access to, etc.). In Node.js, you can use an external DOM library.

API Reference

new Readability(document, options)

The options object accepts a number of properties, all optional:

parse()

Returns an object containing the following properties:

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:

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();
}

Node.js usage

Since Node.js does not come with its own DOM implementation, we rely on external libraries like jsdom. Here's an example using jsdom to obtain a DOM document object:

var { Readability } = require('@mozilla/readability');
var { JSDOM } = require('jsdom');
var doc = new JSDOM("<body>Look at this cat: <img src='./cat.jpg'></body>", {
  url: "https://www.example.com/the-page-i-got-the-source-from"
});
let reader = new Readability(doc.window.document);
let article = reader.parse();

Remember to pass the page's URI as the url option in the JSDOM constructor (as shown in the example above), so that Readability can convert relative URLs for images, hyperlinks, etc. to their absolute counterparts.

jsdom has the ability to run the scripts included in the HTML and fetch remote resources. For security reasons these are disabled by default, and we strongly recommend you keep them that way.

Security

If you're going to use Readability with untrusted input (whether in HTML or DOM form), we strongly recommend you use a sanitizer library like DOMPurify to avoid script injection when you use the output of Readability. We would also recommend using CSP to add further defense-in-depth restrictions to what you allow the resulting content to do. The Firefox integration of reader mode uses both of these techniques itself. Sanitizing unsafe content out of the input is explicitly not something we aim to do as part of Readability itself - there are other good sanitizer libraries out there, use them!

Contributing

Please see our Contributing document.

License

Copyright (c) 2010 Arc90 Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

readability (mozilla/readability)

Searching Database…

这是什么?Readability.js 是一个独立的内容提取库,可以从杂乱的网页中提取出正文,并去掉广告、导航栏和侧边栏。它也是 Firefox 阅读视图背后所用的同一个库。
这是浏览器扩展,还是我可以在代码中调用的库?它是一个普通的 JavaScript 库,不是打包好的浏览器扩展。你需要基于 DOM 文档创建 Readability 对象,并自己调用 parse(),可以在网页中使用,也可以在 Node.js 中使用。
如何把它添加到项目中?可以通过 npm 安装后用 require() 引入,也可以直接在网页中用 script 标签加载 Readability.js。在 Node.js 中需要搭配 jsdom 之类的外部 DOM 库使用,因为 Node 没有内置 DOM。
在 Node 中使用它,我需要 DOM 库吗?需要。Node.js 本身不提供 DOM 实现,所以你需要 jsdom(或类似的库)来构建文档对象,再把它传给 Readability。
它会对提取出的 HTML 做清理吗?不会。Readability 只负责提取正文,不做清理。如果输入内容不可信,你需要自己用 DOMPurify 之类的清理工具处理输出结果。
谁会用到它?主要是开发阅读模式功能、文章提取器、RSS 或内容处理流程的开发者,以及任何需要把原始网页转换为干净可读文本的人。
它既能在浏览器中运行,也能在服务器上运行吗?两者都可以:它可以直接在浏览器中使用页面自身的 DOM,也可以在服务器端搭配 jsdom 在 Node.js 中运行。
在完整提取之前,有没有快速判断页面是否值得解析的方法?有。isProbablyReaderable(document, options) 是一个轻量的启发式检查,用来估算 parse() 是否可能成功,这样就不必对每个页面都运行较重的逻辑。
parse() 到底返回什么?返回一个对象,包含 title、content(HTML 字符串)、textContent(纯文本)、length、excerpt、byline、dir、siteName、lang 和 publishedTime。
提取出的内容能保留一些 CSS 类名吗?可以。通过 keepClasses 选项可以保留所有类名;如果只想保留特定的类名,可以用 classesToPreserve 设置白名单,其余的类名会被去掉。
解析会修改原始页面的 DOM 吗?会。parse() 会直接修改传入的文档,所以如果需要保留原始 DOM,请把克隆后的文档传给 Readability 构造函数。
Creator
mozilla
Size
6.9 MB
Stars
11.5K
Updated
CommitJul 09
Age
11y
Commits · 90 Days