npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodepub3

v1.3.1

Published

Create EPUB 3 e-books in nodejs.

Downloads

13

Readme

Nodepub3

Nodepub3 is used to create EPUB 3 e-books in nodejs that reference to kcartlidge/nodepub and mohannadize/nodepub-lite.

About Nodepub3

Nodepub3 is a Node module which can be used to create EPUB 3 documents.

  • Files open fine in Calibre
  • PNG/JPG cover images - recommend 600x800, 600x900, or similar as minimum
  • Custom CSS style can be provided
  • Inline any resource within the EPUB
  • Exclude cover and table of contents from auto contents page
  • Support nested table of contents hierarchies
  • Support for Most Used Right To Left Languages with <body dir="rtl">

Installation

npm i nodepub3
# or use pnpm
pnpm i nodepub3

Then import it for use:

import Nodepub3 from 'nodepub3'

Defining your EPUB

  • Documents consist of metadata and sections
    • Metadata is an object with various properties detailing the book
    • Chapters are chunks of HTML

Here's some sample metadata:

import fs from 'fs-extra'
import Nodepub3, { Metadata } from 'nodepub3'

const metadata: Metadata = {
  id: 'isbn:278-123456789',
  cover: {
    name: 'cover.png',
    // base64 encoded resource, or Buffer
    data: fs.readFileSync('cover.png')
  },
  title: 'Unnamed Document',
  creator: {
    value: 'Anonymous',
    meta: {
      'file-as': 'Anonymous'
    }
  },
  subject: ['Sample', 'Example', 'Test'],
  copyright: 'Anonymous, 1980',
  publisher: 'My Fake Publisher',
  publicationDate: new Date('2000-12-31'),
  language: 'en',
  description: 'A test book.',
  source: 'http://www.kcartlidge.com'
}

Call the constructor method with the aforementioned metadata object detailing your book.

const epub = new Nodepub3(metadata)

Fill in the content

The bulk of the work is adding your content.

Call addChapter on your new document with a title and the HTML/TEXT contents for each chapter in turn.

epub.addChapter(title, content[, isNormalText, depth])

For example:

epub.addChapters(
  {
    title: 'Copyright',
    content: '<p>...</p>\n<p>...</p>'
  },
  {
    title: 'Chapter 1',
    content: '...\n...',
    // content will convert to "<p>...</p>\n<p>...</p>"
    isNormalText: true
  }
)

Support nested table of contents hierarchies.

/**
 * Table of Contens:
 * 1
 * 2
 *   2.1
 *   2.2
 *     2.2.1
 * 3
 */
epub.addChapters(
  { title: '1', content: '' },
  { title: '2', content: '' },
  { title: '2.1', content: '', depth: 1 },
  { title: '2.2', content: '', depth: 1 },
  { title: '2.2.1', content: '', depth: 2 },
  { title: '3', content: '' }
)

Optionally add some extra CSS style

You can inject basic CSS style into your book.

epub.addStyle('p { text-indent: 2em; }')

Inside any resource such as image、audio、video or font

For example:

epub.addResource('f1.png', fs.readFileSync('image.png'))

epub.addResources(
  {
    name: 'f2.mp3',
    data: fs.readFileSync('audio.mp3')
  },
  {
    name: 'f3.mp4',
    data: fs.readFileSync('video.mp4')
  },
  {
    name: 'f4.ttf',
    data: fs.readFileSync('font.ttf')
  }
)

use resource in chapter case:

<img src="../res/f1.png" />
<audio src="../res/f2.mp3" controls="controls" />
<video src="../res/f3.mp4" controls="controls" />

use resource in CSS style case:

@font-face {
  font-family: 'f4';
  src: url('res/f4.ttf');
}

Generating your EPUB

Note that Nodepub3 is asynchronous, actionable using async/await.

// This will generate an epub file `example.epub`
;(async () => {
  try {
    await epub.create('example.epub')
  } catch (e) {
    console.error(e)
  }
})()