1 Star 0 Fork 1

陶良通/nodepub3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

空文件

简介

Create EPUB 3 e-books in nodejs. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (1)

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/taolt/nodepub3.git
git@gitee.com:taolt/nodepub3.git
taolt
nodepub3
nodepub3
master

搜索帮助