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.
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
// Re-export shared path utilities from @quartz-community/utils
|
|
export {
|
|
isFilePath,
|
|
isFullSlug,
|
|
isSimpleSlug,
|
|
isRelativeURL,
|
|
isAbsoluteURL,
|
|
getFullSlug,
|
|
slugifyFilePath,
|
|
simplifySlug,
|
|
joinSegments,
|
|
endsWith,
|
|
trimSuffix,
|
|
stripSlashes,
|
|
getFileExtension,
|
|
isFolderPath,
|
|
getAllSegmentPrefixes,
|
|
pathToRoot,
|
|
resolveRelative,
|
|
splitAnchor,
|
|
slugTag,
|
|
transformInternalLink,
|
|
transformLink,
|
|
normalizeHastElement,
|
|
} from "@quartz-community/utils"
|
|
|
|
export type {
|
|
FilePath,
|
|
FullSlug,
|
|
SimpleSlug,
|
|
RelativeURL,
|
|
TransformOptions,
|
|
} from "@quartz-community/utils"
|
|
|
|
// --- v5-specific exports below ---
|
|
|
|
export const QUARTZ = "quartz"
|
|
|
|
// from micromorph/src/utils.ts
|
|
// https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5
|
|
const _rebaseHtmlElement = (el: Element, attr: string, newBase: string | URL) => {
|
|
const rebased = new URL(el.getAttribute(attr)!, newBase)
|
|
el.setAttribute(attr, rebased.pathname + rebased.hash)
|
|
}
|
|
export function normalizeRelativeURLs(el: Element | Document, destination: string | URL) {
|
|
el.querySelectorAll('[href=""], [href^="./"], [href^="../"]').forEach((item) => {
|
|
_rebaseHtmlElement(item, "href", destination)
|
|
})
|
|
el.querySelectorAll('[src=""], [src^="./"], [src^="../"]').forEach((item) => {
|
|
_rebaseHtmlElement(item, "src", destination)
|
|
})
|
|
}
|