Skip to content

Commit

Permalink
feat: 移除终止条件限制
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Sep 3, 2024
1 parent 7cd8b8e commit 76619cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
9 changes: 8 additions & 1 deletion tools/docs-to-pdf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down
41 changes: 33 additions & 8 deletions tools/docs-to-pdf/src/print-page.ts
Original file line number Diff line number Diff line change
@@ -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<Page> {
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<void> {
if (_browser) {
await _browser.close();
_browser = null;
}
}

const INJECT_CSS = /* css */ `
${await fs.readFile(path.resolve(import.meta.dirname, '../../../src/css/print.css'), 'utf8')}
Expand All @@ -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';
}
Expand All @@ -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 });
Expand All @@ -50,13 +75,13 @@ 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',
right: '1.90cm',
bottom: '1.90cm',
},
});
await page.close();
return result;
}
16 changes: 8 additions & 8 deletions tools/docs-to-pdf/src/print-pages.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -32,15 +31,16 @@ export interface PrintedDocument {
async function printRange(range: Range, dist: string): Promise<PrintedDocument[]> {
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;
Expand All @@ -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;
}
18 changes: 0 additions & 18 deletions tools/docs-to-pdf/src/puppetter.ts

This file was deleted.

0 comments on commit 76619cb

Please sign in to comment.