Skip to content

Commit

Permalink
font loading options, optimize css
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Jul 25, 2023
1 parent e82ba97 commit c0278a8
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 38 deletions.
1 change: 1 addition & 0 deletions content/features/upcoming features.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ draft: true

## high priority

- local fonts
- images in same folder are broken on shortest path mode
- https://help.obsidian.md/Editing+and+formatting/Tags#Nested+tags nested tags?? and big tag listing
- watch mode for config/source code
Expand Down
189 changes: 189 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"hast-util-to-string": "^2.0.0",
"is-absolute-url": "^4.0.1",
"js-yaml": "^4.1.0",
"lightningcss": "^1.21.5",
"mdast-util-find-and-replace": "^2.2.2",
"mdast-util-to-string": "^3.2.0",
"micromorph": "^0.4.5",
Expand Down
2 changes: 1 addition & 1 deletion quartz.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const config: QuartzConfig = {
filters: [Plugin.RemoveDrafts()],
emitters: [
Plugin.AliasRedirects(),
Plugin.ComponentResources(),
Plugin.ComponentResources({ fontOrigin: "googleFonts" }),
Plugin.ContentPage({
...sharedPageComponents,
...contentPageLayout,
Expand Down
2 changes: 2 additions & 0 deletions quartz/bootstrap-cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { intro, isCancel, outro, select, text } from "@clack/prompts"
import { rimraf } from "rimraf"
import prettyBytes from "pretty-bytes"
import { spawnSync } from "child_process"
import { transform } from "lightningcss"

const UPSTREAM_NAME = "upstream"
const QUARTZ_SOURCE_BRANCH = "v4-alpha"
Expand Down Expand Up @@ -302,6 +303,7 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
plugins: [
sassPlugin({
type: "css-text",
cssImports: true,
}),
{
name: "inline-script-loader",
Expand Down
94 changes: 60 additions & 34 deletions quartz/plugins/emitters/componentResources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FilePath, ServerSlug } from "../../path"
import { PluginTypes, QuartzEmitterPlugin } from "../types"
import { QuartzEmitterPlugin } from "../types"

// @ts-ignore
import spaRouterScript from "../../components/scripts/spa.inline"
Expand All @@ -13,6 +13,7 @@ import { BuildCtx } from "../../ctx"
import { StaticResources } from "../../resources"
import { QuartzComponent } from "../../components/types"
import { googleFontHref, joinStyles } from "../../theme"
import { transform } from "lightningcss"

type ComponentResources = {
css: string[]
Expand Down Expand Up @@ -67,7 +68,6 @@ function addGlobalPageResources(
) {
const cfg = ctx.cfg.configuration
const reloadScript = ctx.argv.serve
staticResources.css.push(googleFontHref(cfg.theme))

// popovers
if (cfg.enablePopovers) {
Expand Down Expand Up @@ -120,35 +120,61 @@ function addGlobalPageResources(
}
}

export const ComponentResources: QuartzEmitterPlugin = () => ({
name: "ComponentResources",
getQuartzComponents() {
return []
},
async emit(ctx, _content, resources, emit): Promise<FilePath[]> {
// component specific scripts and styles
const componentResources = getComponentResources(ctx)
// important that this goes *after* component scripts
// as the "nav" event gets triggered here and we should make sure
// that everyone else had the chance to register a listener for it
addGlobalPageResources(ctx, resources, componentResources)
const fps = await Promise.all([
emit({
slug: "index" as ServerSlug,
ext: ".css",
content: joinStyles(ctx.cfg.configuration.theme, styles, ...componentResources.css),
}),
emit({
slug: "prescript" as ServerSlug,
ext: ".js",
content: joinScripts(componentResources.beforeDOMLoaded),
}),
emit({
slug: "postscript" as ServerSlug,
ext: ".js",
content: joinScripts(componentResources.afterDOMLoaded),
}),
])
return fps
},
})
interface Options {
fontOrigin: "googleFonts" | "local"
}

const defaultOptions: Options = {
fontOrigin: "googleFonts",
}

export const ComponentResources: QuartzEmitterPlugin<Options> = (opts?: Partial<Options>) => {
const { fontOrigin } = { ...defaultOptions, ...opts }
return {
name: "ComponentResources",
getQuartzComponents() {
return []
},
async emit(ctx, _content, resources, emit): Promise<FilePath[]> {
// component specific scripts and styles
const componentResources = getComponentResources(ctx)
// important that this goes *after* component scripts
// as the "nav" event gets triggered here and we should make sure
// that everyone else had the chance to register a listener for it

if (fontOrigin === "googleFonts") {
resources.css.push(googleFontHref(ctx.cfg.configuration.theme))
} else if (fontOrigin === "local") {
// let the user do it themselves in css
}

addGlobalPageResources(ctx, resources, componentResources)

const stylesheet = joinStyles(ctx.cfg.configuration.theme, styles, ...componentResources.css)
const prescript = joinScripts(componentResources.beforeDOMLoaded)
const postscript = joinScripts(componentResources.afterDOMLoaded)
const fps = await Promise.all([
emit({
slug: "index" as ServerSlug,
ext: ".css",
content: transform({
filename: "index.css",
code: Buffer.from(stylesheet),
minify: true
}).code.toString(),
}),
emit({
slug: "prescript" as ServerSlug,
ext: ".js",
content: prescript,
}),
emit({
slug: "postscript" as ServerSlug,
ext: ".js",
content: postscript,
}),
])
return fps
},
}
}
2 changes: 1 addition & 1 deletion quartz/styles/base.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@use "./custom.scss";
@use "./syntax.scss";
@use "./callouts.scss";
@use "./custom.scss";
@use "./variables.scss" as *;

html {
Expand Down
Loading

0 comments on commit c0278a8

Please sign in to comment.