Coder Social home page Coder Social logo

readability's Introduction

Readability.js

Build Status

A standalone version of the readability library used for Firefox Reader View. Any changes to Readability.js itself should be reviewed by an appropriate Firefox/toolkit peer, such as @gijsk, since these changes will be automatically merged to mozilla-central.

Contributing

For outstanding issues, see the issue list in this repo, as well as this bug list.

To test local changes to Readability.js, you can use the automated tests. There's a node script to help you create new ones.

Note that because JSDOMParser is restricted to parsing XHTML-compatible input, you will likely need to tweak any input you fetch directly from the internet (e.g. to close <meta> tags). Even if creating a 'readable' version fails, the script will leave the input for you to change. You can then re-run the generate-testcase.js script passing only the test page slug, and it will reuse the altered input. Ideally we should fix the generate-testcase.js script to no longer need this manual pre/post-processing. If you have time to help with this, a pull request would be very welcome!

Please make sure to run eslint against any proposed changes when creating a pull request.

Usage

To parse a document, you must create a new Readability object from a URI object and a document object, and then call parse(). Here's an example:

var loc = document.location;
var uri = {
  spec: loc.href,
  host: loc.host,
  prePath: loc.protocol + "//" + loc.host,
  scheme: loc.protocol.substr(0, loc.protocol.indexOf(":")),
  pathBase: loc.protocol + "//" + loc.host + loc.pathname.substr(0, loc.pathname.lastIndexOf("/") + 1)
};
var article = new Readability(uri, document).parse();

This article object will contain the following properties:

  • uri: original uri object that was passed to constructor
  • title: article title
  • content: HTML string of processed article content
  • length: length of article, in characters
  • excerpt: article description, or short excerpt from content
  • byline: author metadata
  • dir: content direction

If you're using Readability on the web, 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.). Otherwise, you would need to construct such an object using a DOM parser such as jsdom. While this repository contains a parser of its own (JSDOMParser), that is restricted to reading XML-compatible markup and therefore we do not recommend it for general use.

Until #346 is fixed, if you're using jsdom and node (rather than running in a browser), you will need to also ensure a global Node object is available for Readability to use, e.g. like so:

...
var dom = new JSDOM(html, OPTIONS);
Node = dom.window.Node;
var article = new Readability(uri, dom.window.document).parse();

Optional

Readability's parse() works by modifying the DOM. This removes some elements in the web page. You could avoid this by passing the clone of the document object while creating a Readability object.

var documentClone = document.cloneNode(true); 
var article = new Readability(uri, documentClone).parse();   

Tests

Please run eslint as a first check that your changes adhere to our style guidelines.

To run the test suite:

$ mocha test/test-*.js

To run a specific test page by its name:

$ mocha test/test-*.js -g 001

To run the test suite in TDD mode:

$ mocha test/test-*.js -w

Combo time:

$ mocha test/test-*.js -w -g 001

Benchmarks

Benchmarks for all test pages:

$ npm run perf

Reference benchmark:

$ npm run perf-reference

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's People

Contributors

gijsk avatar n1k0 avatar leibovic avatar evanxd avatar andreskrey avatar andrei-ch avatar heycam avatar hsemarap avatar pdehaan avatar srlakhe avatar tigt avatar tarekziade avatar st3fan avatar treora avatar usergit avatar zoehneto avatar backflip avatar archaeopteryx avatar sahilagarwal321 avatar persidskiy avatar iq-dot avatar farbdose avatar davidbruant avatar brendanlong avatar bradphilips avatar nifgraup avatar almet avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.