From e337bd8f5938f68613a1121dfa5023df3b86a91e Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 20 Sep 2024 10:48:35 +0800 Subject: [PATCH] feat(sample): line number --- packages/sample/lib/lib/pdf.mjs | 88 ++++++++++++++++------------ packages/sample/lib/lib/picker.mjs | 16 ++++- packages/sample/lib/lib/template.txt | 52 +++++++++++----- packages/sample/package.json | 2 +- packages/utils/index.mjs | 4 ++ 5 files changed, 107 insertions(+), 55 deletions(-) diff --git a/packages/sample/lib/lib/pdf.mjs b/packages/sample/lib/lib/pdf.mjs index 5dac333..b90823b 100644 --- a/packages/sample/lib/lib/pdf.mjs +++ b/packages/sample/lib/lib/pdf.mjs @@ -6,25 +6,27 @@ function html([string]) { return string; } -const style = html``; +const style = html` + +`; export async function pdf(data, config) { const { chromium: instance } = await import( @@ -33,7 +35,8 @@ export async function pdf(data, config) { ); const browser = await instance.launch(); - const page = await browser.newPage(); + const context = await browser.newContext(); + const page = await context.newPage(); const raw = { ...config, data }; @@ -43,10 +46,13 @@ export async function pdf(data, config) { const options = { headerTemplate: - html`
-
- / -
` + style, + html` +
+
+ / + +
+ ` + style, footerTemplate: ' ', }; @@ -60,20 +66,30 @@ export async function pdf(data, config) { }, ); - await page.pdf({ - path: join( - process.cwd(), - '.bring-it', - 'sample', - `${config.title}_${config.version}_源代码.pdf`, - ), - format: 'A4', - printBackground: true, - displayHeaderFooter: true, - ...options, - }); + const { error } = await page + .pdf({ + path: join( + process.cwd(), + '.bring-it', + 'sample', + `${config.title}_${config.version}_源代码.pdf`, + ), + format: 'A4', + printBackground: true, + displayHeaderFooter: true, + ...options, + }) + .catch((error_) => { + return { error: error_ }; + }); await page.close(); + await context.close(); + await browser.close(); + + if (error) { + throw error; + } } diff --git a/packages/sample/lib/lib/picker.mjs b/packages/sample/lib/lib/picker.mjs index 0c80984..c84f2dc 100644 --- a/packages/sample/lib/lib/picker.mjs +++ b/packages/sample/lib/lib/picker.mjs @@ -14,11 +14,13 @@ function read(file, config) { return readFile(join(config.cwd, file), 'utf8'); } +const LINE_NUMBERS = 50; + export async function picker(lists, config) { const io = []; for (const file of lists) { - if (io.length < 3050) { + if (io.length < LINE_NUMBERS) { await read(file, config) .then((code) => code @@ -45,7 +47,17 @@ export async function picker(lists, config) { export function scan(config) { return globby(config.pattern, { cwd: config.cwd, - ignore: [...defaultIgnore, ...config.ignore, '**/.bring-it/**'], + ignore: [ + ...defaultIgnore, + ...config.ignore, + '**/.best-shot/**', + '**/.bring-it/**', + '**/.github/**', + '**/dist/**', + '**/License', + '**/License.*', + '**/*.md', + ], gitignore: true, onlyFiles: true, dot: true, diff --git a/packages/sample/lib/lib/template.txt b/packages/sample/lib/lib/template.txt index afb231c..3fb42b6 100644 --- a/packages/sample/lib/lib/template.txt +++ b/packages/sample/lib/lib/template.txt @@ -1,4 +1,4 @@ - + @@ -15,21 +15,34 @@ font-family: 'Noto Sans Mono CJK SC', monospace; } header { - text-align: center; break-after: page; margin-top: 20em; + text-align: center; } h3 { margin-top: 100pt; } pre, code { + display: block; margin: 0; width: 100%; - - white-space: pre-wrap; - line-height: 1.25; font-size: 12pt; + line-height: 1.25; + white-space: pre-wrap; + } + code { + counter-increment: line; + overflow: hidden; + } + code::before { + display: inline-block; + margin-right: 1em; + background-color: #f5f5f5; + padding-right: 2px; + width: 2em; + content: counter(line); + text-align: right; } @@ -38,24 +51,31 @@

源代码

-
+

   
   
 
diff --git a/packages/sample/package.json b/packages/sample/package.json
index f438d7b..1ded09a 100644
--- a/packages/sample/package.json
+++ b/packages/sample/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@bring-it/sample",
-  "version": "0.4.12",
+  "version": "0.4.14",
   "description": "Generate code sample files",
   "license": "MIT",
   "author": {
diff --git a/packages/utils/index.mjs b/packages/utils/index.mjs
index 5069c0a..8e57e99 100644
--- a/packages/utils/index.mjs
+++ b/packages/utils/index.mjs
@@ -55,6 +55,9 @@ export const ignore = [
   '**/{node,web}_modules/**',
   '**/*.{pem,ppk}',
   '**/id_{d,r}sa',
+  '**/.obsidian/**',
+  '**/.docusaurus/**',
+  '**/miniprogram_npm/**',
   '**/ssh*config',
   '**/sshd*config',
 ];
@@ -92,6 +95,7 @@ export function http({ url, query, json, method = 'GET' }) {
     io.searchParams.set(key, value);
   }
 
+  // eslint-disable-next-line n/no-unsupported-features/node-builtins
   return fetch(io.href, {
     method,
     headers: {