Skip to content

Commit

Permalink
Add an option to hide the Table of content
Browse files Browse the repository at this point in the history
  • Loading branch information
e-adrien committed Aug 15, 2024
1 parent d8ed7fd commit 41cdf38
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ epub.render()
Title of the table of contents. If not specified, will fallback to `Table Of Contents`.
- `appendChapterTitles`:
Automatically append the chapter title at the beginning of each contents. You can disable that by specifying `false`.
- `hideToC`:
Hide the Table of content in the generated EPUB (optional, default to `false`).
- `customOpfTemplatePath`:
Optional. For advanced customizations: absolute path to an OPF template.
- `customNcxTocTemplatePath`:
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export interface EpubOptions {
author?: Array<string> | string;
tocTitle?: string;
appendChapterTitles?: boolean;
hideToC?: boolean;
date?: string;
lang?: string;
css?: string;
Expand Down Expand Up @@ -326,6 +327,7 @@ export class EPub {
author: Array<string>;
tocTitle: string;
appendChapterTitles: boolean;
showToC: boolean;
date: string;
lang: string;
css: string | null;
Expand Down Expand Up @@ -367,6 +369,7 @@ export class EPub {
}
this.tocTitle = options.tocTitle ?? "Table Of Contents";
this.appendChapterTitles = options.appendChapterTitles ?? true;
this.showToC = options.hideToC !== true;
this.date = options.date ?? new Date().toISOString();
this.lang = options.lang ?? "en";
this.css = options.css ?? null;
Expand Down
4 changes: 2 additions & 2 deletions templates/epub2/content.opf.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<% } %>
<% }) %>
<% if (locals.cover) { %><itemref idref="cover"/><% } %>
<itemref idref="toc" />
<% if (locals.showToC) { %><itemref idref="toc" /><% } %>
<% content.forEach(function(content, index){ %>
<% if(!content.beforeToc && !content.excludeFromToc){ %>
<itemref idref="content_<%= index %>_<%= content.id %>"/>
Expand All @@ -59,6 +59,6 @@
</spine>
<guide>
<% if (locals.cover) { %><reference type="cover" title="Cover" href="cover.xhtml"/><% } %>
<reference type="text" title="Table of Content" href="toc.xhtml"/>
<% if (locals.showToC) { %><reference type="text" title="Table of Content" href="toc.xhtml"/><% } %>
</guide>
</package>
4 changes: 2 additions & 2 deletions templates/epub3/content.opf.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<% } %>
<% }) %>
<% if (locals.cover) { %><itemref idref="cover"/><% } %>
<itemref idref="toc" />
<% if (locals.showToC) { %><itemref idref="toc" /><% } %>
<% content.forEach(function(content, index){ %>
<% if(!content.beforeToc && !content.excludeFromToc){ %>
<itemref idref="content_<%= index %>_<%= content.id %>"/>
Expand All @@ -75,6 +75,6 @@
</spine>
<guide>
<% if (locals.cover) { %><reference type="cover" title="Cover" href="cover.xhtml"/><% } %>
<reference type="text" title="Table of Content" href="toc.xhtml"/>
<% if (locals.showToC) { %><reference type="text" title="Table of Content" href="toc.xhtml"/><% } %>
</guide>
</package>
2 changes: 2 additions & 0 deletions templates/toc.ncx.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
<% } %>
<% }) %>

<% if (locals.showToC) { %>
<navPoint id="toc" playOrder="<%= _index++ %>" class="chapter">
<navLabel>
<text><%= tocTitle %></text>
</navLabel>
<content src="toc.xhtml"/>
</navPoint>
<% } %>

<% content.forEach(function(content, index){ %>
<% if(!content.excludeFromToc && !content.beforeToc){ %>
Expand Down
23 changes: 23 additions & 0 deletions tests/book-no-toc-v2.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions tests/book-no-toc-v3.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/epub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ it("Ebook without cover > generate v3", async () => {
assert.strictEqual(await runTestOn("book-no-cover-v3"), true);
}).timeout(12000);

it("Ebook without ToC > generate v2", async () => {
assert.strictEqual(await runTestOn("book-no-toc-v2"), true);
}).timeout(12000);

it("Ebook without ToC > generate v3", async () => {
assert.strictEqual(await runTestOn("book-no-toc-v3"), true);
}).timeout(12000);

it("HTML Page > generate v2", async () => {
assert.strictEqual(await runTestOn("article-v2"), true);
}).timeout(12000);
Expand Down

0 comments on commit 41cdf38

Please sign in to comment.