chore: conditional check

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2025-01-01 11:11:30 -08:00
parent a3e369be1f
commit 692a84d517
No known key found for this signature in database
GPG Key ID: 18974753009D2BFA
10 changed files with 28 additions and 48 deletions

View File

@ -274,7 +274,7 @@ export const ContentPage: QuartzEmitterPlugin = () => {
const allFiles = content.map((c) => c[1].data)
for (const [tree, file] of content) {
const slug = canonicalizeServer(file.data.slug!)
const externalResources = pageResources(slug, resources)
const externalResources = pageResources(slug, file, resources)
const componentData: QuartzComponentProps = {
fileData: file.data,
externalResources,

View File

@ -1,51 +1,11 @@
import { htmlToJsx } from "../../util/jsx"
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "../types"
// @ts-ignore
import mermaidScript from "../scripts/mermaid.inline"
import mermaidStyle from "../styles/mermaid.scss"
const Content: QuartzComponent = ({ fileData, tree }: QuartzComponentProps) => {
const content = htmlToJsx(fileData.filePath!, tree)
const classes: string[] = fileData.frontmatter?.cssclasses ?? []
const classString = ["popover-hint", ...classes].join(" ")
return (
<>
<article class={classString}>{content}</article>
<div id="mermaid-container">
<div class="mermaid-backdrop" />
<div id="mermaid-space">
<div class="mermaid-header">
<button
class="close-button"
aria-label="close button"
title="close button"
type="button"
>
<svg
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1={18} y1={6} x2={6} y2={18} />
<line x1={6} y1={6} x2={18} y2={18} />
</svg>
</button>
</div>
<div class="mermaid-content" />
</div>
</div>
</>
)
return <article class={classString}>{content}</article>
}
Content.afterDOMLoaded = mermaidScript
Content.css = mermaidStyle
export default (() => Content) satisfies QuartzComponentConstructor

View File

@ -8,6 +8,10 @@ import { visit } from "unist-util-visit"
import { Root, Element, ElementContent } from "hast"
import { GlobalConfiguration } from "../cfg"
import { i18n } from "../i18n"
// @ts-ignore
import mermaidScript from "./scripts/mermaid.inline"
import mermaidStyle from "./styles/mermaid.inline.scss"
import { QuartzPluginData } from "../plugins/vfile"
interface RenderComponents {
head: QuartzComponent
@ -23,12 +27,13 @@ interface RenderComponents {
const headerRegex = new RegExp(/h[1-6]/)
export function pageResources(
baseDir: FullSlug | RelativeURL,
fileData: QuartzPluginData,
staticResources: StaticResources,
): StaticResources {
const contentIndexPath = joinSegments(baseDir, "static/contentIndex.json")
const contentIndexScript = `const fetchData = fetch("${contentIndexPath}").then(data => data.json())`
return {
const resources: StaticResources = {
css: [
{
content: joinSegments(baseDir, "index.css"),
@ -56,6 +61,18 @@ export function pageResources(
},
],
}
if (fileData.hasMermaidDiagram) {
resources.js.push({
script: mermaidScript,
loadTime: "afterDOMReady",
moduleType: "module",
contentType: "inline",
})
resources.css.push({ content: mermaidStyle, inline: true })
}
return resources
}
export function renderPage(

View File

@ -158,6 +158,7 @@ document.addEventListener("nav", async () => {
)
mermaidImport ||= await import(
//@ts-ignore
"https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.4.0/mermaid.esm.min.mjs"
)
const mermaid = mermaidImport.default

View File

@ -37,7 +37,6 @@ export const NotFoundPage: QuartzEmitterPlugin = () => {
const url = new URL(`https://${cfg.baseUrl ?? "example.com"}`)
const path = url.pathname as FullSlug
const externalResources = pageResources(path, resources)
const notFound = i18n(cfg.locale).pages.error.title
const [tree, vfile] = defaultProcessedContent({
slug,
@ -45,6 +44,7 @@ export const NotFoundPage: QuartzEmitterPlugin = () => {
description: notFound,
frontmatter: { title: notFound, tags: [] },
})
const externalResources = pageResources(path, vfile.data, resources)
const componentData: QuartzComponentProps = {
ctx,
fileData: vfile.data,

View File

@ -106,7 +106,7 @@ export const ContentPage: QuartzEmitterPlugin<Partial<FullPageLayout>> = (userOp
containsIndex = true
}
const externalResources = pageResources(pathToRoot(slug), resources)
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
const componentData: QuartzComponentProps = {
ctx,
fileData: file.data,

View File

@ -106,8 +106,8 @@ export const FolderPage: QuartzEmitterPlugin<Partial<FolderPageOptions>> = (user
for (const folder of folders) {
const slug = joinSegments(folder, "index") as FullSlug
const externalResources = pageResources(pathToRoot(slug), resources)
const [tree, file] = folderDescriptions[folder]
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
const componentData: QuartzComponentProps = {
ctx,
fileData: file.data,

View File

@ -114,8 +114,8 @@ export const TagPage: QuartzEmitterPlugin<Partial<TagPageOptions>> = (userOpts)
for (const tag of tags) {
const slug = joinSegments("tags", tag) as FullSlug
const externalResources = pageResources(pathToRoot(slug), resources)
const [tree, file] = tagDescriptions[tag]
const externalResources = pageResources(pathToRoot(slug), file.data, resources)
const componentData: QuartzComponentProps = {
ctx,
fileData: file.data,

View File

@ -510,9 +510,10 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>
if (opts.mermaid) {
plugins.push(() => {
return (tree: Root, _file) => {
return (tree: Root, file) => {
visit(tree, "code", (node: Code) => {
if (node.lang === "mermaid") {
file.data.hasMermaidDiagram = true
node.data = {
hProperties: {
className: ["mermaid"],
@ -819,5 +820,6 @@ declare module "vfile" {
interface DataMap {
blocks: Record<string, Element>
htmlAst: HtmlRoot
hasMermaidDiagram: boolean
}
}