From 17d5ae76bfdb5a45f964937000d73d870711a1d6 Mon Sep 17 00:00:00 2001 From: saberzero1 Date: Fri, 17 Apr 2026 02:27:48 +0200 Subject: [PATCH] refactor(util/path): re-export normalizeHastElement from @quartz-community/utils The normalizeHastElement helper and its internal _rebaseHastElement have been promoted to @quartz-community/utils so that plugins can consume the same cross-slug HAST rebasing logic Quartz core uses for transclude expansion. Previously they lived only in Quartz's internal util/path module, which out-of-tree plugins (e.g. canvas-page, whose buildEmbeddedContent embeds vfile.data.htmlAst from arbitrary pages) could not import from. normalizeRelativeURLs and its DOM-element sibling _rebaseHtmlElement stay local - those operate on DOM Elements at runtime, not HAST AST nodes, and belong with Quartz's DOM-specific code. --- package-lock.json | 2 +- quartz/util/path.ts | 35 +---------------------------------- 2 files changed, 2 insertions(+), 35 deletions(-) diff --git a/package-lock.json b/package-lock.json index c07858f..5ef898e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1857,7 +1857,7 @@ }, "node_modules/@quartz-community/utils": { "version": "0.1.0", - "resolved": "git+ssh://git@github.com/quartz-community/utils.git#4dda81bc82b90f5bceaa3582e81547ce41cc581b", + "resolved": "git+ssh://git@github.com/quartz-community/utils.git#83f824b0ad495ae24b7f3524dd2049da76d24c2c", "dev": true, "license": "MIT", "dependencies": { diff --git a/quartz/util/path.ts b/quartz/util/path.ts index b17d04f..5332b60 100644 --- a/quartz/util/path.ts +++ b/quartz/util/path.ts @@ -21,6 +21,7 @@ export { slugTag, transformInternalLink, transformLink, + normalizeHastElement, } from "@quartz-community/utils" export type { @@ -33,11 +34,6 @@ export type { // --- v5-specific exports below --- -import type { Element as HastElement } from "hast" -import type { FullSlug } from "@quartz-community/utils" -import { isRelativeURL, joinSegments, resolveRelative } from "@quartz-community/utils" -import { clone } from "./clone" - export const QUARTZ = "quartz" // from micromorph/src/utils.ts @@ -54,32 +50,3 @@ export function normalizeRelativeURLs(el: Element | Document, destination: strin _rebaseHtmlElement(item, "src", destination) }) } - -const _rebaseHastElement = ( - el: HastElement, - attr: string, - curBase: FullSlug, - newBase: FullSlug, -) => { - if (el.properties?.[attr]) { - if (!isRelativeURL(String(el.properties[attr]))) { - return - } - - const rel = joinSegments(resolveRelative(curBase, newBase), "..", el.properties[attr] as string) - el.properties[attr] = rel - } -} - -export function normalizeHastElement(rawEl: HastElement, curBase: FullSlug, newBase: FullSlug) { - const el = clone(rawEl) // clone so we dont modify the original page - _rebaseHastElement(el, "src", curBase, newBase) - _rebaseHastElement(el, "href", curBase, newBase) - if (el.children) { - el.children = el.children.map((child) => - normalizeHastElement(child as HastElement, curBase, newBase), - ) - } - - return el -}