From fd5c8d17d3b3cd0a13b0a21a2b78d85010e02e12 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Tue, 20 Jun 2023 03:37:45 +0000
Subject: [PATCH] basic search implementation

---
 quartz/plugins/index.ts |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/quartz/plugins/index.ts b/quartz/plugins/index.ts
index 6d5d840..0378b1b 100644
--- a/quartz/plugins/index.ts
+++ b/quartz/plugins/index.ts
@@ -4,6 +4,7 @@
 import { googleFontHref, 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 = {
@@ -13,7 +14,8 @@
 }
 
 function joinScripts(scripts: string[]): string {
-  return scripts.join("\n")
+  // 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) {
@@ -32,10 +34,6 @@
     afterDOMLoaded: []
   }
 
-  if (cfg.enableSPA) {
-    componentResources.afterDOMLoaded.push(spaRouterScript)
-  }
-
   for (const component of allComponents) {
     const { css, beforeDOMLoaded, afterDOMLoaded } = component
     if (css) {
@@ -49,6 +47,16 @@
     }
   }
 
+  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)`
+    )
+  }
+
   emit({
     slug: "index",
     ext: ".css",
@@ -77,7 +85,7 @@
   }
 
   for (const transformer of plugins.transformers) {
-    const res = transformer.externalResources
+    const res = transformer.externalResources ? transformer.externalResources() : {}
     if (res?.js) {
       staticResources.js = staticResources.js.concat(res.js)
     }

--
Gitblit v1.10.0