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.
This commit is contained in:
2
package-lock.json
generated
2
package-lock.json
generated
@@ -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": {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user