mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
fix(fonts): explicit body, code and interface fonts apply with an Obsidian theme (Obsidian's --font-*-override variables on body)
This commit is contained in:
3 files changed
+53
-4
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
Fonts: explicitly configured `body`, `code` and `interface` fonts now apply with an Obsidian theme. They are also written to Obsidian's `--font-*-override` variables on `body`, which the theme's styles read; before, the theme's own `body` font variables won over Quartz's `:root` ones.
|
||||
@@ -642,4 +642,26 @@ describe("fonts built-in", () => {
|
||||
delete (globalThis as Record<string, unknown>).__quartzFonts
|
||||
}
|
||||
})
|
||||
|
||||
test("explicit body/interface/code fonts set Obsidian's overrides on body", () => {
|
||||
;(globalThis as Record<string, unknown>).__quartzFonts = {
|
||||
themeName: "Things",
|
||||
fonts: { "--font-text": "Inter", "--font-monospace": "JetBrains Mono" },
|
||||
}
|
||||
try {
|
||||
const opts = { body: "Lora", code: "Fira Code" }
|
||||
const css = buildUnlayeredCSS(resolveFonts(opts, {}), opts, true)
|
||||
assert.match(css, /html body \{[^}]*--font-text-override: Lora;/)
|
||||
assert.match(css, /html body \{[^}]*--font-monospace-override: Fira Code;/)
|
||||
assert.doesNotMatch(css, /--font-interface-override/)
|
||||
// a theme with themeFonts: false registers no fonts, but still reads the overrides
|
||||
assert.match(
|
||||
buildUnlayeredCSS(resolveFonts({ interface: "Inter" }, {}), { interface: "Inter" }, false),
|
||||
/html body \{\n --font-interface-override: Inter;\n\}/,
|
||||
)
|
||||
assert.doesNotMatch(buildUnlayeredCSS(resolveFonts({}, {}), {}, true), /override/)
|
||||
} finally {
|
||||
delete (globalThis as Record<string, unknown>).__quartzFonts
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -310,15 +310,37 @@ export function buildLayeredCSS(fonts: ResolvedFonts, options: FontsConfig, them
|
||||
].join("\n")
|
||||
}
|
||||
|
||||
/** Unlayered heading fonts beat every layer; with a theme, only explicitly configured ones. */
|
||||
/** Obsidian's override variables for the body, interface and code roles. */
|
||||
const OBSIDIAN_OVERRIDES: [string, "body" | "interface" | "code"][] = [
|
||||
["--font-text-override", "body"],
|
||||
["--font-interface-override", "interface"],
|
||||
["--font-monospace-override", "code"],
|
||||
]
|
||||
|
||||
/**
|
||||
* Unlayered heading fonts beat every layer; with a theme, only explicitly configured ones.
|
||||
*
|
||||
* Explicitly configured body/interface/code fonts also go to Obsidian's `--font-*-override`
|
||||
* variables on `body`: an Obsidian theme's app.css sets `--font-text` and friends on `body`
|
||||
* itself, so the `:root` values from {@link buildLayeredCSS} never reach the page. This holds
|
||||
* whether or not the theme registers its fonts (with `themeFonts: false` it maps the overrides
|
||||
* to Quartz's font variables on `body`), so the selector is `html body`, which wins over a
|
||||
* theme's `body` rule regardless of stylesheet order. Without an Obsidian theme nothing reads
|
||||
* the overrides.
|
||||
*/
|
||||
export function buildUnlayeredCSS(
|
||||
fonts: ResolvedFonts,
|
||||
options: FontsConfig,
|
||||
themeActive: boolean,
|
||||
) {
|
||||
return HEADING_LEVELS.filter((l) => !themeActive || isExplicit(options, l))
|
||||
.map((l) => `${l} { font-family: ${fonts[l]}; }`)
|
||||
.join("\n")
|
||||
const rules = HEADING_LEVELS.filter((l) => !themeActive || isExplicit(options, l)).map(
|
||||
(l) => `${l} { font-family: ${fonts[l]}; }`,
|
||||
)
|
||||
const overrides = OBSIDIAN_OVERRIDES.filter(([, key]) => options[key] !== undefined).map(
|
||||
([name, key]) => ` ${name}: ${fonts[key]};`,
|
||||
)
|
||||
if (overrides.length > 0) rules.push(["html body {", ...overrides, "}"].join("\n"))
|
||||
return rules.join("\n")
|
||||
}
|
||||
|
||||
// ---- plugin ------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user