@alauda/doom 0.12.2 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cli/export-pdf-core/generatePdf.d.ts +19 -0
- package/lib/cli/export-pdf-core/generatePdf.js +81 -0
- package/lib/cli/export-pdf-core/index.d.ts +3 -0
- package/lib/cli/export-pdf-core/index.js +3 -0
- package/lib/cli/export-pdf-core/types.d.ts +15 -0
- package/lib/cli/export-pdf-core/types.js +1 -0
- package/lib/cli/export-pdf-core/utils/convertPathToPosix.d.ts +8 -0
- package/lib/cli/export-pdf-core/utils/convertPathToPosix.js +26 -0
- package/lib/cli/export-pdf-core/utils/filterRoute.d.ts +8 -0
- package/lib/cli/export-pdf-core/utils/filterRoute.js +11 -0
- package/lib/cli/export-pdf-core/utils/getUrlLink.d.ts +4 -0
- package/lib/cli/export-pdf-core/utils/getUrlLink.js +7 -0
- package/lib/cli/export-pdf-core/utils/index.d.ts +3 -0
- package/lib/cli/export-pdf-core/utils/index.js +3 -0
- package/lib/cli/export-pdf-core/utils/mergePDF.d.ts +27 -0
- package/lib/cli/export-pdf-core/utils/mergePDF.js +140 -0
- package/lib/cli/export-pdf-core/utils/timeTransformer.d.ts +8 -0
- package/lib/cli/export-pdf-core/utils/timeTransformer.js +22 -0
- package/lib/cli/export.d.ts +0 -2
- package/lib/cli/export.js +87 -19
- package/lib/cli/html-export-pdf/core/index.d.ts +3 -0
- package/lib/cli/html-export-pdf/core/index.js +3 -0
- package/lib/cli/html-export-pdf/core/outline.d.ts +47 -0
- package/lib/cli/html-export-pdf/core/outline.js +216 -0
- package/lib/cli/html-export-pdf/core/postprocesser.d.ts +14 -0
- package/lib/cli/html-export-pdf/core/postprocesser.js +38 -0
- package/lib/cli/html-export-pdf/core/printer.d.ts +61 -0
- package/lib/cli/html-export-pdf/core/printer.js +235 -0
- package/lib/cli/html-export-pdf/index.d.ts +2 -0
- package/lib/cli/html-export-pdf/index.js +2 -0
- package/lib/cli/html-export-pdf/utils/cliProgress.d.ts +13 -0
- package/lib/cli/html-export-pdf/utils/cliProgress.js +80 -0
- package/lib/cli/html-export-pdf/utils/fs.d.ts +5 -0
- package/lib/cli/html-export-pdf/utils/fs.js +18 -0
- package/lib/cli/html-export-pdf/utils/getAbsFileName.d.ts +1 -0
- package/lib/cli/html-export-pdf/utils/getAbsFileName.js +4 -0
- package/lib/cli/html-export-pdf/utils/getDirname.d.ts +1 -0
- package/lib/cli/html-export-pdf/utils/getDirname.js +5 -0
- package/lib/cli/html-export-pdf/utils/index.d.ts +7 -0
- package/lib/cli/html-export-pdf/utils/index.js +7 -0
- package/lib/cli/html-export-pdf/utils/isValidUrl.d.ts +1 -0
- package/lib/cli/html-export-pdf/utils/isValidUrl.js +3 -0
- package/lib/cli/html-export-pdf/utils/optionsCustomProcessing.d.ts +2 -0
- package/lib/cli/html-export-pdf/utils/optionsCustomProcessing.js +6 -0
- package/lib/cli/html-export-pdf/utils/replaceExt.d.ts +1 -0
- package/lib/cli/html-export-pdf/utils/replaceExt.js +19 -0
- package/lib/cli/index.js +8 -12
- package/lib/cli/load-config.js +2 -5
- package/lib/cli/merge-pdfs/formatDate.d.ts +1 -0
- package/lib/cli/merge-pdfs/formatDate.js +23 -0
- package/lib/cli/merge-pdfs/index.d.ts +8 -0
- package/lib/cli/merge-pdfs/index.js +61 -0
- package/lib/global/PrintStyle/index.d.ts +2 -0
- package/lib/global/PrintStyle/index.js +13 -0
- package/lib/plugins/auto-sidebar/index.d.ts +3 -2
- package/lib/plugins/auto-sidebar/index.js +33 -30
- package/lib/plugins/index.d.ts +3 -0
- package/lib/plugins/index.js +3 -0
- package/lib/runtime/components/ExternalSiteLink.js +9 -6
- package/lib/runtime/components/K8sCrd.js +4 -3
- package/lib/runtime/hooks/index.d.ts +1 -0
- package/lib/runtime/hooks/index.js +1 -0
- package/lib/runtime/hooks/useIsPrint.d.ts +3 -0
- package/lib/runtime/hooks/useIsPrint.js +31 -0
- package/lib/runtime/hooks/useTranslation.d.ts +1 -1
- package/lib/utils/helpers.d.ts +2 -0
- package/lib/utils/helpers.js +4 -0
- package/lib/utils/types.d.ts +3 -1
- package/package.json +12 -1
- package/pyodide/README.md +90 -0
- package/pyodide/console.html +259 -0
- package/pyodide/ffi.d.ts +1118 -0
- package/pyodide/micropip-0.8.0-py3-none-any.whl +0 -0
- package/pyodide/package.json +137 -0
- package/pyodide/packaging-24.2-py3-none-any.whl +0 -0
- package/pyodide/pyodide-lock.json +1 -0
- package/pyodide/pyodide.asm.js +17 -0
- package/pyodide/pyodide.asm.wasm +0 -0
- package/pyodide/pyodide.d.ts +1627 -0
- package/pyodide/pyodide.js +6 -0
- package/pyodide/pyodide.js.map +1 -0
- package/pyodide/pyodide.mjs +4 -0
- package/pyodide/pyodide.mjs.map +1 -0
- package/pyodide/pypdf-3.8.1-py3-none-any.whl +0 -0
- package/pyodide/python_stdlib.zip +0 -0
- package/pyodide/repodata.json +1 -0
- package/styles/global.scss +30 -0
- package/styles/shiki.css +3 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type PrinterOptions } from '../html-export-pdf/index.js';
|
|
2
|
+
import type { LaunchOptions, Page, PDFOptions } from './types.js';
|
|
3
|
+
export interface GeneratePdfOptions {
|
|
4
|
+
pages: Page[];
|
|
5
|
+
tempDir: string;
|
|
6
|
+
port: number;
|
|
7
|
+
host: string;
|
|
8
|
+
outFile: string;
|
|
9
|
+
outDir: string;
|
|
10
|
+
launchOptions?: LaunchOptions;
|
|
11
|
+
pdfOptions?: PDFOptions;
|
|
12
|
+
pdfOutlines?: boolean;
|
|
13
|
+
urlOrigin?: string;
|
|
14
|
+
printerOptions?: PrinterOptions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate PDF from VuePress or VitePress dev server.
|
|
18
|
+
*/
|
|
19
|
+
export declare function generatePdf({ pages, tempDir, port, host, outFile, outDir, urlOrigin, pdfOptions, pdfOutlines, launchOptions, printerOptions, }: GeneratePdfOptions): Promise<string>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import fse from 'fs-extra';
|
|
4
|
+
import { red, yellow } from 'yoctocolors';
|
|
5
|
+
import { createProgress, isValidUrl, Printer, writeFileSafe, } from '../html-export-pdf/index.js';
|
|
6
|
+
import { getUrlLink, mergePDF, } from './utils/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Generate PDF from VuePress or VitePress dev server.
|
|
9
|
+
*/
|
|
10
|
+
export async function generatePdf({ pages, tempDir, port, host, outFile, outDir, urlOrigin, pdfOptions, pdfOutlines = true, launchOptions, printerOptions, }) {
|
|
11
|
+
fse.ensureDirSync(tempDir);
|
|
12
|
+
const isValidUrlOrigin = isValidUrl(urlOrigin ?? '');
|
|
13
|
+
if (urlOrigin && !isValidUrlOrigin) {
|
|
14
|
+
process.stdout.write(red(`${urlOrigin} is not a valid URL`));
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
let userURLOrigin = '';
|
|
18
|
+
if (urlOrigin && isValidUrlOrigin) {
|
|
19
|
+
userURLOrigin = new URL(urlOrigin).origin;
|
|
20
|
+
}
|
|
21
|
+
const localURLOrigin = `${host}:${port}`;
|
|
22
|
+
const normalizePages = pages.map((page) => {
|
|
23
|
+
return {
|
|
24
|
+
url: page.path,
|
|
25
|
+
title: page.title,
|
|
26
|
+
location: urlOrigin
|
|
27
|
+
? `${userURLOrigin}${page.path}`
|
|
28
|
+
: `http://${localURLOrigin}${page.path}`,
|
|
29
|
+
pagePath: path.resolve(tempDir, `${page.key}.pdf`),
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
const singleBar = createProgress();
|
|
33
|
+
singleBar.start(normalizePages.length);
|
|
34
|
+
const printer = new Printer(printerOptions);
|
|
35
|
+
const page = await printer.setup(launchOptions);
|
|
36
|
+
if (urlOrigin && isValidUrlOrigin) {
|
|
37
|
+
await page.route('**/*', (route) => {
|
|
38
|
+
const reqUrl = route.request().url();
|
|
39
|
+
if (!isValidUrl(reqUrl)) {
|
|
40
|
+
return route.continue();
|
|
41
|
+
}
|
|
42
|
+
// http or https
|
|
43
|
+
const parsedUrl = new URL(reqUrl);
|
|
44
|
+
if (userURLOrigin === parsedUrl.origin) {
|
|
45
|
+
parsedUrl.host = host;
|
|
46
|
+
parsedUrl.protocol = 'http:';
|
|
47
|
+
parsedUrl.port = `${port}`;
|
|
48
|
+
const parsedUrlString = parsedUrl.toString();
|
|
49
|
+
return route.continue({
|
|
50
|
+
url: parsedUrlString,
|
|
51
|
+
headers: Object.assign({}, route.request().headers(), {
|
|
52
|
+
refer: parsedUrlString,
|
|
53
|
+
// Same origin
|
|
54
|
+
// origin: parsedUrl.origin,
|
|
55
|
+
// CORS
|
|
56
|
+
// host: parsedUrl.host,
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const allOutlines = [];
|
|
63
|
+
for (const { location, pagePath, title } of normalizePages) {
|
|
64
|
+
const { data, outlineNodes } = await printer.pdf(location, {
|
|
65
|
+
format: 'A4',
|
|
66
|
+
...pdfOptions,
|
|
67
|
+
}, pdfOutlines);
|
|
68
|
+
if (pdfOutlines) {
|
|
69
|
+
allOutlines.push([getUrlLink(location).link, outlineNodes]);
|
|
70
|
+
}
|
|
71
|
+
await writeFileSafe(pagePath, data);
|
|
72
|
+
singleBar.increment(1, { headTitle: title || (await page.title()) });
|
|
73
|
+
}
|
|
74
|
+
singleBar.stop();
|
|
75
|
+
await printer.closeBrowser();
|
|
76
|
+
const exportedPath = await mergePDF(normalizePages, outFile, outDir, allOutlines);
|
|
77
|
+
const message = `\nExported to ${yellow(exportedPath)}\n`;
|
|
78
|
+
process.stdout.write(message);
|
|
79
|
+
fse.removeSync(tempDir);
|
|
80
|
+
return exportedPath;
|
|
81
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CommandOptions {
|
|
2
|
+
config?: string;
|
|
3
|
+
outDir?: string;
|
|
4
|
+
outFile?: string;
|
|
5
|
+
debug?: boolean;
|
|
6
|
+
urlOrigin?: string;
|
|
7
|
+
pdfOutlines?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface Page {
|
|
10
|
+
title?: string;
|
|
11
|
+
key: string;
|
|
12
|
+
path: string;
|
|
13
|
+
}
|
|
14
|
+
export type { Browser, PDFOptions } from '../html-export-pdf/index.js';
|
|
15
|
+
export type { LaunchOptions } from 'playwright';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a file path to a POSIX-style path.
|
|
3
|
+
* On Windows, it replaces DOS device paths and converts backslashes to forward slashes.
|
|
4
|
+
* @param path - The file path to convert.
|
|
5
|
+
* @returns The converted POSIX-style path.
|
|
6
|
+
* @see https://github.com/mrmlnc/fast-glob/blob/28a3d61e44d5d9193ba97de4f21df6dc7725f7c0/src/utils/path.ts
|
|
7
|
+
*/
|
|
8
|
+
export declare function convertPathToPosix(path: string): string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { platform } from 'node:os';
|
|
2
|
+
/**
|
|
3
|
+
* The device path (\\.\ or \\?\).
|
|
4
|
+
* https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
|
|
5
|
+
*/
|
|
6
|
+
const DOS_DEVICE_PATH_RE = /^\\\\(?<path>[.?])/;
|
|
7
|
+
/**
|
|
8
|
+
* All backslashes except those escaping special characters.
|
|
9
|
+
* Windows: !()+@{}
|
|
10
|
+
* https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
|
|
11
|
+
*/
|
|
12
|
+
const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@{}])/g;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a file path to a POSIX-style path.
|
|
15
|
+
* On Windows, it replaces DOS device paths and converts backslashes to forward slashes.
|
|
16
|
+
* @param path - The file path to convert.
|
|
17
|
+
* @returns The converted POSIX-style path.
|
|
18
|
+
* @see https://github.com/mrmlnc/fast-glob/blob/28a3d61e44d5d9193ba97de4f21df6dc7725f7c0/src/utils/path.ts
|
|
19
|
+
*/
|
|
20
|
+
export function convertPathToPosix(path) {
|
|
21
|
+
return platform() === 'win32'
|
|
22
|
+
? path
|
|
23
|
+
.replace(DOS_DEVICE_PATH_RE, '//$1')
|
|
24
|
+
.replaceAll(WINDOWS_BACKSLASHES_RE, '/')
|
|
25
|
+
: path;
|
|
26
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Page } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Filter route by a list of glob patterns.
|
|
4
|
+
* @param pages - List of pages.
|
|
5
|
+
* @param routePatterns - List of glob patterns.
|
|
6
|
+
* @returns - List of pages that match the glob patterns.
|
|
7
|
+
*/
|
|
8
|
+
export declare function filterRoute(pages: Page[], routePatterns: string[]): Page[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import multimatch from 'multimatch';
|
|
2
|
+
/**
|
|
3
|
+
* Filter route by a list of glob patterns.
|
|
4
|
+
* @param pages - List of pages.
|
|
5
|
+
* @param routePatterns - List of glob patterns.
|
|
6
|
+
* @returns - List of pages that match the glob patterns.
|
|
7
|
+
*/
|
|
8
|
+
export function filterRoute(pages, routePatterns) {
|
|
9
|
+
const pagePaths = multimatch(pages.map(({ path }) => path), routePatterns);
|
|
10
|
+
return pages.filter(({ path }) => pagePaths.includes(path));
|
|
11
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { PDFArray } from 'pdf-lib';
|
|
3
|
+
import type { OutlineNode } from '../../html-export-pdf/index.js';
|
|
4
|
+
export interface NormalizePage {
|
|
5
|
+
location: string;
|
|
6
|
+
pagePath: string;
|
|
7
|
+
url: string;
|
|
8
|
+
title?: string;
|
|
9
|
+
}
|
|
10
|
+
export type PDFOutline = [path: string, outlineNodes: OutlineNode[]];
|
|
11
|
+
export interface Outline {
|
|
12
|
+
title: string;
|
|
13
|
+
dest: PDFArray;
|
|
14
|
+
children: Outline[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Based on @see https://github.com/Hopding/pdf-lib/issues/867#issuecomment-827570106
|
|
18
|
+
*/
|
|
19
|
+
export declare const replaceLinksWithOutline: (pdfData: Buffer | Uint8Array, pdfOutlines: PDFOutline[]) => Promise<Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>>;
|
|
20
|
+
/**
|
|
21
|
+
* Merge PDFs.
|
|
22
|
+
* @param pages - NormalizePage
|
|
23
|
+
* @param outFile - Output file
|
|
24
|
+
* @param outDir - Output directory
|
|
25
|
+
* @returns relativePath - Output relative path
|
|
26
|
+
*/
|
|
27
|
+
export declare function mergePDF(pages: NormalizePage[], outFile: string, outDir: string, pdfOutlines: PDFOutline[]): Promise<string>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import fse from 'fs-extra';
|
|
2
|
+
import { Buffer } from 'node:buffer';
|
|
3
|
+
import { join, relative } from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { asPDFName, PDFArray, PDFDict, PDFDocument, PDFObject, PDFString, } from 'pdf-lib';
|
|
6
|
+
import PDFMerger from 'pdf-merger-js';
|
|
7
|
+
import { red } from 'yoctocolors';
|
|
8
|
+
import { mergePDFs } from '../../merge-pdfs/index.js';
|
|
9
|
+
import { convertPathToPosix } from './convertPathToPosix.js';
|
|
10
|
+
import { getUrlLink } from './getUrlLink.js';
|
|
11
|
+
const extractOutline = (doc, dict) => {
|
|
12
|
+
return {
|
|
13
|
+
title: dict.get(asPDFName('Title')).decodeText(),
|
|
14
|
+
dest: dict.get(asPDFName('Dest')),
|
|
15
|
+
children: extractOutlines(doc, dict),
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
const extractOutlines = (doc, dictObj) => {
|
|
19
|
+
const outlineDict = doc.context.lookup(dictObj, PDFDict);
|
|
20
|
+
const first = outlineDict.get(asPDFName('First'));
|
|
21
|
+
let dict = doc.context.lookupMaybe(first, PDFDict);
|
|
22
|
+
if (!dict) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const outlines = [extractOutline(doc, dict)];
|
|
26
|
+
let obj;
|
|
27
|
+
while ((obj = dict.get(asPDFName('Next')))) {
|
|
28
|
+
dict = doc.context.lookup(obj, PDFDict);
|
|
29
|
+
outlines.push(extractOutline(doc, dict));
|
|
30
|
+
}
|
|
31
|
+
return outlines;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Based on @see https://github.com/Hopding/pdf-lib/issues/867#issuecomment-827570106
|
|
35
|
+
*/
|
|
36
|
+
export const replaceLinksWithOutline = async (pdfData, pdfOutlines) => {
|
|
37
|
+
const pdfDoc = await PDFDocument.load(pdfData);
|
|
38
|
+
const outlinesObj = pdfDoc.catalog.get(asPDFName('Outlines'));
|
|
39
|
+
if (!outlinesObj) {
|
|
40
|
+
return pdfData;
|
|
41
|
+
}
|
|
42
|
+
const outlineMap = new WeakMap();
|
|
43
|
+
const outlineNodes = pdfOutlines.flatMap(([, outlineNodes]) => outlineNodes);
|
|
44
|
+
const outlineNodeMap = new Map(pdfOutlines);
|
|
45
|
+
const outlines = extractOutlines(pdfDoc, outlinesObj);
|
|
46
|
+
for (const [index, outlineNode] of outlineNodes.entries()) {
|
|
47
|
+
const outline = outlines[index];
|
|
48
|
+
outlineMap.set(outlineNode, outline);
|
|
49
|
+
for (const [index, outlineChild] of outlineNode.children.entries()) {
|
|
50
|
+
outlineMap.set(outlineChild, outline.children[index]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const pages = pdfDoc.getPages();
|
|
54
|
+
for (const page of pages) {
|
|
55
|
+
for (const annot of page.node.Annots()?.asArray() || []) {
|
|
56
|
+
const dict = pdfDoc.context.lookupMaybe(annot, PDFDict);
|
|
57
|
+
const aRecord = dict?.get(asPDFName(`A`));
|
|
58
|
+
const link = pdfDoc.context.lookupMaybe(aRecord, PDFDict);
|
|
59
|
+
const uri = link?.get(asPDFName('URI'))?.toString().slice(1, -1); // get the original link, remove parenthesis
|
|
60
|
+
if (!dict || !aRecord || !link || !uri) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const url = uri.replace(/\\(\d{2,3})/g, (_, code) => String.fromCharCode(Number.parseInt(code, 8)));
|
|
64
|
+
if (/^https?:\/\//.test(url)) {
|
|
65
|
+
const { link, hash } = getUrlLink(url);
|
|
66
|
+
let matched = outlineNodeMap.get(link);
|
|
67
|
+
if (!matched && link.endsWith('/')) {
|
|
68
|
+
matched = outlineNodeMap.get(link + 'index.html');
|
|
69
|
+
}
|
|
70
|
+
if (!matched?.length) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
let matchedOutlineNode = matched[0];
|
|
74
|
+
if (hash) {
|
|
75
|
+
for (const node of matched) {
|
|
76
|
+
if (node.destination === hash) {
|
|
77
|
+
matchedOutlineNode = node;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
for (const child of node.children) {
|
|
81
|
+
if (child.destination === hash) {
|
|
82
|
+
matchedOutlineNode = child;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const matchedOutline = outlineMap.get(matchedOutlineNode);
|
|
89
|
+
if (!matchedOutline) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
dict.set(asPDFName('A'), pdfDoc.context.obj({
|
|
93
|
+
S: 'GoTo',
|
|
94
|
+
D: matchedOutline.dest,
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return pdfDoc.save();
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Merge PDFs.
|
|
103
|
+
* @param pages - NormalizePage
|
|
104
|
+
* @param outFile - Output file
|
|
105
|
+
* @param outDir - Output directory
|
|
106
|
+
* @returns relativePath - Output relative path
|
|
107
|
+
*/
|
|
108
|
+
export async function mergePDF(pages, outFile, outDir, pdfOutlines) {
|
|
109
|
+
const saveDirPath = join(process.cwd(), outDir);
|
|
110
|
+
if (outDir) {
|
|
111
|
+
fse.ensureDirSync(saveDirPath);
|
|
112
|
+
}
|
|
113
|
+
const saveFilePath = join(saveDirPath, outFile);
|
|
114
|
+
if (pages.length === 0) {
|
|
115
|
+
process.stdout.write(red('The website has no pages, please check whether the export path is set correctly'));
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
else if (pages.length === 1) {
|
|
119
|
+
fse.moveSync(pages[0].pagePath, saveFilePath, { overwrite: true });
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
let pdfData;
|
|
123
|
+
if (pdfOutlines.length > 0) {
|
|
124
|
+
pdfData = await mergePDFs(pages.map(({ pagePath }) => {
|
|
125
|
+
const relativePagePath = relative(process.cwd(), pagePath);
|
|
126
|
+
return convertPathToPosix(relativePagePath);
|
|
127
|
+
}));
|
|
128
|
+
pdfData = await replaceLinksWithOutline(pdfData, pdfOutlines);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const merger = new PDFMerger();
|
|
132
|
+
for (const { pagePath } of pages) {
|
|
133
|
+
await merger.add(pagePath);
|
|
134
|
+
}
|
|
135
|
+
pdfData = await merger.saveAsBuffer();
|
|
136
|
+
}
|
|
137
|
+
fse.writeFileSync(saveFilePath, pdfData, { encoding: 'binary' });
|
|
138
|
+
}
|
|
139
|
+
return relative(process.cwd(), saveFilePath);
|
|
140
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* get current time in format: YYYY-MM-DD-HH-mm-ss
|
|
3
|
+
* @param timestamp - Timestamp
|
|
4
|
+
* @param lang - Language
|
|
5
|
+
* @param dateOptions - Date options
|
|
6
|
+
* @returns - Date string
|
|
7
|
+
*/
|
|
8
|
+
export declare function timeTransformer(timestamp?: Date, lang?: string, dateOptions?: Intl.DateTimeFormatOptions): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const options = {
|
|
2
|
+
year: 'numeric',
|
|
3
|
+
month: '2-digit',
|
|
4
|
+
day: '2-digit',
|
|
5
|
+
hour: '2-digit',
|
|
6
|
+
minute: '2-digit',
|
|
7
|
+
second: '2-digit',
|
|
8
|
+
hour12: false,
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* get current time in format: YYYY-MM-DD-HH-mm-ss
|
|
12
|
+
* @param timestamp - Timestamp
|
|
13
|
+
* @param lang - Language
|
|
14
|
+
* @param dateOptions - Date options
|
|
15
|
+
* @returns - Date string
|
|
16
|
+
*/
|
|
17
|
+
export function timeTransformer(timestamp = new Date(), lang = 'zh-CN', dateOptions = options) {
|
|
18
|
+
return new Date(timestamp)
|
|
19
|
+
.toLocaleString(lang, dateOptions)
|
|
20
|
+
.replaceAll(/(\/|:|)/g, '')
|
|
21
|
+
.replace(/\s/, '-');
|
|
22
|
+
}
|
package/lib/cli/export.d.ts
CHANGED
package/lib/cli/export.js
CHANGED
|
@@ -1,33 +1,101 @@
|
|
|
1
|
-
import
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { generatePdf, } from './export-pdf-core/index.js';
|
|
3
|
+
import { removeLeadingSlash, serve } from '@rspress/core';
|
|
4
|
+
import { logger } from '@rspress/shared/logger';
|
|
2
5
|
import { Command } from 'commander';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
6
|
+
import { autoSidebar } from '../plugins/index.js';
|
|
7
|
+
import { pathExists, removeBothEndsSlashes, setNodeEnv, } from '../utils/index.js';
|
|
5
8
|
import { loadConfig } from './load-config.js';
|
|
6
|
-
export const processExport = async (sidebar, lang) => {
|
|
7
|
-
console.log(sidebar, lang);
|
|
8
|
-
chromium.launch({
|
|
9
|
-
headless: false,
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
9
|
export const exportCommand = new Command('export')
|
|
13
|
-
.description('Export the documentation
|
|
10
|
+
.description('Export the documentation as PDF')
|
|
14
11
|
.argument('[root]', 'Root directory of the documentation')
|
|
15
|
-
.option('--host [host]', '
|
|
16
|
-
.option('--port [port]', '
|
|
12
|
+
.option('-H, --host [host]', 'Serve host name')
|
|
13
|
+
.option('-P, --port [port]', 'Serve port number', '4173')
|
|
17
14
|
.action(async function (root) {
|
|
15
|
+
setNodeEnv('production');
|
|
18
16
|
const { port, host, ...globalOptions } = this.optsWithGlobals();
|
|
19
|
-
|
|
20
|
-
const
|
|
17
|
+
let { config } = await loadConfig(root, globalOptions);
|
|
18
|
+
const outDir = config.outDir;
|
|
19
|
+
if (!(await pathExists(path.resolve(outDir, 'index.html'), 'file'))) {
|
|
20
|
+
logger.error('Please build the documentation first');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
config = await autoSidebar(config, {
|
|
21
24
|
...config.sidebar,
|
|
22
25
|
excludeRoutes: (globalOptions.ignore && config.internalRoutes) || [],
|
|
23
|
-
}
|
|
26
|
+
});
|
|
27
|
+
config.builderConfig.server.open = false;
|
|
28
|
+
const themeConfig = config.themeConfig;
|
|
29
|
+
logger.start('Serving...');
|
|
24
30
|
await serve({ config, host, port });
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
const collectPages = (sidebarItems) => {
|
|
32
|
+
const pages = [];
|
|
33
|
+
for (const item of sidebarItems) {
|
|
34
|
+
if ('link' in item && item.link) {
|
|
35
|
+
const link = removeLeadingSlash(item.link);
|
|
36
|
+
pages.push({
|
|
37
|
+
key: link,
|
|
38
|
+
path: config.base + link + '.html?print=true',
|
|
39
|
+
title: item.text,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if ('items' in item) {
|
|
43
|
+
pages.push(...collectPages(item.items));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return pages;
|
|
47
|
+
};
|
|
48
|
+
const commonOptions = {
|
|
49
|
+
tempDir: path.resolve(outDir, '.doom'),
|
|
50
|
+
port: port,
|
|
51
|
+
host: host || 'localhost',
|
|
52
|
+
outDir,
|
|
53
|
+
pdfOptions: {
|
|
54
|
+
margin: {
|
|
55
|
+
top: 40,
|
|
56
|
+
right: 40,
|
|
57
|
+
bottom: 40,
|
|
58
|
+
left: 40,
|
|
59
|
+
},
|
|
60
|
+
printBackground: true,
|
|
61
|
+
headerTemplate: /* HTML */ `<div
|
|
62
|
+
style="margin-top: -0.4cm; height: 70%; width: 100%; display: flex; justify-content: center; align-items: center; color: lightgray; border-bottom: solid lightgray 1px; font-size: 10px;"
|
|
63
|
+
>
|
|
64
|
+
<span class="title"></span>
|
|
65
|
+
</div>`,
|
|
66
|
+
footerTemplate: /* HTML */ `<div
|
|
67
|
+
style="margin-bottom: -0.4cm; height: 70%; width: 100%; display: flex; justify-content: flex-start; align-items: center; color: lightgray; border-top: solid lightgray 1px; font-size: 10px;"
|
|
68
|
+
></div>`,
|
|
69
|
+
},
|
|
70
|
+
printerOptions: {
|
|
71
|
+
ignoreHTTPSErrors: true,
|
|
72
|
+
initScripts: [
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
74
|
+
async () => {
|
|
75
|
+
localStorage.setItem('rspress-theme-appearance', 'light');
|
|
76
|
+
localStorage.setItem('rspress-visited', '1');
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
outlineContainerSelector: '.rspress-doc',
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
const exportPdf = async (sidebarItems, lang = config.lang) => {
|
|
83
|
+
await generatePdf({
|
|
84
|
+
pages: collectPages(sidebarItems),
|
|
85
|
+
outFile: `${removeBothEndsSlashes(config.base) || config.title}-${lang}.pdf`,
|
|
86
|
+
...commonOptions,
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
if (themeConfig.locales?.length) {
|
|
90
|
+
for (const { lang, sidebar } of themeConfig.locales) {
|
|
91
|
+
const sidebarItems = sidebar[config.lang === lang ? '/' : `/${lang}`];
|
|
92
|
+
await exportPdf(sidebarItems, lang);
|
|
28
93
|
}
|
|
29
94
|
}
|
|
30
95
|
else {
|
|
31
|
-
|
|
96
|
+
const sidebarItems = themeConfig.sidebar['/'];
|
|
97
|
+
await exportPdf(sidebarItems);
|
|
32
98
|
}
|
|
99
|
+
// close the server
|
|
100
|
+
process.exit(0);
|
|
33
101
|
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { PDFDocument, PDFObject, PDFRef } from 'pdf-lib';
|
|
2
|
+
import { PDFName } from 'pdf-lib';
|
|
3
|
+
import type { Page } from 'playwright';
|
|
4
|
+
export type DictMap = Map<PDFName, PDFObject>;
|
|
5
|
+
export interface RootOutlineNode {
|
|
6
|
+
children: OutlineNode[];
|
|
7
|
+
depth: number;
|
|
8
|
+
parent?: OutlineNode | RootOutlineNode;
|
|
9
|
+
}
|
|
10
|
+
export interface OutlineNode {
|
|
11
|
+
title: string;
|
|
12
|
+
destination: string;
|
|
13
|
+
children: OutlineNode[];
|
|
14
|
+
depth: number;
|
|
15
|
+
parent?: OutlineNode | RootOutlineNode;
|
|
16
|
+
italic?: boolean;
|
|
17
|
+
bold?: boolean;
|
|
18
|
+
color?: number[];
|
|
19
|
+
}
|
|
20
|
+
export interface OutlineRef extends OutlineNode {
|
|
21
|
+
children: OutlineRef[];
|
|
22
|
+
ref: PDFRef;
|
|
23
|
+
parentRef: PDFRef;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Format the outline container selector by removing extra spaces and ensuring trailing space.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} outlineContainerSelector - The selector for the outline container.
|
|
29
|
+
* @returns The formatted selector.
|
|
30
|
+
*/
|
|
31
|
+
export declare function formatOutlineContainerSelector(outlineContainerSelector: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the outline of a webpage using a headless browser.
|
|
34
|
+
* @param {Page} page - The page to evaluate.
|
|
35
|
+
* @param {string[]} tags - An array of tag names to use for the outline.
|
|
36
|
+
* @param outlineContainerSelector - Outline Container Selector
|
|
37
|
+
* @returns A Promise that resolves to an array of top-level OutlineNode objects representing the parsed outline.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getOutlineNodes(page: Page, tags: string[], outlineContainerSelector?: string): Promise<OutlineNode[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Sets the outlines of a PDF document from a nested outline tree.
|
|
42
|
+
* @param {PDFDocument} pdfDoc - The PDF document to set outlines on.
|
|
43
|
+
* @param {OutlineNode[]} outlineNodes - The nested outline tree to use as outlines.
|
|
44
|
+
* @param {boolean} [enableWarnings=false] - Whether to generate warnings for missing destinations.
|
|
45
|
+
* @returns The PDF document with outlines set.
|
|
46
|
+
*/
|
|
47
|
+
export declare function setOutlineNodes(pdfDoc: PDFDocument, outlineNodes: OutlineNode[], enableWarnings?: boolean): OutlineRef[];
|