Skip to content

Commit 58bd0fe

Browse files
feat(config): add "routePatterns" config
1 parent d05707f commit 58bd0fe

File tree

15 files changed

+116
-2
lines changed

15 files changed

+116
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ config options:
8888
- `sorter` - function for changing pages order (default `undefined`)
8989
- `outFile` - name of output file (default `vuepress-YYMMDD-HHmmss.pdf`)
9090
- `outDir` - Directory of output files (default `package.json` file exists in directory)
91+
- `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)
9192
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
9293
- `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 }`)
9394

@@ -115,6 +116,20 @@ for example:
115116

116117
![print-style.png](./assets/print-style.png)
117118

119+
## Examples
120+
121+
### Don't export homepage
122+
123+
`.vuepress/vuepress-pdf.config.ts` add `routePatterns`:
124+
125+
```ts
126+
export default defineUserConfig({
127+
routePatterns: ["!/"],
128+
});
129+
```
130+
131+
> Note: `!` at the beginning of a pattern will negate the match
132+
118133
## Contributing
119134

120135
1. Fork it!

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"cac": "^6.7.12",
5858
"debug": "^4.3.4",
5959
"envinfo": "^7.8.1",
60+
"multimatch": "^6.0.0",
6061
"pdfjs": "^2.4.7",
6162
"puppeteer": "^15.4.0",
6263
"semver": "^7.3.7"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface UserConfig {
1111
bundler?: Bundler
1212
sorter?: UserSorter
1313
puppeteerLaunchOptions?: PuppeteerLaunchOptions
14+
routePatterns?: string[]
1415
pdfOptions?: PDFOptions
1516
outFile?: string
1617
outDir?: string

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
3737
if (userConfigPath)
3838
userConfig = await loadModule(userConfigPath);
3939

40+
// set default routePatterns
41+
if (Array.isArray(userConfig.routePatterns))
42+
userConfig.routePatterns = ["/**", "!/404.html", ...userConfig.routePatterns];
43+
else
44+
userConfig.routePatterns = ["/**", "!/404.html"];
45+
4046
const vuepressOutFile = commandOptions.outFile ?? `vuepress-${timeTransformer()}.pdf`;
4147
const vuepressOutDir = commandOptions.outDir ?? ".";
4248

@@ -50,6 +56,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
5056
pdfOptions,
5157
outFile = vuepressOutFile,
5258
outDir = vuepressOutDir,
59+
routePatterns,
5360
} = userConfig;
5461

5562
const devApp = createDevApp({ source: sourceDir, bundler, theme, host: "localhost", port: 8714 });
@@ -73,6 +80,7 @@ export const vuePressServer = async(dir = "docs", commandOptions: CommandOptions
7380
sorter,
7481
puppeteerLaunchOptions,
7582
pdfOptions,
83+
routePatterns,
7684
});
7785
}
7886
catch (error) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Page } from "vuepress";
2+
import multimatch from "multimatch";
3+
4+
/**
5+
* Filter route by a list of glob patterns.
6+
* @param pages - List of pages.
7+
* @param routePatterns= - List of glob patterns.
8+
* @returns - List of pages that match the glob patterns.
9+
*/
10+
export const filterRoute = (pages: Page[], routePatterns: string[]) => {
11+
const pagePaths = multimatch(pages.map(({ path }) => path), routePatterns);
12+
return pages.filter(({ path }) => pagePaths.includes(path));
13+
};

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { chalk, fs, logger, path } from "@vuepress/utils";
44
import puppeteer from "puppeteer";
55
import type { UserSorter } from "../configs";
66

7+
import { filterRoute } from "../utils";
78
import { mergePDF } from "./mergePDF";
89

910
interface IGeneratePdfOptions {
1011
port: number
1112
host: string
1213
outFile: string
1314
outDir: string
15+
routePatterns: string[]
1416
sorter?: UserSorter
1517
puppeteerLaunchOptions?: PuppeteerLaunchOptions
1618
pdfOptions?: PDFOptions
@@ -32,12 +34,13 @@ export const generatePdf = async(ctx: DevApp, {
3234
outDir,
3335
puppeteerLaunchOptions,
3436
pdfOptions,
37+
routePatterns,
3538
}: IGeneratePdfOptions) => {
3639
const { pages, options: { temp: tempPath } } = ctx;
3740
const tempPdfDir = join(tempPath, "pdf");
3841
fs.ensureDirSync(tempPdfDir);
3942

40-
let exportPages = pages.slice(0);
43+
let exportPages = filterRoute(pages, routePatterns);
4144

4245
if (typeof sorter === "function")
4346
exportPages = exportPages.sort(sorter);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./timeTransformer";
22
export * from "./wrapCommand";
33
export * from "./generatePdf";
4+
export * from "./filterRoute";
45
export * from "./checkEnv";
56
export * from "./mergePDF";

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ config options:
109109
- `sorter` - function for changing pages order (default `undefined`)
110110
- `outFile` - name of output file (default `vuepress-YYMMDD-HHmmss.pdf`)
111111
- `outDir` - Directory of output files (default `package.json` file exists in directory)
112+
- `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)
112113
- `puppeteerLaunchOptions` - [Puppeteer launch options object](https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.puppeteerlaunchoptions.md)
113114
- `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 }`)
114115

@@ -136,6 +137,20 @@ for example:
136137

137138
![print-style.png](./assets/print-style.png)
138139

140+
## Examples
141+
142+
### Don't export homepage
143+
144+
`.vuepress/vuepress-pdf.config.ts` add `routePatterns`:
145+
146+
```ts
147+
export default defineUserConfig({
148+
routePatterns: ["!/"],
149+
});
150+
```
151+
152+
> Note: `!` at the beginning of a pattern will negate the match
153+
139154
## Contributing
140155

141156
1. Fork it!

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"cac": "^6.7.12",
5757
"debug": "^4.3.4",
5858
"envinfo": "^7.8.1",
59+
"multimatch": "^6.0.0",
5960
"pdfjs": "^2.4.7",
6061
"puppeteer": "^15.4.0",
6162
"semver": "^7.3.7"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface UserConfig {
1010
theme?: string
1111
sorter?: UserSorter
1212
puppeteerLaunchOptions?: PuppeteerLaunchOptions
13+
routePatterns?: string[]
1314
pdfOptions?: PDFOptions
1415
outFile?: string
1516
outDir?: string

0 commit comments

Comments
 (0)