Skip to content

Commit

Permalink
feat(sample): line number
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Sep 20, 2024
1 parent 92e7846 commit e337bd8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 55 deletions.
88 changes: 52 additions & 36 deletions packages/sample/lib/lib/pdf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ function html([string]) {
return string;
}

const style = html`<style>
header {
display: flex;
margin: 0 5%;
width: 92%;
font-size: 8pt;
font-family: 'Noto Sans Mono CJK SC', monospace;
text-align: center;
}
.title {
margin-right: auto;
}
.pageNumber {
margin-right: 4pt;
}
.totalPages {
margin-left: 4pt;
}
</style>`;
const style = html`
<style>
header {
display: flex;
margin: 0 5%;
width: 92%;
font-size: 8pt;
font-family: 'Noto Sans Mono CJK SC', monospace;
text-align: center;
}
.title {
margin-right: auto;
}
.pageNumber {
margin-right: 4pt;
}
.totalPages {
margin-left: 4pt;
}
</style>
`;

export async function pdf(data, config) {
const { chromium: instance } = await import(
Expand All @@ -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 };

Expand All @@ -43,10 +46,13 @@ export async function pdf(data, config) {

const options = {
headerTemplate:
html`<header>
<div class="title"></div>
<span class="pageNumber"></span>/<span class="totalPages"></span>
</header>` + style,
html`
<header>
<div class="title"></div>
<span class="pageNumber"></span>/
<span class="totalPages"></span>
</header>
` + style,
footerTemplate: ' ',
};

Expand All @@ -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;
}
}
16 changes: 14 additions & 2 deletions packages/sample/lib/lib/picker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
52 changes: 36 additions & 16 deletions packages/sample/lib/lib/template.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
Expand All @@ -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;
}
</style>
<body>
Expand All @@ -38,24 +51,31 @@
<h2></h2>
<h3>源代码</h3>
</header>
<pre><code></code></pre>
<pre></pre>
</body>
<script defer>
const { body } = document;
window.mock().then((result) => {

const header = body.querySelector('header');
window
.mock()
.then((result) => {
const header = body.querySelector('header');

header.querySelector('h1').textContent = result.title;
header.querySelector('h1').textContent = result.title;

header.querySelector('h2').textContent = result.version;
header.querySelector('h2').textContent = result.version;

document.title = [result.title, result.version].join(' ');
document.title = [result.title, result.version].join(' ');

body.querySelector('pre code').textContent = result.data;
const pre = body.querySelector('pre');

}).catch(error => {
console.error(error);
});
for (const line of result.data.split('\n')) {
const code = document.createElement('code');
code.textContent = line;
pre.append(code);
}
})
.catch((error) => {
console.error(error);
});
</script>
</html>
2 changes: 1 addition & 1 deletion packages/sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sample",
"version": "0.4.12",
"version": "0.4.14",
"description": "Generate code sample files",
"license": "MIT",
"author": {
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const ignore = [
'**/{node,web}_modules/**',
'**/*.{pem,ppk}',
'**/id_{d,r}sa',
'**/.obsidian/**',
'**/.docusaurus/**',
'**/miniprogram_npm/**',
'**/ssh*config',
'**/sshd*config',
];
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit e337bd8

Please sign in to comment.