Skip to content

Commit d64edff

Browse files
feat: add enhanceApp API
1 parent 9e7376e commit d64edff

File tree

8 files changed

+20
-4
lines changed

8 files changed

+20
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ config options:
109109
- `routePatterns` - Specify the patterns of files you want to be exported. The patterns are relative to the source directory (default `["/**", "!/404.html"]`).Patterns to match Route path using [multimatch](https://github.com/sindresorhus/multimatch)
110110
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
111111
- `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 }`)
112+
- `enhanceApp` - Enhanceapp is a function that is executed before generating PDF. It receives two parameters: the created [browser](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.browser.md) instance and the [page](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.page.md) instance
112113

113114
## PDF print style
114115

packages/vuepress-plugin-export-pdf-v2/src/configs/userConfigTypes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
1+
import type { Browser, Page as BrowserPage, PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
22
import type { Bundler, Page, Theme } from "vuepress";
33

4+
export type EnhanceApp = (browser: Browser, browserPage: BrowserPage) => void;
45
export type UserSorter = (a: Page, b: Page) => number;
56

67
/**
@@ -15,4 +16,5 @@ export interface UserConfig {
1516
pdfOptions?: PDFOptions
1617
outFile?: string
1718
outDir?: string
19+
enhanceApp?: EnhanceApp
1820
}

packages/vuepress-plugin-export-pdf-v2/src/dev-server/vuePressServer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
5757
outFile = vuepressOutFile,
5858
outDir = vuepressOutDir,
5959
routePatterns,
60+
enhanceApp,
6061
} = userConfig;
6162

6263
const devApp = createDevApp({ source: sourceDir, bundler, theme, host: "localhost", port: 8714 });
@@ -81,6 +82,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
8182
puppeteerLaunchOptions,
8283
pdfOptions,
8384
routePatterns,
85+
enhanceApp,
8486
});
8587
}
8688
catch (error) {

packages/vuepress-plugin-export-pdf-v2/src/utils/generatePdf.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { DevApp } from "vuepress";
22
import type { PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
33
import { chalk, fs, path } from "@vuepress/utils";
44
import puppeteer from "puppeteer";
5-
import type { UserSorter } from "../configs";
5+
import type { EnhanceApp, UserSorter } from "../configs";
66

77
import { filterRoute, singleProgressBar } from "../utils";
88
import { mergePDF } from "./mergePDF";
@@ -16,6 +16,7 @@ interface IGeneratePdfOptions {
1616
sorter?: UserSorter
1717
puppeteerLaunchOptions?: PuppeteerLaunchOptions
1818
pdfOptions?: PDFOptions
19+
enhanceApp?: EnhanceApp
1920
}
2021

2122
const { yellow, gray } = chalk;
@@ -35,6 +36,7 @@ export const generatePdf = async(ctx: DevApp, {
3536
puppeteerLaunchOptions,
3637
pdfOptions,
3738
routePatterns,
39+
enhanceApp,
3840
}: IGeneratePdfOptions) => {
3941
const { pages, options: { temp: tempPath } } = ctx;
4042
const tempPdfDir = join(tempPath, "pdf");
@@ -61,6 +63,7 @@ export const generatePdf = async(ctx: DevApp, {
6163

6264
for (const { location, pagePath, url, title } of normalizePages) {
6365
const browserPage = await browser.newPage();
66+
typeof enhanceApp === "function" && enhanceApp(browser, browserPage);
6467
browserPage.setDefaultNavigationTimeout(0);
6568

6669
await browserPage.goto(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ config options:
129129
- `routePatterns` - Specify the patterns of files you want to be exported. The patterns are relative to the source directory (default `["/**", "!/404.html"]`).Patterns to match Route path using [multimatch](https://github.com/sindresorhus/multimatch)
130130
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
131131
- `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 }`)
132+
- `enhanceApp` - Enhanceapp is a function that is executed before generating PDF. It receives two parameters: the created [browser](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.browser.md) instance and the [page](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.page.md) instance
132133

133134
## PDF print style
134135

packages/vuepress-plugin-export-pdf/src/configs/userConfigTypes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
1+
import type { Browser, Page as BrowserPage, PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
22
import type { Page } from "vuepress";
33

4+
export type EnhanceApp = (browser: Browser, browserPage: BrowserPage) => void;
45
export type UserSorter = (a: Page, b: Page) => number;
56

67
/**
@@ -14,4 +15,5 @@ export interface UserConfig {
1415
pdfOptions?: PDFOptions
1516
outFile?: string
1617
outDir?: string
18+
enhanceApp?: EnhanceApp
1719
}

packages/vuepress-plugin-export-pdf/src/dev-server/vuePressServer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
6262
outDir = vuepressOutDir,
6363
theme = vuepressTheme,
6464
routePatterns,
65+
enhanceApp,
6566
} = userConfig;
6667

6768
const devContext = await dev({
@@ -94,6 +95,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
9495
puppeteerLaunchOptions,
9596
pdfOptions,
9697
routePatterns,
98+
enhanceApp,
9799
});
98100
}
99101
catch (error) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Context } from "vuepress";
22
import type { PDFOptions, PuppeteerLaunchOptions } from "puppeteer";
33
import { chalk, fs, path } from "@vuepress/utils";
44
import puppeteer from "puppeteer";
5-
import type { UserSorter } from "../configs";
5+
import type { EnhanceApp, UserSorter } from "../configs";
66

77
import { filterRoute, singleProgressBar } from "../utils";
88
import { mergePDF } from "./mergePDF";
@@ -16,6 +16,7 @@ interface IGeneratePdfOptions {
1616
sorter?: UserSorter
1717
puppeteerLaunchOptions?: PuppeteerLaunchOptions
1818
pdfOptions?: PDFOptions
19+
enhanceApp?: EnhanceApp
1920
}
2021

2122
const { yellow, gray } = chalk;
@@ -35,6 +36,7 @@ export const generatePdf = async(ctx: Context, {
3536
puppeteerLaunchOptions,
3637
pdfOptions,
3738
routePatterns,
39+
enhanceApp,
3840
}: IGeneratePdfOptions) => {
3941
const { pages, tempPath } = ctx;
4042
const tempPdfDir = join(tempPath, "pdf");
@@ -61,6 +63,7 @@ export const generatePdf = async(ctx: Context, {
6163

6264
for (const { location, pagePath, url, title } of normalizePages) {
6365
const browserPage = await browser.newPage();
66+
typeof enhanceApp === "function" && enhanceApp(browser, browserPage);
6467
browserPage.setDefaultNavigationTimeout(0);
6568

6669
await browserPage.goto(

0 commit comments

Comments
 (0)