mirror of
https://github.com/quartz-community/utils.git
synced 2026-07-22 02:50:27 +00:00
No description
Moved from Quartz core (quartz/util/path.ts) into the shared utils package so that plugins performing cross-slug HAST embedding can re-use the same rebase helper as Quartz's internal transclude expansion, rather than reimplementing or skipping the rebase. The primary consumer outside of Quartz core is canvas-page, whose buildEmbeddedContent() pulls vfile.data.htmlAst from arbitrary target pages and serializes it verbatim into canvas pages - producing broken relative hrefs that survive accidentally on root deployments but fail under non-trivial basePath configurations. Exposing the helper here lets canvas-page (and any future embedding plugin) rebase correctly without duplicating the logic. Implementation details: - Deep-clones the input via structuredClone (Node >=22 builtin) so callers don't need to bring their own clone function - Absolute URLs (http://, https://, /root-relative) pass through untouched; only relative URLs are rebased - Recurses into children so nested anchors inside a rebased subtree are also normalized Backed by 10 new tests covering same-slug, cross-slug (both directions), nested depth, anchor preservation, absolute URL passthrough, root-relative passthrough, src-attribute rebasing, elements without href/src, nested children recursion, and input-immutability. |
||
|---|---|---|
| .github/workflows | ||
| dist | ||
| src | ||
| test | ||
| .eslintrc.json | ||
| .gitignore | ||
| .prettierrc | ||
| CHANGELOG.md | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.build.json | ||
| tsconfig.json | ||
| tsup.config.ts | ||
| vitest.config.ts | ||
@quartz-community/utils
Shared utility functions for Quartz community plugins.
Installation
npm install @quartz-community/utils
Or using GitHub:
npm install github:quartz-community/utils
Usage
import {
simplifySlug,
getFullSlug,
joinSegments,
removeAllChildren,
classNames,
} from "@quartz-community/utils";
// Path utilities
const slug = simplifySlug("folder/index"); // "folder/"
const currentSlug = getFullSlug(window); // e.g., "blog/my-post"
const path = joinSegments("a", "b", "c"); // "a/b/c"
// DOM utilities
removeAllChildren(document.getElementById("container")!);
// Language utilities
const classes = classNames("btn", isActive && "active", null); // "btn active"
Modules
Path (@quartz-community/utils/path)
Path manipulation utilities for Quartz slugs:
simplifySlug(slug)- Remove/indexsuffix from slugsgetFullSlug(window)- Get current page slug from document body datasetgetFullSlugFromUrl()- Get current page slug from URL pathnamejoinSegments(...segments)- Join path segments with proper slash handlingresolvePath(path)- Ensure path starts with/endsWith(str, suffix)- Check if path ends with suffixtrimSuffix(str, suffix)- Remove suffix from pathstripSlashes(str, onlyPrefix?)- Remove leading/trailing slashesgetFileExtension(path)- Get file extensionisFolderPath(path)- Check if path represents a foldergetAllSegmentPrefixes(path)- Get all path prefixes
DOM (@quartz-community/utils/dom)
DOM manipulation utilities:
removeAllChildren(element)- Remove all child nodes from an elementregisterEscapeHandler(container, callback)- Register Escape key and click-outside handlersnormalizeRelativeURLs(document, baseUrl)- Convert relative URLs to absolute
Lang (@quartz-community/utils/lang)
Language/general utilities:
classNames(...classes)- Combine CSS class names, filtering falsy values
Types
The package exports TypeScript types for type-safe slug handling:
import type { FullSlug, SimpleSlug, RelativeURL } from "@quartz-community/utils";
License
MIT