Skip to content

Commit 42d6427

Browse files
committed
Use light theme colors in pdf generation
1 parent d52723f commit 42d6427

6 files changed

Lines changed: 25 additions & 22 deletions

File tree

Binary file not shown.

scripts/generate-pdfs.mjs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,23 @@ const generatePdf = async (browser, slug) => {
104104
await page.emulateMediaType('print')
105105

106106
// Wait for all images to finish loading
107-
await page.evaluate(() =>
108-
Promise.all(
109-
Array.from(document.images)
107+
await page.evaluate(() => {
108+
const doc = globalThis.document
109+
110+
return Promise.all(
111+
Array.from(doc.images)
110112
.filter(img => !img.complete)
111113
.map(img => new Promise(r => { img.onload = r; img.onerror = r }))
112114
)
113-
)
115+
})
114116

115-
const title = await page.evaluate(() => document.title)
117+
const title = await page.title()
116118

117119
// Inject approximate page numbers into the ToC
118120
await page.evaluate((contentH) => {
119-
const toc = document.querySelector('.print-toc')
120-
const article = document.querySelector('.print-article')
121+
const doc = globalThis.document
122+
const toc = doc.querySelector('.print-toc')
123+
const article = doc.querySelector('.print-article')
121124
if (!toc || !article) return
122125

123126
// Cover always occupies 1 page (break-after: page)
@@ -133,7 +136,7 @@ const generatePdf = async (browser, slug) => {
133136
pageMap.set(h.id, pagesBeforeArticle + Math.floor(Math.max(0, offset) / contentH) + 1)
134137
}
135138

136-
for (const span of document.querySelectorAll('.print-toc__page')) {
139+
for (const span of doc.querySelectorAll('.print-toc__page')) {
137140
const href = span.closest('a')?.getAttribute('href')?.replace('#', '')
138141
if (href && pageMap.has(href)) {
139142
span.textContent = String(pageMap.get(href))

src/components/Layout/Print/Cover/index.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ const publishDateLabel = article.data.publishDate.toLocaleDateString(undefined,
1616
})
1717
---
1818

19-
<section class="print-cover">
20-
<h1 class="print-cover__title">{article.data.title}</h1>
21-
<div class="print-cover__image">
19+
<section class="flex min-h-[100vh] break-after-page flex-col items-center justify-center text-center">
20+
<h1 class="mb-[1cm] text-[28pt] leading-[1.2] font-bold">{article.data.title}</h1>
21+
<div class="mb-[1cm] max-w-[80%]">
2222
<Image
2323
src={article.data.cover}
2424
alt={article.data.coverAlt}
2525
loading="eager"
2626
variant="full"
2727
/>
2828
</div>
29-
<p class="print-cover__author">By {authorName}</p>
30-
<p class="print-cover__date">
29+
<p class="mb-[0.3cm] text-[14pt]">By {authorName}</p>
30+
<p class="text-[11pt] text-(--color-content-offset)">
3131
Published on <time datetime={publishDateIso}>{publishDateLabel}</time>
3232
</p>
3333
</section>

src/components/Layout/Print/Toc/index.astro

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ const tocTree = buildTocTree(headings)
1818
---
1919

2020
{tocTree.length > 0 && (
21-
<section class="print-toc">
22-
<h2 class="print-toc__title">Table of Contents</h2>
21+
<section class="print-toc break-after-page py-[1cm]">
22+
<h2 class="mb-[1cm] text-[20pt] font-bold">Table of Contents</h2>
2323
<nav aria-label="Table of contents">
2424
{(() => {
2525
const renderNodes = (nodes: TocItem[], depth = 0): unknown =>
2626
nodes.map(node => (
2727
<div class:list={[
28-
'print-toc__entry',
29-
depth > 0 && 'print-toc__entry--nested',
28+
'mb-[0.15cm]',
29+
depth > 0 && 'pl-[1.5em]',
3030
]}>
31-
<a href={`#${node.slug}`} class="print-toc__link">
32-
<span class="print-toc__text">{node.text}</span>
33-
<span class="print-toc__leader" aria-hidden="true"></span>
31+
<a href={`#${node.slug}`} class="flex items-baseline text-inherit no-underline">
32+
<span class="shrink-0">{node.text}</span>
33+
<span class="mx-[0.3em] min-w-[2em] grow border-b border-dotted border-[#999]" aria-hidden="true"></span>
3434
<span class="print-toc__page"></span>
3535
</a>
3636
{node.children.length > 0 && renderNodes(node.children, depth + 1)}

src/pages/print/[...slug].astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ const publishDate = article.data.publishDate
9393
</PrintLayout>
9494

9595
<style is:global>
96-
@import '@styles/pdf.css';
96+
@import './index.css';
9797
</style>

src/pages/print/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* =============================================================================
2929
*/
3030
@media print {
31-
html[data-theme="light"] {
31+
html[data-theme="light"][data-theme] {
3232
--theme-color-primary: #001A39;
3333
--theme-color-primary-offset: #003d86;
3434
--theme-color-primary-inverse: #FFF;

0 commit comments

Comments
 (0)