epub

repository·master·Indexed 18 days ago

https://github.com/julien-c/epub

A pure-JS module and CLI for parsing EPUB electronic book files with Node.JS. It enables developers to extract metadata, navigate the table of contents, and retrieve chapter text, images, and other internal files.

Tokens
939
Snippets
9
Records
10
Agent score
13%

What's inside epub

  1. Initialize and parse an EPUB file

    master

    To use the library, instantiate the EPub class with a file path and call await epub.parse() before attempting to access any content.

    Constructor Options:

    • pathToFile: The file path to the EPUB file.
    • imageWebRoot (Optional): Prefix for image URLs. Defaults to /images/. If set to /images/, an internal URL like /images/IMG_ID/IMG_FILENAME can be used with getImage by using the IMG_ID.
    • chapterWebRoot (Optional): Prefix for chapter URLs. Defaults to /links/.
    import EPub from "epub";
    const epub = new EPub(pathToFile, {
      imageWebRoot: '/images/',
      chapterWebRoot: '/links/'
    });
    
    await epub.parse();
  2. Access EPUB metadata

    master

    The metadata property contains information about the book. After calling await epub.parse(), you can access the following fields:

    • creator: The first author listed.
    • creatorFileAs: The author name as formatted on the file.
    • title: The title of the book.
    • language: Language code (e.g., en or en-us).
    • subject: The topic of the book.
    • date: The creation date of the file.
    • description: A description of the book.
    console.log(epub.metadata.title);
  3. Access the Table of Contents (TOC)

    master
    The toc property provides a list of titles and URLs for the Table of Contents. To find the actual chapter and its corresponding ID, you must inspect the href property of the TOC entries.
  4. Retrieve arbitrary files with getFile()

    master

    Load any file from the ebook (such as CSS files) as a Buffer using its file_id.

    const { data, mimeType } = await epub.getFile("css1");
  5. Iterate through the book flow and chapters

    master

    The flow property contains the ordered list of chapters. You can iterate through this list to find chapter IDs, which are required to load chapter content via getChapter.

    for (const chapter of epub.flow) {
    	console.log(chapter.id);
    }
  6. Retrieve images with getImage()

    master

    Load an image from the ebook as a Buffer. This requires an image_id.

    const { data, mimeType } = await epub.getImage("image1");
  7. Get chapter content with getChapter() and getChapterRaw()

    master

    Use these methods to retrieve chapter content using a chapter_id obtained from the flow or toc properties.

    • getChapter(chapter_id): Returns the chapter text.
    • getChapterRaw(chapter_id): Returns the raw chapter text.
    const text = await epub.getChapter("chapter1");
  8. Read an EPUB file from the terminal using the CLI

    master

    You can read an EPUB file directly from your terminal using npx or pnpx by passing the URL or file path as an argument.

    URL="https://github.com/progit/progit2/releases/download/2.1.449/progit.epub"
    
    npx epub "$URL"
    # or
    pnpx epub "$URL"
  9. Use the epub CLI to read an EPUB file or URL

    master

    The epub CLI allows you to inspect the contents of an EPUB file or a remote EPUB URL directly from your terminal. It parses the file and outputs the metadata, a hierarchical table of contents, and the text content of all chapters (with HTML tags stripped).

    Usage:

    epub <file.epub | url>

    Supported Inputs:

    • A local path to an .epub file.
    • A URL starting with http:// or https://.
    # Read a local file
    epub ./my-book.epub
    
    # Read from a URL
    epub https://example.com/book.epub