Skip to content

Commit

Permalink
feat: generate manifest and spine
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 26, 2023
1 parent b813e8b commit 6cde61c
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 126 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/bundle/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const MIMETYPE = 'application/epub+zip';

export const ImageGif = 'image/gif';
export const ImageJpeg = 'image/jpeg';
export const ImagePng = 'image/png';
export const ImageSvg = 'image/svg+xml';
export const ImageWebp = 'image/webp';

export type ImageMediaType = typeof ImageGif | typeof ImageJpeg | typeof ImagePng;

export const TextCSS = 'text/css';

export const XHTML = 'application/xhtml+xml';

export type MediaType = ImageMediaType | typeof TextCSS | typeof XHTML;
34 changes: 29 additions & 5 deletions packages/core/src/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as fflate from 'fflate';
import { XMLBuilder } from 'fast-xml-parser';

import type { Epubook, PackageDocument } from '../epub';
import type { Epubook, ManifestItem, ManifestItemRef, PackageDocument } from '../epub';

import { BundleError } from '../error';

const MIMETYPE = 'application/epub+zip';
import { MIMETYPE } from './constant';

export * from './constant';

/**
* Bundle epub to zip archive
Expand Down Expand Up @@ -96,7 +99,7 @@ export function makePackageDocument(opf: PackageDocument): string {
format: true,
ignoreAttributes: false,
suppressUnpairedNode: false,
unpairedTags: ['rootfile']
unpairedTags: ['item', 'itemref']
});

const optionalMetadata: Record<string, any> = {};
Expand Down Expand Up @@ -143,14 +146,35 @@ export function makePackageDocument(opf: PackageDocument): string {
]
};

function makeManifestItem(item: ManifestItem) {
return {
'@_fallback': item.fallback(),
'@_href': item.href(),
'@_id': item.id(),
'@_media-overlay': item.mediaOverlay(),
'@_media-type': item.mediaType(),
'@_properties': item.properties()
};
}

function makeManifestItemRef(item: ManifestItemRef) {
return {
'@_idref': item.idref()
};
}

return builder.build({
'?xml': { '#text': '', '@_version': '1.0', '@_encoding': 'UTF-8' },
package: {
'@_unique-identifier': opf.uniqueIdentifier(),
'@_version': opf.version(),
metadata,
manifest: {},
spine: {}
manifest: {
item: opf.manifest().map((i) => makeManifestItem(i))
},
spine: {
itemref: opf.spine().map((ir) => makeManifestItemRef(ir))
}
}
});
}
119 changes: 0 additions & 119 deletions packages/core/src/epub.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/core/src/epub/epub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type PathLike, promises as fs } from 'node:fs';

import { PackageDocument } from './opf';

export class Epubook {
/**
* See: https://www.w3.org/TR/epub-33/#sec-package-doc
*
* Now, it only supports single opf (OEBPS/content.opf)
*
* @returns list of package documents
*/
private opfs: PackageDocument[] = [new PackageDocument('OEBPS/content.opf')];

constructor() {}

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

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

async bundle() {
const { bundle } = await import('../bundle');
return await bundle(this);
}

async writeFile(file: PathLike) {
const buffer = await this.bundle();
await fs.writeFile(file, buffer);
}
}
3 changes: 3 additions & 0 deletions packages/core/src/epub/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './epub';

export * from './opf';
Loading

0 comments on commit 6cde61c

Please sign in to comment.