Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support tocTitle and disableCover #479

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ npx docs-to-pdf --initialDocURLs="https://docusaurus.io/docs/" --contentSelector
| `--pdfMargin` | No | set margin around PDF file. Separate each margin **with comma and no space**. ex: `--pdfMargin="10,20,30,40"`. This sets margin `top: 10px, right: 20px, bottom: 30px, left: 40px` |
| `--paperFormat` | No | pdf format ex: `--paperFormat="A3"`. Please check this link for available formats [Puppeteer document](https://pptr.dev/#?product=Puppeteer&version=v5.2.1&show=api-pagepdfoptions)|
| `--disableTOC` | No | Optional toggle to show the table of contents or not |
| `--tocTitle` | No | Title for the table of contents. |
| `--disableCover` | No | Optional toggle to show the PDF cover or not. |
| `--coverTitle` | No | Title for the PDF cover. |
| `--coverImage` | No | `<src>` Image for PDF cover (does not support SVG) |
| `--coverSub` | No | Subtitle the for PDF cover. Add `<br/>` tags for multiple lines. |
Expand Down
4 changes: 3 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ export function makeProgram() {
)
.option('--pdfFormat <format>', '(DEPRECATED use paperFormat)') //TODO: Remove at next major version, replaced by paperFormat
.option('--paperFormat <format>', 'pdf format ex: A3, A4...')
.option('--disableTOC', 'disable table of contents')
.option('--tocTitle <title>', 'title for table of contents')
.option('--disableCover', 'disable PDF cover')
.option('--coverTitle <title>', 'title for PDF cover')
.option(
'--coverImage <src>',
'image for PDF cover. *.svg file not working!',
)
.option('--disableTOC', 'disable table of contents')
.option('--coverSub <subtitle>', 'subtitle for PDF cover')
.option(
'--waitForRender <timeout>',
Expand Down
13 changes: 10 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export interface GeneratePDFOptions {
excludeSelectors: Array<string>;
cssStyle: string;
puppeteerArgs: Array<string>;
disableTOC: boolean;
tocTitle: string;
disableCover: boolean;
coverTitle: string;
coverImage: string;
disableTOC: boolean;
coverSub: string;
waitForRender: number;
headerTemplate: string;
Expand All @@ -49,9 +51,11 @@ export async function generatePDF({
excludeSelectors,
cssStyle,
puppeteerArgs,
disableTOC,
tocTitle,
disableCover,
coverTitle,
coverImage,
disableTOC,
coverSub,
waitForRender,
headerTemplate,
Expand Down Expand Up @@ -152,7 +156,9 @@ export async function generatePDF({
);

// Generate Toc
const { modifiedContentHTML, tocHTML } = utils.generateToc(contentHTML);
const { modifiedContentHTML, tocHTML } = utils.generateToc(contentHTML, {
tocTitle,
});

// Restructuring the HTML of a document
console.log(chalk.cyan('Restructuring the html of a document...'));
Expand All @@ -166,6 +172,7 @@ export async function generatePDF({
tocHTML,
modifiedContentHTML,
disableTOC,
disableCover,
baseUrl,
);

Expand Down
24 changes: 17 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export function concatHtml(
cover: string,
toc: string,
content: string,
disable: boolean,
disableTOC: boolean,
disableCover: boolean,
baseUrl: string,
) {
// Clear the body content
Expand All @@ -154,10 +155,12 @@ export function concatHtml(
}

// Add the cover HTML to the body
body.innerHTML += cover;
if (!disableCover) {
body.innerHTML += cover;
}

// Add the table of contents HTML to the body if not disabled
if (!disable) {
if (!disableTOC) {
body.innerHTML += toc;
}

Expand Down Expand Up @@ -246,7 +249,13 @@ export function generateCoverHtml(
* @param maxLevel - The maximum header level to include in the TOC. Defaults to 3.
* @returns An object containing the modified content HTML and the TOC HTML.
*/
export function generateToc(contentHtml: string, maxLevel = 4) {
export function generateToc(
contentHtml: string,
options?: { maxLevel?: number; tocTitle?: string },
) {
const maxLevel = options?.maxLevel ?? 4;
const tocTitle = options?.tocTitle;

const headers: Array<{
header: string;
level: number;
Expand All @@ -271,7 +280,7 @@ export function generateToc(contentHtml: string, maxLevel = 4) {
return replaceHeader(matchedStr, headerId, maxLevel);
}

const tocHTML = generateTocHtml(headers);
const tocHTML = generateTocHtml(headers, tocTitle);

return { modifiedContentHTML, tocHTML };
}
Expand All @@ -281,7 +290,8 @@ export function generateToc(contentHtml: string, maxLevel = 4) {
* @param headers - An array of header objects containing level, id, and header properties.
* @returns The HTML code for the table of contents.
*/
export function generateTocHtml(headers: any[]) {
export function generateTocHtml(headers: any[], tocTitle?: string) {
const title = tocTitle ?? 'Table of contents:';
// Map the headers array to create a list item for each header with the appropriate indentation
const toc = headers
.map(
Expand All @@ -294,7 +304,7 @@ export function generateTocHtml(headers: any[]) {
// Return the HTML code for the table of contents
return `
<div class="toc-page" style="page-break-after: always;">
<h1 class="toc-header">Table of contents:</h1>
${title ? `<h1 class="toc-header">${title}</h1>` : ''}
${toc}
</div>
`;
Expand Down
8 changes: 6 additions & 2 deletions tests/docusaurus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ describe('generateDocusaurusPDF', () => {
excludeSelectors: [],
cssStyle: '',
puppeteerArgs: [],
disableTOC: false,
tocTitle: 'Table of contents:',
disableCover: false,
coverTitle: '',
coverImage: '',
disableTOC: false,
coverSub: '',
waitForRender: 0,
headerTemplate: '',
Expand Down Expand Up @@ -232,9 +234,11 @@ describe('generateFromBuild', () => {
excludeSelectors: [],
cssStyle: '',
puppeteerArgs: [],
disableTOC: false,
tocTitle: 'Table of contents:',
disableCover: false,
coverTitle: '',
coverImage: '',
disableTOC: false,
coverSub: '',
waitForRender: 0,
headerTemplate: '',
Expand Down
55 changes: 51 additions & 4 deletions tests/utils_jsdom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,72 @@ describe('concatHtml', () => {
const cover = '<div class="cover">Cover</div>';
const toc = '<ul><li>TOC</li></ul>';
const content = '<div class="content">Content</div>';
const disable = false;
const disableTOC = false;
const disableCover = false;
const baseUrl = '';

it('should concatenate the HTML elements correctly', () => {
const baseUrl = 'http://example.com/';
const result = concatHtml(cover, toc, content, disable, baseUrl);
const result = concatHtml(
cover,
toc,
content,
disableTOC,
disableCover,
baseUrl,
);

expect(result).toBe(
`<base href="http://example.com/"><div class="cover">Cover</div><ul><li>TOC</li></ul><div class="content">Content</div>`,
);
});

it('should not add base when no baseUrl given', () => {
const baseUrl = '';
const result = concatHtml(cover, toc, content, disable, baseUrl);
const result = concatHtml(
cover,
toc,
content,
disableTOC,
disableCover,
baseUrl,
);

expect(result).toBe(
`<div class="cover">Cover</div><ul><li>TOC</li></ul><div class="content">Content</div>`,
);
});

it('should not add toc when disableTOC given', () => {
const disableTOC = true;
const result = concatHtml(
cover,
toc,
content,
disableTOC,
disableCover,
baseUrl,
);

expect(result).toBe(
`<div class="cover">Cover</div><div class="content">Content</div>`,
);
});

it('should not add cover when disableCover given', () => {
const disableCover = true;
const result = concatHtml(
cover,
toc,
content,
disableTOC,
disableCover,
baseUrl,
);

expect(result).toBe(
`<ul><li>TOC</li></ul><div class="content">Content</div>`,
);
});
});

describe('removeElementFromSelector', () => {
Expand Down
Loading