From 76619cb7dba85deb1f3ce44439029511070c913d Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Tue, 3 Sep 2024 18:47:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E7=BB=88=E6=AD=A2?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/docs-to-pdf/src/index.ts | 9 +++++- tools/docs-to-pdf/src/print-page.ts | 41 ++++++++++++++++++++++------ tools/docs-to-pdf/src/print-pages.ts | 16 +++++------ tools/docs-to-pdf/src/puppetter.ts | 18 ------------ 4 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 tools/docs-to-pdf/src/puppetter.ts diff --git a/tools/docs-to-pdf/src/index.ts b/tools/docs-to-pdf/src/index.ts index 8e07c32f4d..f70fd52517 100644 --- a/tools/docs-to-pdf/src/index.ts +++ b/tools/docs-to-pdf/src/index.ts @@ -2,7 +2,14 @@ import fs from 'node:fs/promises'; import { printPages, type Range } from './print-pages.ts'; import { PdfGenerator } from './generate-pdf.ts'; -const RANGES: Range[] = [['/documents/software/emtlab/', '/documents/software/emtlab/introduction-to-emtlab/']]; +// const RANGES: Range[] = [['/documents/software/emtlab/', '/documents/software/emtlab/introduction-to-emtlab/']]; +const RANGES: Range[] = [ + ['https://kb.cloudpss.net/documents/quick-start/quick-start/'], + ['https://kb.cloudpss.net/documents/software/'], + ['https://kb.cloudpss.net/documents/hardware/desktop-type/'], + ['https://kb.cloudpss.net/documents/software-tools/conversion-tools/'], + ['https://kb.cloudpss.net/documents/maintenance-tools/server-manager/'], +]; //'/documents/software/emtlab/topology-analysis/'; //const TO = '/documents/software/xstudio/simstudio/cloud-space/'; diff --git a/tools/docs-to-pdf/src/print-page.ts b/tools/docs-to-pdf/src/print-page.ts index 78b7815919..d690da4dc5 100644 --- a/tools/docs-to-pdf/src/print-page.ts +++ b/tools/docs-to-pdf/src/print-page.ts @@ -1,6 +1,34 @@ -import { browser } from './puppetter.ts'; import fs from 'node:fs/promises'; import path from 'node:path'; +import { type Browser, launch, type Page } from 'puppeteer'; +import { HOST } from './constants.ts'; + +let _browser: Browser | null = null; +let _page: Page; +/** 浏览器 */ +export async function getPage(): Promise { + if (!_browser) { + _browser = await launch(); + _page = await _browser.newPage(); + await _page.goto(HOST, { waitUntil: 'domcontentloaded', timeout: 0 }); + await _page.evaluate( + ({ THEME_KEY }) => { + localStorage.setItem(THEME_KEY, 'light'); + window.dispatchEvent(new StorageEvent('storage', { key: THEME_KEY })); + }, + { THEME_KEY }, + ); + } + return _page; +} + +/** 退出浏览器 */ +export async function done(): Promise { + if (_browser) { + await _browser.close(); + _browser = null; + } +} const INJECT_CSS = /* css */ ` ${await fs.readFile(path.resolve(import.meta.dirname, '../../../src/css/print.css'), 'utf8')} @@ -18,13 +46,10 @@ const NEXT_SELECTOR = 'a.pagination-nav__link.pagination-nav__link--next'; /** 打印页面到指定位置 */ export async function printPage(url: string, dist: string): Promise<{ title: string; prev?: string; next?: string }> { - const b = await browser(); - const page = await b.newPage(); + const page = await getPage(); await page.goto(url, { waitUntil: 'networkidle0', timeout: 0 }); const result = await page.evaluate( - ({ INJECT_CSS, THEME_KEY, HEADER_SELECTOR, PREV_SELECTOR, NEXT_SELECTOR }) => { - localStorage.setItem(THEME_KEY, 'light'); - window.dispatchEvent(new StorageEvent('storage', { key: THEME_KEY })); + ({ INJECT_CSS, HEADER_SELECTOR, PREV_SELECTOR, NEXT_SELECTOR }) => { for (const img of document.querySelectorAll('img')) { img.loading = 'eager'; } @@ -41,7 +66,7 @@ export async function printPage(url: string, dist: string): Promise<{ title: str return { title, prev, next }; }, - { INJECT_CSS, THEME_KEY, HEADER_SELECTOR, PREV_SELECTOR, NEXT_SELECTOR }, + { INJECT_CSS, HEADER_SELECTOR, PREV_SELECTOR, NEXT_SELECTOR }, ); await page.waitForNetworkIdle({ idleTime: 100 }); await fs.mkdir(path.dirname(dist), { recursive: true }); @@ -50,6 +75,7 @@ export async function printPage(url: string, dist: string): Promise<{ title: str format: 'A4', printBackground: true, scale: 0.8, + outline: true, margin: { top: '1.90cm', left: '1.90cm', @@ -57,6 +83,5 @@ export async function printPage(url: string, dist: string): Promise<{ title: str bottom: '1.90cm', }, }); - await page.close(); return result; } diff --git a/tools/docs-to-pdf/src/print-pages.ts b/tools/docs-to-pdf/src/print-pages.ts index f435efa20a..629a29784c 100644 --- a/tools/docs-to-pdf/src/print-pages.ts +++ b/tools/docs-to-pdf/src/print-pages.ts @@ -1,10 +1,9 @@ import path from 'node:path'; import { HOST } from './constants.ts'; -import { printPage } from './print-page.ts'; -import { exit } from './puppetter.ts'; +import { done, printPage } from './print-page.ts'; /** 表示页面范围,包含开始和结束页 */ -export type Range = [string, string]; +export type Range = [string, string?]; /** 格式化页面范围的链接 */ function formatRangeLink(link: string): string { @@ -32,15 +31,16 @@ export interface PrintedDocument { async function printRange(range: Range, dist: string): Promise { const result = []; const from = formatRangeLink(range[0]); - const to = formatRangeLink(range[1]); + const to = range[1] ? formatRangeLink(range[1]) : undefined; let url: string | undefined = HOST + from; while (url) { - const file = path.join(dist, url.slice(`${HOST}/`.length).replace(/\/?$/, '.pdf')); - process.stdout.write(`Printing ${url} -> ${path.relative(process.cwd(), file)} ...`); + const pathname = url.slice(HOST.length); + const file = path.join(dist, pathname.slice(1).replace(/\/?$/, '.pdf')); + process.stdout.write(`Printing ${pathname} ...`); const pageInfo = await printPage(url, file); result.push({ ...pageInfo, url, file }); process.stdout.write('\b\b\bDone\n'); - if (url === HOST + to) break; + if (to && url === HOST + to) break; url = pageInfo.next; } return result; @@ -53,6 +53,6 @@ export async function printPages(ranges: readonly Range[], dist: string): Promis for (const range of ranges) { result.push(...(await printRange(range, dist))); } - await exit(); + await done(); return result; } diff --git a/tools/docs-to-pdf/src/puppetter.ts b/tools/docs-to-pdf/src/puppetter.ts deleted file mode 100644 index 55d802f966..0000000000 --- a/tools/docs-to-pdf/src/puppetter.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { type Browser, launch } from 'puppeteer'; - -let browserInstance: Browser | null = null; -/** 浏览器 */ -export async function browser(): Promise { - if (!browserInstance) { - browserInstance = await launch(); - } - return browserInstance; -} - -/** 退出 */ -export async function exit(): Promise { - if (browserInstance) { - await browserInstance.close(); - browserInstance = null; - } -}