Skip to content

Commit

Permalink
feat: simplify user api
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 26, 2023
1 parent cd7be7f commit fd500a7
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 30 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ It will support the generation of the [latest epub standard](https://www.w3.org/

> 👷‍♂️ Still work in progress.
## Installation

```bash
npm i -g epubook
```

## Resources

+ [EPUB 3.2 specification](https://www.w3.org/publishing/epub32/)
Expand Down
31 changes: 27 additions & 4 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
# epubook
# @epubook/core

A Node EPUB generation library.

It will support the generation of the [latest epub standard](https://www.w3.org/TR/epub-33/) (3.3).
The fundamental module of [epubook](https://github.com/yjl9903/epubook) for generating EPUB books.

> 👷‍♂️ Still work in progress.
## Installation

```bash
npm i @epubook/core
```

## Usage

```ts
import { Epubook } from '@epubook/core'

const book = new Epubook({
title: 'Test Book',
date: new Date('2023-02-01T11:00:00.000Z'),
lastModified: new Date('2023-02-26T11:00:00.000Z'),
creator: 'XLor',
description: 'for test usage',
source: 'imagine'
})

// ...

await book.writeFile('./test.epub')
```

## License

MIT License © 2023 [XLor](https://github.com/yjl9903)
6 changes: 3 additions & 3 deletions packages/core/src/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { toISO8601String } from '../utils';
export async function bundle(epub: Epubook): Promise<Uint8Array> {
return new Promise(async (res, rej) => {
const opfs = epub
.packageDocuments()
.packages()
.map((opf) => [opf.filename(), fflate.strToU8(makePackageDocument(opf))] as const);

const items: Record<string, Uint8Array> = {};
for (const opf of epub.packageDocuments()) {
for (const opf of epub.packages()) {
const base = path.dirname(opf.filename());
for (const item of opf.items()) {
const name = path.join(base, item.filename());
Expand Down Expand Up @@ -77,7 +77,7 @@ export function makeContainer(epub: Epubook): string {
unpairedTags: ['rootfile']
});

const rootfile = epub.packageDocuments().map((p) => ({
const rootfile = epub.packages().map((p) => ({
'@_full-path': p.filename(),
'@_media-type': 'application/oebps-package+xml',
'#text': ''
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/epub/epub.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'pathe';
import { existsSync, mkdirSync, promises as fs } from 'node:fs';

import { PackageDocument } from './opf';
import { PackageDocument, PackageDocumentMeta } from './opf';

export class Epubook {
/**
Expand All @@ -13,13 +13,15 @@ export class Epubook {
*/
private opfs: PackageDocument[] = [new PackageDocument('OEBPS/content.opf')];

constructor() {}
constructor(meta: Partial<PackageDocumentMeta> = {}) {
this.opfs[0].update(meta);
}

public packageDocuments(): PackageDocument[] {
public packages(): PackageDocument[] {
return this.opfs;
}

public mainPackageDocument() {
public main() {
return this.opfs[0];
}

Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/epub/opf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ const defu = createDefu((obj: any, key, value: any) => {
}
});

export interface PackageDocumentMeta {
title: string;
language: string;
contributor: string[];
coverage: string;
creator: string;
date: Date;
description: string;
format: string;
publisher: string;
relation: string;
rights: string;
source: string;
subject: string;
type: string;
lastModified: Date;
}

export class PackageDocument {
private readonly file: string;

Expand All @@ -20,7 +38,7 @@ export class PackageDocument {

private _identifier = randomUUID();

private _metadata = {
private _metadata: PackageDocumentMeta = {
title: '',
language: 'zh-CN',
contributor: [] as string[],
Expand Down Expand Up @@ -57,7 +75,7 @@ export class PackageDocument {
}

// --- metadata ---
public update(info: Partial<typeof this._metadata>) {
public update(info: Partial<PackageDocumentMeta>) {
// TODO: valiate input data
this._metadata = defu(info, this._metadata);
return this;
Expand Down
11 changes: 5 additions & 6 deletions packages/core/test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Bundle Epub', () => {
it('generate opf', () => {
const epub = new Epubook();

const opf = epub.mainPackageDocument();
const opf = epub.main();
opf.setIdentifier('test-book-id', 'BookId');
opf.update({
title: 'Test Book',
Expand Down Expand Up @@ -63,11 +63,7 @@ describe('Bundle Epub', () => {
});

it('write epub', async () => {
const epub = new Epubook();

const opf = epub.mainPackageDocument();
opf.setIdentifier('test-book-id', 'BookId');
opf.update({
const epub = new Epubook({
title: 'Test Book',
date: new Date('2023-02-01T11:00:00.000Z'),
lastModified: new Date('2023-02-26T11:00:00.000Z'),
Expand All @@ -76,6 +72,9 @@ describe('Bundle Epub', () => {
source: 'imagine'
});

const opf = epub.main();
opf.setIdentifier('test-book-id', 'BookId');

const content = `<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Data URL does not open in top-level context</title>
Expand Down
12 changes: 12 additions & 0 deletions packages/epubook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ It will support the generation of the [latest epub standard](https://www.w3.org/

> 👷‍♂️ Still work in progress.
## Installation

```bash
npm i -g epubook
```

## Resources

+ [EPUB 3.2 specification](https://www.w3.org/publishing/epub32/)
+ [EPUB 3.3 specification](https://www.w3.org/TR/epub-33/)
+ [EbookLib](https://github.com/aerkalov/ebooklib)

## License

MIT License © 2023 [XLor](https://github.com/yjl9903)
12 changes: 5 additions & 7 deletions packages/epubook/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { defineBuildConfig } from 'unbuild'
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
entries: [
'src/index',
],
entries: ['src/index', 'src/cli.ts'],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
},
});
emitCJS: true
}
});
3 changes: 3 additions & 0 deletions packages/epubook/epubook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import('./dist/cli.mjs');
8 changes: 7 additions & 1 deletion packages/epubook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "MIT",
"author": "XLor",
"sideEffects": false,
"type": "module",
"exports": {
".": {
"require": "./dist/index.cjs",
Expand All @@ -24,7 +25,11 @@
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"bin": {
"epubook": "epubook.mjs"
},
"files": [
"*.mjs",
"dist"
],
"scripts": {
Expand All @@ -35,7 +40,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@epubook/core": "workspace:*"
"@epubook/core": "workspace:*",
"breadc": "^0.8.8"
},
"devDependencies": {
"vitest": "^0.29.1"
Expand Down
12 changes: 12 additions & 0 deletions packages/epubook/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { breadc } from 'breadc';

import { version } from '../package.json';

const cli = breadc('epubook', {
version,
description: 'Generate EPUB books'
});

cli.command('').action(() => {});

cli.run(process.argv.slice(2)).catch((err) => console.error(err));
4 changes: 1 addition & 3 deletions packages/epubook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export function hello() {
return 'world';
}
export * from '@epubook/core';
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd500a7

Please sign in to comment.