Jacky Zhao
2024-02-05 06ee73e006fedef64d326aae8909bcc4ca5b243a
quartz/components/renderPage.tsx
@@ -7,6 +7,8 @@
import { visit } from "unist-util-visit"
import { Root, Element, ElementContent } from "hast"
import { QuartzPluginData } from "../plugins/vfile"
import { GlobalConfiguration } from "../cfg"
import { i18n } from "../i18n"
interface RenderComponents {
  head: QuartzComponent
@@ -23,7 +25,7 @@
  staticResources: StaticResources,
): StaticResources {
  const contentIndexPath = joinSegments(baseDir, "static/contentIndex.json")
  const contentIndexScript = `const fetchData = fetch(\`${contentIndexPath}\`).then(data => data.json())`
  const contentIndexScript = `const fetchData = fetch("${contentIndexPath}").then(data => data.json())`
  return {
    css: [joinSegments(baseDir, "index.css"), ...staticResources.css],
@@ -63,6 +65,7 @@
}
export function renderPage(
  cfg: GlobalConfiguration,
  slug: FullSlug,
  componentData: QuartzComponentProps,
  components: RenderComponents,
@@ -74,13 +77,13 @@
      const classNames = (node.properties?.className ?? []) as string[]
      if (classNames.includes("transclude")) {
        const inner = node.children[0] as Element
        const transcludeTarget = inner.properties?.["data-slug"] as FullSlug
        const transcludeTarget = inner.properties["data-slug"] as FullSlug
        const page = getOrComputeFileIndex(componentData.allFiles).get(transcludeTarget)
        if (!page) {
          return
        }
        let blockRef = node.properties?.dataBlock as string | undefined
        let blockRef = node.properties.dataBlock as string | undefined
        if (blockRef?.startsWith("#^")) {
          // block transclude
          blockRef = blockRef.slice("#^".length)
@@ -90,6 +93,7 @@
              blockNode = {
                type: "element",
                tagName: "ul",
                properties: {},
                children: [blockNode],
              }
            }
@@ -135,7 +139,9 @@
              type: "element",
              tagName: "a",
              properties: { href: inner.properties?.href, class: ["internal"] },
              children: [{ type: "text", value: `Link to original` }],
              children: [
                { type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal },
              ],
            },
          ]
        } else if (page.htmlAst) {
@@ -144,8 +150,16 @@
            {
              type: "element",
              tagName: "h1",
              properties: {},
              children: [
                { type: "text", value: page.frontmatter?.title ?? `Transclude of ${page.slug}` },
                {
                  type: "text",
                  value:
                    page.frontmatter?.title ??
                    i18n(cfg.locale).components.transcludes.transcludeOf({
                      targetSlug: page.slug!,
                    }),
                },
              ],
            },
            ...(page.htmlAst.children as ElementContent[]).map((child) =>
@@ -155,7 +169,9 @@
              type: "element",
              tagName: "a",
              properties: { href: inner.properties?.href, class: ["internal"] },
              children: [{ type: "text", value: `Link to original` }],
              children: [
                { type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal },
              ],
            },
          ]
        }