Skip to content

Commit 03ad80e

Browse files
committed
Merge upstream changes
2 parents f961008 + b4236e5 commit 03ad80e

File tree

9 files changed

+82
-105
lines changed

9 files changed

+82
-105
lines changed

quartz.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const config: QuartzConfig = {
2020
defaultDateType: "modified",
2121
locale: "en-US",
2222
theme: {
23+
fontOrigin: "googleFonts",
2324
cdnCaching: true,
2425
typography: {
2526
header: "Geist",
@@ -74,7 +75,7 @@ const config: QuartzConfig = {
7475
filters: [Plugin.RemoveDrafts()],
7576
emitters: [
7677
Plugin.AliasRedirects(),
77-
Plugin.ComponentResources({ fontOrigin: "googleFonts" }),
78+
Plugin.ComponentResources(),
7879
Plugin.ContentPage(),
7980
Plugin.FolderPage(),
8081
Plugin.TagPage(),

quartz/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ async function partialRebuildFromEntrypoint(
309309
}
310310
await rimraf([...destinationsToDelete])
311311

312+
console.log(chalk.green(`Done rebuilding in ${perf.timeSince()}`))
313+
312314
toRemove.clear()
313315
release()
314316
clientRefresh()

quartz/components/Head.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { i18n } from "../i18n"
22
import { FullSlug, joinSegments, pathToRoot } from "../util/path"
33
import { JSResourceToScriptElement } from "../util/resources"
4+
import { googleFontHref } from "../util/theme"
45
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
56

67
export default (() => {
@@ -47,10 +48,11 @@ export default (() => {
4748
/>
4849
<title>{title}</title>
4950
<meta charSet="utf-8" />
50-
{cfg.theme.cdnCaching && (
51+
{cfg.theme.cdnCaching && cfg.theme.fontOrigin === "googleFonts" && (
5152
<>
5253
<link rel="preconnect" href="https://fonts.googleapis.com" />
5354
<link rel="preconnect" href="https://fonts.gstatic.com" />
55+
<link rel="stylesheet" href={googleFontHref(cfg.theme)} />
5456
</>
5557
)}
5658
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

quartz/components/renderPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ export function renderPage(
118118
// skip until we find the blockref that matches
119119
if (el.properties?.id === blockRef) {
120120
startIdx = i
121-
startDepth = Number(el.tagName.substring(1))
121+
startDepth = depth
122122
}
123123
} else if (depth <= startDepth) {
124124
// looking for new header that is same level or higher
125125
endIdx = i
126+
break
126127
}
127128
}
128129

@@ -209,7 +210,7 @@ export function renderPage(
209210
</div>
210211
)
211212

212-
const lang = componentData.frontmatter?.lang ?? cfg.locale?.split("-")[0] ?? "en"
213+
const lang = componentData.fileData.frontmatter?.lang ?? cfg.locale?.split("-")[0] ?? "en"
213214
const doc = (
214215
<html lang={lang}>
215216
<Head {...componentData} />

quartz/plugins/emitters/componentResources.ts

Lines changed: 52 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import popoverScript from "../../components/scripts/popover.inline"
88
import styles from "../../styles/custom.scss"
99
import popoverStyle from "../../components/styles/popover.scss"
1010
import { BuildCtx } from "../../util/ctx"
11-
import { StaticResources } from "../../util/resources"
1211
import { QuartzComponent } from "../../components/types"
1312
import { googleFontHref, joinStyles } from "../../util/theme"
1413
import { Features, transform } from "lightningcss"
@@ -69,13 +68,8 @@ async function joinScripts(scripts: string[]): Promise<string> {
6968
return res.code
7069
}
7170

72-
function addGlobalPageResources(
73-
ctx: BuildCtx,
74-
staticResources: StaticResources,
75-
componentResources: ComponentResources,
76-
) {
71+
function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentResources) {
7772
const cfg = ctx.cfg.configuration
78-
const reloadScript = ctx.argv.serve
7973

8074
// popovers
8175
if (cfg.enablePopovers) {
@@ -85,12 +79,12 @@ function addGlobalPageResources(
8579

8680
if (cfg.analytics?.provider === "google") {
8781
const tagId = cfg.analytics.tagId
88-
staticResources.js.push({
89-
src: `https://www.googletagmanager.com/gtag/js?id=${tagId}`,
90-
contentType: "external",
91-
loadTime: "afterDOMReady",
92-
})
9382
componentResources.afterDOMLoaded.push(`
83+
const gtagScript = document.createElement("script")
84+
gtagScript.src = "https://www.googletagmanager.com/gtag/js?id=${tagId}"
85+
gtagScript.async = true
86+
document.head.appendChild(gtagScript)
87+
9488
window.dataLayer = window.dataLayer || [];
9589
function gtag() { dataLayer.push(arguments); }
9690
gtag("js", new Date());
@@ -166,115 +160,72 @@ function addGlobalPageResources(
166160
document.dispatchEvent(event)
167161
`)
168162
}
169-
170-
let wsUrl = `ws://localhost:${ctx.argv.wsPort}`
171-
172-
if (ctx.argv.remoteDevHost) {
173-
wsUrl = `wss://${ctx.argv.remoteDevHost}:${ctx.argv.wsPort}`
174-
}
175-
176-
if (reloadScript) {
177-
staticResources.js.push({
178-
loadTime: "afterDOMReady",
179-
contentType: "inline",
180-
script: `
181-
const socket = new WebSocket('${wsUrl}')
182-
// reload(true) ensures resources like images and scripts are fetched again in firefox
183-
socket.addEventListener('message', () => document.location.reload(true))
184-
`,
185-
})
186-
}
187-
}
188-
189-
interface Options {
190-
fontOrigin: "googleFonts" | "local"
191-
}
192-
193-
const defaultOptions: Options = {
194-
fontOrigin: "googleFonts",
195163
}
196164

197-
export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<Options>) => {
198-
const { fontOrigin } = { ...defaultOptions, ...opts }
165+
// This emitter should not update the `resources` parameter. If it does, partial
166+
// rebuilds may not work as expected.
167+
export const ComponentResources: QuartzEmitterPlugin = () => {
199168
return {
200169
name: "ComponentResources",
201170
getQuartzComponents() {
202171
return []
203172
},
204-
async getDependencyGraph(ctx, content, _resources) {
205-
// This emitter adds static resources to the `resources` parameter. One
206-
// important resource this emitter adds is the code to start a websocket
207-
// connection and listen to rebuild messages, which triggers a page reload.
208-
// The resources parameter with the reload logic is later used by the
209-
// ContentPage emitter while creating the final html page. In order for
210-
// the reload logic to be included, and so for partial rebuilds to work,
211-
// we need to run this emitter for all markdown files.
212-
const graph = new DepGraph<FilePath>()
213-
214-
for (const [_tree, file] of content) {
215-
const sourcePath = file.data.filePath!
216-
const slug = file.data.slug!
217-
graph.addEdge(sourcePath, joinSegments(ctx.argv.output, slug + ".html") as FilePath)
218-
}
219-
220-
return graph
173+
async getDependencyGraph(_ctx, _content, _resources) {
174+
return new DepGraph<FilePath>()
221175
},
222-
async emit(ctx, _content, resources): Promise<FilePath[]> {
176+
async emit(ctx, _content, _resources): Promise<FilePath[]> {
223177
const promises: Promise<FilePath>[] = []
224178
const cfg = ctx.cfg.configuration
225179
// component specific scripts and styles
226180
const componentResources = getComponentResources(ctx)
227181
let googleFontsStyleSheet = ""
228-
if (fontOrigin === "local") {
182+
if (cfg.theme.fontOrigin === "local") {
229183
// let the user do it themselves in css
230-
} else if (fontOrigin === "googleFonts") {
231-
if (cfg.theme.cdnCaching) {
232-
resources.css.push(googleFontHref(cfg.theme))
233-
} else {
234-
let match
235-
236-
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
237-
238-
googleFontsStyleSheet = await (
239-
await fetch(googleFontHref(ctx.cfg.configuration.theme))
240-
).text()
241-
242-
while ((match = fontSourceRegex.exec(googleFontsStyleSheet)) !== null) {
243-
// match[0] is the `url(path)`, match[1] is the `path`
244-
const url = match[1]
245-
// the static name of this file.
246-
const [filename, ext] = url.split("/").pop()!.split(".")
247-
248-
googleFontsStyleSheet = googleFontsStyleSheet.replace(
249-
url,
250-
`/static/fonts/${filename}.ttf`,
251-
)
252-
253-
promises.push(
254-
fetch(url)
255-
.then((res) => {
256-
if (!res.ok) {
257-
throw new Error(`Failed to fetch font`)
258-
}
259-
return res.arrayBuffer()
260-
})
261-
.then((buf) =>
262-
write({
263-
ctx,
264-
slug: joinSegments("static", "fonts", filename) as FullSlug,
265-
ext: `.${ext}`,
266-
content: Buffer.from(buf),
267-
}),
268-
),
269-
)
270-
}
184+
} else if (cfg.theme.fontOrigin === "googleFonts" && !cfg.theme.cdnCaching) {
185+
// when cdnCaching is true, we link to google fonts in Head.tsx
186+
let match
187+
188+
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
189+
190+
googleFontsStyleSheet = await (
191+
await fetch(googleFontHref(ctx.cfg.configuration.theme))
192+
).text()
193+
194+
while ((match = fontSourceRegex.exec(googleFontsStyleSheet)) !== null) {
195+
// match[0] is the `url(path)`, match[1] is the `path`
196+
const url = match[1]
197+
// the static name of this file.
198+
const [filename, ext] = url.split("/").pop()!.split(".")
199+
200+
googleFontsStyleSheet = googleFontsStyleSheet.replace(
201+
url,
202+
`https://${cfg.baseUrl}/static/fonts/${filename}.ttf`,
203+
)
204+
205+
promises.push(
206+
fetch(url)
207+
.then((res) => {
208+
if (!res.ok) {
209+
throw new Error(`Failed to fetch font`)
210+
}
211+
return res.arrayBuffer()
212+
})
213+
.then((buf) =>
214+
write({
215+
ctx,
216+
slug: joinSegments("static", "fonts", filename) as FullSlug,
217+
ext: `.${ext}`,
218+
content: Buffer.from(buf),
219+
}),
220+
),
221+
)
271222
}
272223
}
273224

274225
// important that this goes *after* component scripts
275226
// as the "nav" event gets triggered here and we should make sure
276227
// that everyone else had the chance to register a listener for it
277-
addGlobalPageResources(ctx, resources, componentResources)
228+
addGlobalPageResources(ctx, componentResources)
278229

279230
const stylesheet = joinStyles(
280231
ctx.cfg.configuration.theme,

quartz/plugins/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ export function getStaticResourcesFromPlugins(ctx: BuildCtx) {
1818
}
1919
}
2020

21+
// if serving locally, listen for rebuilds and reload the page
22+
if (ctx.argv.serve) {
23+
const wsUrl = ctx.argv.remoteDevHost
24+
? `wss://${ctx.argv.remoteDevHost}:${ctx.argv.wsPort}`
25+
: `ws://localhost:${ctx.argv.wsPort}`
26+
27+
staticResources.js.push({
28+
loadTime: "afterDOMReady",
29+
contentType: "inline",
30+
script: `
31+
const socket = new WebSocket('${wsUrl}')
32+
// reload(true) ensures resources like images and scripts are fetched again in firefox
33+
socket.addEventListener('message', () => document.location.reload(true))
34+
`,
35+
})
36+
}
37+
2138
return staticResources
2239
}
2340

quartz/plugins/transformers/description.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const Description: QuartzTransformerPlugin<Partial<Options> | undefined>
6161
const currentSentence = sentence.endsWith(".") ? sentence : sentence + "."
6262
finalDesc.push(currentSentence)
6363
currentDescriptionLength += currentSentence.length
64+
sentenceIdx++
6465
}
6566
}
6667

quartz/plugins/transformers/frontmatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ declare module "vfile" {
9090
description: string
9191
publish: boolean
9292
draft: boolean
93+
lang: string
9394
enableToc: string
9495
cssclasses: string[]
9596
}>

quartz/util/theme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Theme {
2222
}
2323
cdnCaching: boolean
2424
colors: Colors
25+
fontOrigin: "googleFonts" | "local"
2526
}
2627

2728
export type ThemeKey = keyof Colors

0 commit comments

Comments
 (0)