1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-02 14:49:16 +02:00
JS epub parser/reader
Find a file
2023-05-17 11:24:01 -07:00
documentation/md Fix typos 2021-12-16 15:03:30 +08:00
examples Fix MathML example 2022-11-15 18:29:38 +08:00
src Merge dcf108e144 into f09089cf77 2023-05-17 11:24:01 -07:00
test Fix typos 2021-12-16 15:03:30 +08:00
types Merge pull request #1319 from laterbuy/master 2023-05-15 22:01:29 -07:00
.babelrc.json WIP: Update to Webpack 4 and Babel 7 (#835) 2020-06-04 21:38:39 -07:00
.eslintrc.js Added type declarations 2018-08-20 22:21:26 -07:00
.gitignore Added type declarations 2018-08-20 22:21:26 -07:00
.jshintrc Added webpack build, karma test runner, travis config 2016-10-26 17:05:27 +02:00
.nojekyll Turned of Jekyll 2013-11-16 23:48:19 -08:00
.npmignore Ignore .babelrc for npm 2016-12-09 13:19:01 +01:00
.travis.yml Clean up pageList 2017-11-06 11:39:14 -08:00
.watchmanconfig added polymer/url library 2016-11-15 15:21:56 +01:00
bower.json WIP: Update to Webpack 4 and Babel 7 (#835) 2020-06-04 21:38:39 -07:00
documentation.yml Added pageList, initial documentation 2016-11-10 23:08:39 +01:00
karma.conf.js WIP: Update to Webpack 4 and Babel 7 (#835) 2020-06-04 21:38:39 -07:00
license Add license file back, correct in package.json 2016-10-31 00:20:37 +01:00
package-lock.json 0.3.93 2022-02-15 10:13:52 -08:00
package.json 0.3.93 2022-02-15 10:13:52 -08:00
README.md Change ordering of jszip and epub.js 2022-12-16 17:36:36 -08:00
webpack.config.js WIP: Update to Webpack 4 and Babel 7 (#835) 2020-06-04 21:38:39 -07:00

Epub.js v0.3

FuturePress Views

Epub.js is a JavaScript library for rendering ePub documents in the browser, across many devices.

Epub.js provides an interface for common ebook functions (such as rendering, persistence and pagination) without the need to develop a dedicated application or plugin. Importantly, it has an incredibly permissive Free BSD license.

Try it while reading Moby Dick

Why EPUB

Why EPUB

The EPUB standard is a widely used and easily convertible format. Many books are currently in this format, and it is convertible to many other formats (such as PDF, Mobi and iBooks).

An unzipped EPUB3 is a collection of HTML5 files, CSS, images and other media just like any other website. However, it enforces a schema of book components, which allows us to render a book and its parts based on a controlled vocabulary.

More specifically, the EPUB schema standardizes the table of contents, provides a manifest that enables the caching of the entire book, and separates the storage of the content from how its displayed.

Getting Started

If using archived .epub files include JSZip (this must precede inclusion of epub.js):

<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>

Get the minified code from the build folder:

<script src="../dist/epub.min.js"></script>

Set up a element to render to:

<div id="area"></div>

Create the new ePub, and then render it to that element:

<script>
  var book = ePub("url/to/book/package.opf");
  var rendition = book.renderTo("area", {width: 600, height: 400});
  var displayed = rendition.display();
</script>

Render Methods

Default

book.renderTo("area", { method: "default", width: "100%", height: "100%" });

View example

The default manager only displays a single section at a time.

Continuous

book.renderTo("area", { method: "continuous", width: "100%", height: "100%" });

View example

The continuous manager will display as many sections as need to fill the screen, and preload the next section offscreen. This enables seamless swiping / scrolling between pages on mobile and desktop, but is less performant than the default method.

Flow Overrides

Auto (Default)

book.renderTo("area", { flow: "auto", width: "900", height: "600" });

Flow will be based on the settings in the OPF, defaults to paginated.

Paginated

book.renderTo("area", { flow: "paginated", width: "900", height: "600" });

View example

Scrolled: book.renderTo("area", { flow: "scrolled-doc" });

View example

Scripted Content

Scripted content, JavasScript the ePub HTML content, is disabled by default due to the potential for executing malicious content.

This is done by sandboxing the iframe the content is rendered into, though it is still recommended to sanitize the ePub content server-side as well.

If a trusted ePub contains interactivity, it can be enabled by passing allowScriptedContent: true to the Rendition settings.

<script>
  var rendition = book.renderTo("area", {
    width: 600,
    height: 400,
    allowScriptedContent: true
  });
</script>

This will allow the sandboxed content to run scripts, but currently makes the sandbox insecure.

Documentation

API documentation is available at epubjs.org/documentation/0.3/

A Markdown version is included in the repo at documentation/API.md

Running Locally

install node.js

Then install the project dependences with npm

npm install

You can run the reader locally with the command

npm start

Examples

View All Examples

Testing

Test can be run by Karma from NPM

npm test

Building for Distribution

Builds are concatenated and minified using webpack and babel

To generate a new build run

npm run prepare

or to continuously build run

npm run watch

Hooks

Similar to a plugins, Epub.js implements events that can be "hooked" into. Thus you can interact with and manipulate the contents of the book.

Examples of this functionality is loading videos from YouTube links before displaying a chapter's contents or implementing annotation.

Hooks require an event to register to and a can return a promise to block until they are finished.

Example hook:

rendition.hooks.content.register(function(contents, view) {

    var elements = contents.document.querySelectorAll('[video]');
    var items = Array.prototype.slice.call(elements);

    items.forEach(function(item){
      // do something with the video item
    });

})

The parts of the rendering process that can be hooked into are below.

book.spine.hooks.serialize // Section is being converted to text
book.spine.hooks.content // Section has been loaded and parsed
rendition.hooks.render // Section is rendered to the screen
rendition.hooks.content // Section contents have been loaded
rendition.hooks.unloaded // Section contents are being unloaded

Reader

The reader has moved to its own repo at: https://github.com/futurepress/epubjs-reader/

Additional Resources

Gitter Chat

Epub.js Developer Mailing List

IRC Server: freenode.net Channel: #epub.js

Follow us on twitter: @Epubjs

Other

EPUB is a registered trademark of the IDPF.