From 74777118a7fd19e4a296706c2a4b5fdca546c4fa Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 13 Nov 2023 22:51:40 -0800 Subject: [PATCH 01/10] feat: header and full-page transcludes (closes #557) --- quartz/components/renderPage.tsx | 81 ++++++++++++++++++++++++++---- quartz/plugins/transformers/ofm.ts | 8 +-- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/quartz/components/renderPage.tsx b/quartz/components/renderPage.tsx index 451813b5e..36b06464f 100644 --- a/quartz/components/renderPage.tsx +++ b/quartz/components/renderPage.tsx @@ -5,7 +5,7 @@ import BodyConstructor from "./Body" import { JSResourceToScriptElement, StaticResources } from "../util/resources" import { FullSlug, RelativeURL, joinSegments } from "../util/path" import { visit } from "unist-util-visit" -import { Root, Element } from "hast" +import { Root, Element, ElementContent } from "hast" interface RenderComponents { head: QuartzComponent @@ -61,22 +61,81 @@ export function renderPage( const classNames = (node.properties?.className ?? []) as string[] if (classNames.includes("transclude")) { const inner = node.children[0] as Element - const blockSlug = inner.properties?.["data-slug"] as FullSlug - const blockRef = node.properties!.dataBlock as string + const transcludeTarget = inner.properties?.["data-slug"] as FullSlug // TODO: avoid this expensive find operation and construct an index ahead of time - let blockNode = componentData.allFiles.find((f) => f.slug === blockSlug)?.blocks?.[blockRef] - if (blockNode) { - if (blockNode.tagName === "li") { - blockNode = { - type: "element", - tagName: "ul", - children: [blockNode], + const page = componentData.allFiles.find((f) => f.slug === transcludeTarget) + if (!page) { + return + } + + let blockRef = node.properties?.dataBlock as string | undefined + if (blockRef?.startsWith("^")) { + // block transclude + blockRef = blockRef.slice(1) + let blockNode = page.blocks?.[blockRef] + if (blockNode) { + if (blockNode.tagName === "li") { + blockNode = { + type: "element", + tagName: "ul", + children: [blockNode], + } + } + + node.children = [ + blockNode, + { + type: "element", + tagName: "a", + properties: { href: inner.properties?.href, class: ["internal"] }, + children: [{ type: "text", value: `Link to original` }], + }, + ] + } + } else if (blockRef?.startsWith("#") && page.htmlAst) { + // header transclude + blockRef = blockRef.slice(1) + let startIdx = undefined + let endIdx = undefined + for (const [i, el] of page.htmlAst.children.entries()) { + if (el.type === "element" && el.tagName.match(/h[1-6]/)) { + if (endIdx) { + break + } + + if (startIdx) { + endIdx = i + } else if (el.properties?.id === blockRef) { + startIdx = i + } } } + if (!startIdx) { + return + } + node.children = [ - blockNode, + ...(page.htmlAst.children.slice(startIdx, endIdx) as ElementContent[]), + { + type: "element", + tagName: "a", + properties: { href: inner.properties?.href, class: ["internal"] }, + children: [{ type: "text", value: `Link to original` }], + }, + ] + } else if (page.htmlAst) { + // page transclude + node.children = [ + { + type: "element", + tagName: "h1", + children: [ + { type: "text", value: page.frontmatter?.title ?? `Transclude of ${page.slug}` }, + ], + }, + ...(page.htmlAst.children as ElementContent[]), { type: "element", tagName: "a", diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 226e9394e..50c4d5c64 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -1,7 +1,7 @@ import { PluggableList } from "unified" import { QuartzTransformerPlugin } from "../types" import { Root, HTML, BlockContent, DefinitionContent, Code, Paragraph } from "mdast" -import { Element, Literal } from "hast" +import { Element, Literal, Root as HtmlRoot } from "hast" import { Replace, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { slug as slugAnchor } from "github-slugger" import rehypeRaw from "rehype-raw" @@ -236,13 +236,13 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin value: ``, } } else if (ext === "") { - const block = anchor.slice(1) + const block = anchor return { type: "html", data: { hProperties: { transclude: true } }, value: `
Transclude of block ${block}
`, + }" class="transclude-inner">Transclude of ${url}${block}`, } } @@ -436,6 +436,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin const blockTagTypes = new Set(["blockquote"]) return (tree, file) => { file.data.blocks = {} + file.data.htmlAst = tree visit(tree, "element", (node, index, parent) => { if (blockTagTypes.has(node.tagName)) { @@ -524,5 +525,6 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin declare module "vfile" { interface DataMap { blocks: Record + htmlAst: HtmlRoot } } From 76f2664277e07a7d1b011fac236840c6e8e69fdd Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 13 Nov 2023 22:57:05 -0800 Subject: [PATCH 02/10] versioning: bump to v4.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3fa8c2315..aa6324366 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@jackyzha0/quartz", "description": "🌱 publish your digital garden and notes as a website", "private": true, - "version": "4.1.0", + "version": "4.1.1", "type": "module", "author": "jackyzha0 ", "license": "MIT", From 2de48b267a8f2d6ed0461d2febc266559c2d8d47 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Tue, 14 Nov 2023 20:01:48 -0800 Subject: [PATCH 03/10] fix: set htmlAst after walking tree in ofm (closes #589) --- quartz/plugins/transformers/ofm.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 50c4d5c64..a8399d011 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -436,7 +436,6 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin const blockTagTypes = new Set(["blockquote"]) return (tree, file) => { file.data.blocks = {} - file.data.htmlAst = tree visit(tree, "element", (node, index, parent) => { if (blockTagTypes.has(node.tagName)) { @@ -478,6 +477,8 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin } } }) + + file.data.htmlAst = tree } }) } From 8fc7b9f4c68bb8c41a156e92cfbddf3144678dcc Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 09:43:30 -0800 Subject: [PATCH 04/10] feat: deref symlinks when copying static assets (closes #588) --- quartz/plugins/emitters/static.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/quartz/plugins/emitters/static.ts b/quartz/plugins/emitters/static.ts index 6f5d19d41..f0118e2e8 100644 --- a/quartz/plugins/emitters/static.ts +++ b/quartz/plugins/emitters/static.ts @@ -11,7 +11,10 @@ export const Static: QuartzEmitterPlugin = () => ({ async emit({ argv, cfg }, _content, _resources, _emit): Promise { const staticPath = joinSegments(QUARTZ, "static") const fps = await glob("**", staticPath, cfg.configuration.ignorePatterns) - await fs.promises.cp(staticPath, joinSegments(argv.output, "static"), { recursive: true }) + await fs.promises.cp(staticPath, joinSegments(argv.output, "static"), { + recursive: true, + dereference: true, + }) return fps.map((fp) => joinSegments(argv.output, "static", fp)) as FilePath[] }, }) From 06426c8f7e87e9476e3ede9f9d15771db601e3e6 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 19:27:54 -0800 Subject: [PATCH 05/10] feat: support repeated anchor tag (closes #592) --- quartz/plugins/transformers/ofm.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index a8399d011..9ae99228c 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -110,7 +110,7 @@ function canonicalizeCallout(calloutName: string): keyof typeof callouts { // ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name) // (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link) // (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias) -const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)?(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g") +const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g") const highlightRegex = new RegExp(/==([^=]+)==/, "g") const commentRegex = new RegExp(/%%(.+)%%/, "g") // from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts @@ -178,9 +178,9 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin src = src.replaceAll(wikilinkRegex, (value, ...capture) => { const [rawFp, rawHeader, rawAlias] = capture const fp = rawFp ?? "" - const anchor = rawHeader?.trim().slice(1) + const anchor = rawHeader?.trim().replace(/^#+/, "") const displayAnchor = anchor ? `#${slugAnchor(anchor)}` : "" - const displayAlias = rawAlias ?? rawHeader?.replace("#", "|") ?? "" + const displayAlias = rawAlias ?? anchor ?? "" const embedDisplay = value.startsWith("!") ? "!" : "" return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]` }) From f861a7c160a0b3be76a16c5bf2ea1a8d076a9662 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 19:31:18 -0800 Subject: [PATCH 06/10] fix: regression where clicking anchors on the same page wouldn't set the anchor in the url --- quartz/components/scripts/spa.inline.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quartz/components/scripts/spa.inline.ts b/quartz/components/scripts/spa.inline.ts index 0318ec348..bc13c9316 100644 --- a/quartz/components/scripts/spa.inline.ts +++ b/quartz/components/scripts/spa.inline.ts @@ -15,7 +15,7 @@ const isLocalUrl = (href: string) => { if (window.location.origin === url.origin) { return true } - } catch (e) {} + } catch (e) { } return false } @@ -109,6 +109,7 @@ function createRouter() { if (isSamePage(url) && url.hash) { const el = document.getElementById(decodeURIComponent(url.hash.substring(1))) el?.scrollIntoView() + history.pushState({}, "", url) return } From 5befcf4780a9f1c2872dead5d25100d47452ea1e Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 19:32:25 -0800 Subject: [PATCH 07/10] fix: format --- quartz/components/scripts/spa.inline.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quartz/components/scripts/spa.inline.ts b/quartz/components/scripts/spa.inline.ts index bc13c9316..c0152b52d 100644 --- a/quartz/components/scripts/spa.inline.ts +++ b/quartz/components/scripts/spa.inline.ts @@ -15,7 +15,7 @@ const isLocalUrl = (href: string) => { if (window.location.origin === url.origin) { return true } - } catch (e) { } + } catch (e) {} return false } From a26eb59392c617bf567df23ee45d16b3fce69da3 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 20:13:28 -0800 Subject: [PATCH 08/10] feat: scrub link formatting from toc entries --- quartz/plugins/transformers/ofm.ts | 5 ++++- quartz/plugins/transformers/toc.ts | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 9ae99228c..e9510bb2f 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -110,7 +110,10 @@ function canonicalizeCallout(calloutName: string): keyof typeof callouts { // ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name) // (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link) // (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias) -const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g") +export const wikilinkRegex = new RegExp( + /!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, + "g", +) const highlightRegex = new RegExp(/==([^=]+)==/, "g") const commentRegex = new RegExp(/%%(.+)%%/, "g") // from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts diff --git a/quartz/plugins/transformers/toc.ts b/quartz/plugins/transformers/toc.ts index 87c5802ff..d0781ec20 100644 --- a/quartz/plugins/transformers/toc.ts +++ b/quartz/plugins/transformers/toc.ts @@ -3,6 +3,7 @@ import { Root } from "mdast" import { visit } from "unist-util-visit" import { toString } from "mdast-util-to-string" import Slugger from "github-slugger" +import { wikilinkRegex } from "./ofm" export interface Options { maxDepth: 1 | 2 | 3 | 4 | 5 | 6 @@ -24,6 +25,7 @@ interface TocEntry { slug: string // this is just the anchor (#some-slug), not the canonical slug } +const regexMdLinks = new RegExp(/\[([^\[]+)\](\(.*\))/, "g") export const TableOfContents: QuartzTransformerPlugin | undefined> = ( userOpts, ) => { @@ -41,7 +43,16 @@ export const TableOfContents: QuartzTransformerPlugin | undefin let highestDepth: number = opts.maxDepth visit(tree, "heading", (node) => { if (node.depth <= opts.maxDepth) { - const text = toString(node) + let text = toString(node) + + // strip link formatting from toc entries + text = text.replace(wikilinkRegex, (_, rawFp, __, rawAlias) => { + const fp = rawFp?.trim() ?? "" + const alias = rawAlias?.slice(1).trim() + return alias ?? fp + }) + text = text.replace(regexMdLinks, "$1") + highestDepth = Math.min(highestDepth, node.depth) toc.push({ depth: node.depth, From 95b1141b9df7b8de3fdb893454326daf752d7f66 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Wed, 15 Nov 2023 20:35:45 -0800 Subject: [PATCH 09/10] fix: include anchor when normalizing urls for spa/popovers --- quartz/components/scripts/popover.inline.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/quartz/components/scripts/popover.inline.ts b/quartz/components/scripts/popover.inline.ts index 9506ec412..2bd21d1eb 100644 --- a/quartz/components/scripts/popover.inline.ts +++ b/quartz/components/scripts/popover.inline.ts @@ -2,14 +2,18 @@ import { computePosition, flip, inline, shift } from "@floating-ui/dom" // from micromorph/src/utils.ts // https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5 -export function normalizeRelativeURLs(el: Element | Document, base: string | URL) { - const update = (el: Element, attr: string, base: string | URL) => { - el.setAttribute(attr, new URL(el.getAttribute(attr)!, base).pathname) +export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) { + const rebase = (el: Element, attr: string, newBase: string | URL) => { + const rebased = new URL(el.getAttribute(attr)!, newBase) + el.setAttribute(attr, rebased.pathname + rebased.hash) } - el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) => update(item, "href", base)) - - el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) => update(item, "src", base)) + el.querySelectorAll('[href^="./"], [href^="../"]').forEach((item) => + rebase(item, "href", destination), + ) + el.querySelectorAll('[src^="./"], [src^="../"]').forEach((item) => + rebase(item, "src", destination), + ) } const p = new DOMParser() From 50f0ba29a247c81b5f29d290e54f7e1aabb18ab3 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Fri, 17 Nov 2023 07:31:20 +0800 Subject: [PATCH 10/10] feat: cname emitter (#590) * feat: cname emitter * feat: impl cname.ts * Update cname.ts * Update index.ts * Update cname.ts * Update cname.ts * Update cname.ts * Update cname.ts --- quartz/plugins/emitters/cname.ts | 29 +++++++++++++++++++++++++++++ quartz/plugins/emitters/index.ts | 1 + 2 files changed, 30 insertions(+) create mode 100644 quartz/plugins/emitters/cname.ts diff --git a/quartz/plugins/emitters/cname.ts b/quartz/plugins/emitters/cname.ts new file mode 100644 index 000000000..ffe2c6d12 --- /dev/null +++ b/quartz/plugins/emitters/cname.ts @@ -0,0 +1,29 @@ +import { FilePath, joinSegments } from "../../util/path" +import { QuartzEmitterPlugin } from "../types" +import fs from "fs" +import chalk from "chalk" + +export function extractDomainFromBaseUrl(baseUrl: string) { + const url = new URL(`https://${baseUrl}`) + return url.hostname +} + +export const CNAME: QuartzEmitterPlugin = () => ({ + name: "CNAME", + getQuartzComponents() { + return [] + }, + async emit({ argv, cfg }, _content, _resources, _emit): Promise { + if (!cfg.configuration.baseUrl) { + console.warn(chalk.yellow("CNAME emitter requires `baseUrl` to be set in your configuration")) + return [] + } + const path = joinSegments(argv.output, "CNAME") + const content = extractDomainFromBaseUrl(cfg.configuration.baseUrl) + if (!content) { + return [] + } + fs.writeFileSync(path, content) + return [path] as FilePath[] + }, +}) diff --git a/quartz/plugins/emitters/index.ts b/quartz/plugins/emitters/index.ts index 99a2c54d5..bc378c47b 100644 --- a/quartz/plugins/emitters/index.ts +++ b/quartz/plugins/emitters/index.ts @@ -7,3 +7,4 @@ export { Assets } from "./assets" export { Static } from "./static" export { ComponentResources } from "./componentResources" export { NotFoundPage } from "./404" +export { CNAME } from "./cname"