Skip to content

Commit b4c7ffc

Browse files
feat: support specify an outline container selector
1 parent 05fb40b commit b4c7ffc

File tree

12 files changed

+179
-161
lines changed

12 files changed

+179
-161
lines changed

packages/vuepress-plugin-export-pdf-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"cac": "^6.7.14",
5656
"envinfo": "^7.8.1",
5757
"fs-extra": "^11.1.1",
58-
"html-export-pdf-cli": "^1.0.3",
58+
"html-export-pdf-cli": "^1.1.0",
5959
"multimatch": "^6.0.0",
6060
"ora": "^6.3.0",
6161
"pdfjs": "^2.4.7",

packages/vuepress-plugin-export-pdf-core/src/generatePdf.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ export interface IGeneratePdfOptions {
2323
pdfOptions?: PDFOptions
2424
pdfOutlines?: boolean
2525
urlOrigin?: string
26+
outlineContainerSelector?: string
2627
}
2728

2829
/**
29-
* Generate PDF from VuePress dev server.
30+
* Generate PDF from VuePress or VitePress dev server.
3031
* @param param1 - IGeneratePdfOptions
3132
*/
3233
export const generatePdf = async ({
@@ -42,7 +43,8 @@ export const generatePdf = async ({
4243
pdfOutlines,
4344
routePatterns,
4445
puppeteerLaunchOptions,
45-
// eslint-disable-next-line sonarjs/cognitive-complexity
46+
outlineContainerSelector,
47+
// eslint-disable-next-line sonarjs/cognitive-complexity
4648
}: IGeneratePdfOptions) => {
4749
const tempPdfDir = join(tempDir, "pdf");
4850
fse.ensureDirSync(tempPdfDir);
@@ -70,7 +72,7 @@ export const generatePdf = async ({
7072
const singleBar = createProgress();
7173
singleBar.start(normalizePages.length);
7274

73-
const printer = new Printer();
75+
const printer = new Printer({ outlineContainerSelector });
7476
await printer.setup(puppeteerLaunchOptions);
7577

7678
for (const { location, pagePath, title } of normalizePages) {
@@ -114,7 +116,8 @@ export const generatePdf = async ({
114116
});
115117
await writeFileSafe(pagePath, data);
116118

117-
singleBar.increment(1, { headTitle: title });
119+
const headTitle = title || await page.title();
120+
singleBar.increment(1, { headTitle });
118121
}
119122

120123
singleBar.stop();

packages/vuepress-plugin-export-pdf-core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface CommandOptions {
88
}
99

1010
export interface Page {
11-
title: string
11+
title?: string
1212
key: string
1313
path: string
1414
}

packages/vuepress-plugin-export-pdf-core/src/utils/checkEnv.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
import semver from "semver";
66

77
/**
8-
* Check if Node version meets VuePress requirement and Vuepress meets plugin VuePress.
8+
* Ensure that current Node version matches App's Node version and App's version matches peerVersion
9+
*
10+
* @param {string} appName - App's name.
11+
* @param {string} nodeVersion - The minimum required version of Node.
12+
* @param {string} appVersion - The version number of App.
13+
* @param {string} peerVersion - The required version number of the plugin.
14+
* @returns {void}
15+
* @throws {Error} If the environment does not meet the minimum requirements.
916
*/
1017

11-
export function checkEnv(nodeVersion: string, appVersion: string, peerVersion: string) {
18+
export function checkEnv(appName = "VuePress", nodeVersion: string, appVersion: string, peerVersion: string) {
1219
if (!semver.satisfies(process.version, nodeVersion, { includePrerelease: true })) {
1320
console.error(
14-
"\n[vuepress] minimum Node version not met:"
15-
+ `\nYou are using Node ${process.version}, but VuePress `
21+
`\n[${appName}] minimum Node version not met:`
22+
+ `\nYou are using Node ${process.version}, but ${appName} `
1623
+ `requires Node ${nodeVersion}.\nPlease upgrade your Node version.\n`,
1724
);
1825
process.exit(1);
1926
}
2027

2128
if (!semver.satisfies(appVersion, peerVersion, { includePrerelease: true })) {
2229
console.error(
23-
"\n[vuepress] VuePress version not met:"
24-
+ `\nYou are using VuePress ${appVersion}, but plugin `
30+
`\n[${appName}] version not met:`
31+
+ `\nYou are using ${appName} ${appVersion}, but plugin `
2532
+ `requires ${peerVersion}.\nPlease check it.\n`,
2633
);
2734
process.exit(1);

packages/vuepress-plugin-export-pdf-core/src/utils/mergePDF.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface IPage {
77
location: string
88
pagePath: string
99
url: string
10-
title: string
10+
title?: string
1111
}
1212

1313
/**

packages/vuepress-plugin-export-pdf-v1/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ config options:
133133
- `pdfOptions` - [Valid options to configure PDF generation via Page.pdf()](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.pdfoptions.md) (default `{ format: 'A4 }`)
134134
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)
135135
- `urlOrigin`: Change the origin of the print url([How do I change the URL point to the localhost](https://github.com/condorheroblog/vuepress-plugin-export-pdf/issues/5))
136+
- `outlineContainerSelector`: Specify an outline container selector.
137+
136138
## PDF print style
137139

138140
By default, `A4` paper is used for printing, The size of A4 paper is (8.27in x 11.7in), One inch is equal to ninety-six pixels: `1 in = 96 pixel (X)` ,the inch unit of A4 is converted to (793.92px x 1123.2px).

packages/vuepress-plugin-export-pdf-v1/src/serverApp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
5656
outFile = vuepressOutFile,
5757
urlOrigin = commandOptions.urlOrigin,
5858
pdfOutlines = commandOptions.pdfOutlines,
59+
outlineContainerSelector,
5960
} = userConfig;
6061

6162
const devContext = await dev({
@@ -93,6 +94,7 @@ export const serverApp = async (dir = "docs", commandOptions: ICommandOptions =
9394
pdfOutlines,
9495
routePatterns,
9596
puppeteerLaunchOptions,
97+
outlineContainerSelector,
9698
});
9799
}
98100
catch (error) {

packages/vuepress-plugin-export-pdf-v1/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export interface UserConfig {
1515
outDir?: string
1616
pdfOutlines?: boolean
1717
urlOrigin?: string
18+
outlineContainerSelector?: string
1819
}

packages/vuepress-plugin-export-pdf-v2/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ config options:
113113
- `pdfOptions` - [Valid options to configure PDF generation via Page.pdf()](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.pdfoptions.md) (default `{ format: 'A4 }`)
114114
- `pdfOutlines` - Keep PDF outlines/bookmarks(default `true`)
115115
- `urlOrigin`: Change the origin of the print url([How do I change the URL point to the localhost](https://github.com/condorheroblog/vuepress-plugin-export-pdf/issues/5))
116+
- `outlineContainerSelector`: Specify an outline container selector.
117+
116118
## PDF print style
117119

118120
By default, `A4` paper is used for printing, The size of A4 paper is (8.27in x 11.7in), One inch is equal to ninety-six pixels: `1 in = 96 pixel (X)` ,the inch unit of A4 is converted to (793.92px x 1123.2px).

packages/vuepress-plugin-export-pdf-v2/src/serverApp.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
5050
outDir = vuepressOutDir,
5151
urlOrigin = commandOptions.urlOrigin,
5252
pdfOutlines = commandOptions.pdfOutlines,
53+
outlineContainerSelector,
5354
} = userConfig;
5455

5556
const devApp = createDevApp({
@@ -85,6 +86,7 @@ export const serverApp = async (dir = "docs", commandOptions: CommandOptions = {
8586
pdfOutlines,
8687
routePatterns,
8788
puppeteerLaunchOptions,
89+
outlineContainerSelector,
8890
});
8991
}
9092
catch (error) {

0 commit comments

Comments
 (0)