From e0ebee5aa9b3646de722f139f1d8d15591df538e Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 02 Jul 2023 20:08:29 +0000
Subject: [PATCH] various polish

---
 quartz/plugins/index.ts |   63 +++++++++++++------------------
 1 files changed, 27 insertions(+), 36 deletions(-)

diff --git a/quartz/plugins/index.ts b/quartz/plugins/index.ts
index 0378b1b..c55e4dd 100644
--- a/quartz/plugins/index.ts
+++ b/quartz/plugins/index.ts
@@ -1,11 +1,9 @@
 import { GlobalConfiguration } from '../cfg'
 import { QuartzComponent } from '../components/types'
 import { StaticResources } from '../resources'
-import { googleFontHref, joinStyles } from '../theme'
+import { joinStyles } from '../theme'
 import { EmitCallback, PluginTypes } from './types'
 import styles from '../styles/base.scss'
-// @ts-ignore
-import spaRouterScript from '../components/scripts/spa.inline'
 
 export type ComponentResources = {
   css: string[],
@@ -13,13 +11,7 @@
   afterDOMLoaded: string[]
 }
 
-function joinScripts(scripts: string[]): string {
-  // wrap with iife to prevent scope collision
-  return scripts.map(script => `(function () {${script}})();`).join("\n")
-}
-
-export function emitComponentResources(cfg: GlobalConfiguration, resources: StaticResources, plugins: PluginTypes, emit: EmitCallback) {
-  const fps: string[] = []
+export function getComponentResources(plugins: PluginTypes): ComponentResources {
   const allComponents: Set<QuartzComponent> = new Set()
   for (const emitter of plugins.emitters) {
     const components = emitter.getQuartzComponents()
@@ -47,35 +39,34 @@
     }
   }
 
-  if (cfg.enableSPA) {
-    componentResources.afterDOMLoaded.push(spaRouterScript)
-  } else {
-    componentResources.afterDOMLoaded.push(`
-      window.spaNavigate = (url, _) => window.location.assign(url)
-      const event = new CustomEvent("nav", { detail: { slug: document.body.dataset.slug } })
-      document.dispatchEvent(event)`
-    )
-  }
+  return componentResources
+}
 
-  emit({
-    slug: "index",
-    ext: ".css",
-    content: joinStyles(cfg.theme, styles, ...componentResources.css)
-  })
-  emit({
-    slug: "prescript",
-    ext: ".js",
-    content: joinScripts(componentResources.beforeDOMLoaded)
-  })
-  emit({
-    slug: "postscript",
-    ext: ".js",
-    content: joinScripts(componentResources.afterDOMLoaded)
-  })
+function joinScripts(scripts: string[]): string {
+  // wrap with iife to prevent scope collision
+  return scripts.map(script => `(function () {${script}})();`).join("\n")
+}
 
-  fps.push("index.css", "prescript.js", "postscript.js")
-  resources.css.push(googleFontHref(cfg.theme))
+export async function emitComponentResources(cfg: GlobalConfiguration, res: ComponentResources, emit: EmitCallback): Promise<string[]> {
+  const fps = await Promise.all([
+    emit({
+      slug: "index",
+      ext: ".css",
+      content: joinStyles(cfg.theme, styles, ...res.css)
+    }),
+    emit({
+      slug: "prescript",
+      ext: ".js",
+      content: joinScripts(res.beforeDOMLoaded)
+    }),
+    emit({
+      slug: "postscript",
+      ext: ".js",
+      content: joinScripts(res.afterDOMLoaded)
+    })
+  ])
   return fps
+
 }
 
 export function getStaticResourcesFromPlugins(plugins: PluginTypes) {

--
Gitblit v1.10.0