Striven
yesterday c538c151c7462ad0395ff2c15c5e11e89e362aa8
quartz/util/resources.tsx
@@ -1,5 +1,6 @@
import { randomUUID } from "crypto"
import { JSX } from "preact/jsx-runtime"
import { QuartzPluginData } from "../plugins/vfile"
export type JSResource = {
  loadTime: "beforeDOMReady" | "afterDOMReady"
@@ -16,12 +17,19 @@
    }
)
export type CSSResource = {
  content: string
  inline?: boolean
  spaPreserve?: boolean
}
export function JSResourceToScriptElement(resource: JSResource, preserve?: boolean): JSX.Element {
  const scriptType = resource.moduleType ?? "application/javascript"
  const spaPreserve = preserve ?? resource.spaPreserve
  if (resource.contentType === "external") {
    return (
      <script key={resource.src} src={resource.src} type={scriptType} spa-preserve={spaPreserve} />
      <script key={resource.src} src={resource.src} type={scriptType} data-persist={spaPreserve} />
    )
  } else {
    const content = resource.script
@@ -29,14 +37,39 @@
      <script
        key={randomUUID()}
        type={scriptType}
        spa-preserve={spaPreserve}
        data-persist={spaPreserve}
        dangerouslySetInnerHTML={{ __html: content }}
      ></script>
    )
  }
}
export function CSSResourceToStyleElement(resource: CSSResource, preserve?: boolean): JSX.Element {
  const spaPreserve = preserve ?? resource.spaPreserve
  if (resource.inline ?? false) {
    return <style>{resource.content}</style>
  } else {
    return (
      <link
        key={resource.content}
        href={resource.content}
        rel="stylesheet"
        type="text/css"
        data-persist={spaPreserve}
      />
    )
  }
}
export interface StaticResources {
  css: string[]
  css: CSSResource[]
  js: JSResource[]
  additionalHead: (JSX.Element | ((pageData: QuartzPluginData) => JSX.Element))[]
}
export type StringResource = string | string[] | undefined
export function concatenateResources(...resources: StringResource[]): StringResource {
  return resources
    .filter((resource): resource is string | string[] => resource !== undefined)
    .flat()
}