Skip to content

Commit

Permalink
feat: set spine
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2023
1 parent 38b1d0d commit ca7a864
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/epub/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export class Epub {
return this;
}

public spine(...items: Item[]) {
this.opfs[0].setSpine(items);
return this;
}

async bundle() {
const { bundle } = await import('../bundle');
return await bundle(this);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/epub/opf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class PackageDocument {
// --- manifest ---
public addItem(item: Item) {
this._items.push(item);
return this;
}

public items() {
Expand All @@ -115,6 +116,11 @@ export class PackageDocument {
return this.items().map((i) => i.manifest());
}

public setSpine(items: Item[]) {
this._spine.splice(0, this._spine.length, ...items.map((i) => i.itemref()));
return this;
}

public spine() {
return this._spine;
}
Expand Down
23 changes: 22 additions & 1 deletion packages/core/test/__snapshots__/bundle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,28 @@ PackageDocument {
"title": "Test Book",
"type": "",
},
"_spine": [],
"_spine": [
ManifestItemRef {
"_idref": "page1",
"optional": {},
},
ManifestItemRef {
"_idref": "page2",
"optional": {},
},
ManifestItemRef {
"_idref": "page3",
"optional": {},
},
ManifestItemRef {
"_idref": "page4",
"optional": {},
},
ManifestItemRef {
"_idref": "page5",
"optional": {},
},
],
"_toc": Html {
"_properties": "nav",
"content": "<html xmlns=\\"http://www.w3.org/1999/xhtml\\" xmlns:epub=\\"http://www.idpf.org/2007/ops\\" lang=\\"zh-CN\\" xml:lang=\\"zh-CN\\">
Expand Down
10 changes: 6 additions & 4 deletions packages/core/test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ describe('Bundle Epub', () => {
});

const cover = new Html('cover.xhtml', '');
opf.addItem(cover);
opf.spine().push(cover.manifest().ref());
epub.addItem(cover).spine(cover);

const res = makePackageDocument(opf);
expect(res).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -93,8 +92,10 @@ describe('Bundle Epub', () => {
</body>
</html>`;
const cover = new Html('cover.xhtml', content);
epub.addItem(cover).toc([{ text: 'cover', item: cover }]);
epub.main().spine().push(cover.itemref());
epub
.addItem(cover)
.spine(cover)
.toc([{ text: 'cover', item: cover }]);

await epub.writeFile('.output/test.epub');
});
Expand All @@ -118,6 +119,7 @@ describe('Bundle Epub', () => {

await epub
.addItem(item1, item2, item3, item4, item5)
.spine(item1, item2, item3, item4, item5)
.toc(
[
{ text: '1', item: item1 },
Expand Down

0 comments on commit ca7a864

Please sign in to comment.