fix(fonts): load the interface and per-heading fonts; single-weight fonts request their weight

This commit is contained in:
saberzero1 committed 2026-11-22 21:25:05 +01:00
1 parent 3f8104fb50
commit c392e177a7
3 files changed
+61 -17

No files matched your search

@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Fonts: the `interface` font and per-heading fonts (`h1` … `h6`) are now loaded from Google Fonts (or self-hosted with `fontOrigin: selfHosted`) like the other fonts; before, they only rendered if installed locally. A font with a single weight now loads that weight (`weights: [700]` loaded 400), and a single-weight italic font no longer makes the Google Fonts request fail.
+32 -1
View File
@@ -14,7 +14,13 @@ import {
readFrontmatterData,
} from "./frontmatter"
import { Links } from "./links"
import { buildLayeredCSS, buildUnlayeredCSS, resolveFonts } from "./fonts"
import {
buildLayeredCSS,
buildUnlayeredCSS,
googleFontHref,
resolveFontSpecs,
resolveFonts,
} from "./fonts"
import { isContentError } from "../../util/pluginErrors"
import { Drafts, isDraft } from "./drafts"
import {
@@ -643,6 +649,31 @@ describe("fonts built-in", () => {
}
})
test("the interface and per-heading fonts are loaded from Google Fonts", () => {
const href = decodeURIComponent(
googleFontHref(
resolveFontSpecs(
{ interface: "Inter", h1: { name: "Playfair Display", weights: [700] } },
{},
),
),
)
assert.match(href, /family=Inter:wght@400;600/)
assert.match(href, /family=Playfair Display:wght@700/)
assert.doesNotMatch(decodeURIComponent(googleFontHref(resolveFontSpecs({}, {}))), /Inter/)
const single = (spec: object) =>
decodeURIComponent(googleFontHref(resolveFontSpecs({ title: spec as never }, {})))
assert.match(single({ name: "Lora", weights: [400] }), /family=Lora&/)
assert.match(
single({ name: "Lora", weights: [400], includeItalic: true }),
/family=Lora:ital@0;1&/,
)
assert.match(
single({ name: "Lora", weights: [700], includeItalic: true }),
/family=Lora:ital,wght@0,700;1,700&/,
)
})
test("explicit body/interface/code fonts set Obsidian's overrides on body", () => {
;(globalThis as Record<string, unknown>).__quartzFonts = {
themeName: "Things",
+24 -16
View File
@@ -23,7 +23,7 @@ import { QuartzEmitterPlugin } from "../types"
const HEADING_LEVELS = ["h1", "h2", "h3", "h4", "h5", "h6"] as const
type HeadingLevel = (typeof HEADING_LEVELS)[number]
type FontRole = "title" | "header" | "body" | "code"
type FontRole = "title" | "header" | "body" | "code" | "interface"
export const SELF_HOSTED_STYLESHEET = "static/fonts/quartz-fonts.css"
export const QUARTZ_DEFAULT_HEADER = "Schibsted Grotesk"
@@ -40,12 +40,14 @@ const DEFAULT_WEIGHTS: Record<FontRole, number[]> = {
header: [400, 700],
body: [400, 600],
code: [400, 600],
interface: [400, 600],
}
const DEFAULT_ITALIC: Record<FontRole, boolean> = {
title: false,
header: false,
body: true,
code: false,
interface: false,
}
/** Option defaults; every key is listed so unknown options can be reported. */
@@ -107,13 +109,20 @@ export function readConfiguredTypography(ctx: BuildCtx | undefined): ConfiguredT
return result
}
/** Options first, then `configuration.theme.typography`, then Quartz's defaults. */
/**
* Options first, then `configuration.theme.typography`, then Quartz's defaults. The interface
* and per-heading fonts exist only as options.
*/
export function resolveFontSpecs(options: FontsConfig, typography: ConfiguredTypography) {
return {
title: options.title ?? typography.title,
header: options.header ?? typography.header ?? QUARTZ_DEFAULT_HEADER,
body: options.body ?? typography.body ?? QUARTZ_DEFAULT_BODY,
code: options.code ?? typography.code ?? QUARTZ_DEFAULT_CODE,
interface: options.interface,
headings: HEADING_LEVELS.map((l) => options[l]).filter(
(spec): spec is FontSpecification => spec !== undefined,
),
}
}
@@ -140,20 +149,16 @@ function mergeInto(map: Map<string, MergedFont>, role: FontRole, spec: FontSpeci
}
}
/** Google Fonts css2 `family` value; a single weight still names its axis unless it's 400. */
function formatMerged(entry: MergedFont): string {
const weights = [...entry.weights].sort((a, b) => a - b)
const features: string[] = []
if (entry.italic) features.push("ital")
if (weights.length > 1) {
const spec = entry.italic
? weights
.flatMap((w) => [`0,${w}`, `1,${w}`])
.sort()
.join(";")
: weights.join(";")
features.push(`wght@${spec}`)
const defaultWeight = weights.length === 1 && weights[0] === 400
if (entry.italic) {
if (defaultWeight) return `${entry.name}:ital@0;1`
const tuples = [...weights.map((w) => `0,${w}`), ...weights.map((w) => `1,${w}`)]
return `${entry.name}:ital,wght@${tuples.join(";")}`
}
return features.length > 0 ? `${entry.name}:${features.join(",")}` : entry.name
return defaultWeight ? entry.name : `${entry.name}:wght@${weights.join(";")}`
}
export function googleFontHref(fonts: ReturnType<typeof resolveFontSpecs>): string {
@@ -162,6 +167,8 @@ export function googleFontHref(fonts: ReturnType<typeof resolveFontSpecs>): stri
mergeInto(merged, "body", fonts.body)
mergeInto(merged, "code", fonts.code)
if (fonts.title) mergeInto(merged, "title", fonts.title)
if (fonts.interface) mergeInto(merged, "interface", fonts.interface)
for (const spec of fonts.headings) mergeInto(merged, "header", spec)
const params = [...merged.values()]
.map(formatMerged)
.map((f) => `family=${encodeURIComponent(f)}`)
@@ -218,6 +225,7 @@ function validate(options: FontsConfig) {
check("header", options.header, "header")
check("body", options.body, "body")
check("code", options.code, "code")
check("interface", options.interface, "interface")
for (const level of HEADING_LEVELS) check(level, options[level], "header")
}
@@ -367,9 +375,9 @@ export const Fonts: QuartzEmitterPlugin<FontsConfig> = (userOpts) => {
const theme = ctx?.cfg?.configuration?.theme as
{ cdnCaching?: boolean; fontOrigin?: string } | undefined
const headLinksTypography = theme?.cdnCaching === true && theme?.fontOrigin === "googleFonts"
const overridesTypography = (["title", "header", "body", "code"] as const).some(
(k) => options[k] !== undefined,
)
const overridesTypography = (
["title", "header", "body", "code", "interface", ...HEADING_LEVELS] as const
).some((k) => options[k] !== undefined)
if (options.fontOrigin === "googleFonts" && (!headLinksTypography || overridesTypography)) {
const href = googleFontHref(resolveFontSpecs(options, typography))
additionalHead = [