mirror of
https://github.com/quartz-community/utils.git
synced 2026-07-22 02:50:27 +00:00
slugifyPath now lowercases its output, which cascades through slugifyFilePath, slugTag, and transformInternalLink (all of which funnel through _sluggify). This matches Obsidian's case-insensitive link and tag matching: [[MyNote]], [[mynote]], and [[MYNOTE]] all resolve to the slug 'my-note', and the tags #MyTag, #mytag, and #MYTAG collapse into a single tag page. BREAKING CHANGE: all generated URLs are now lowercase. Users upgrading with mixed-case source filenames will see their URLs change (e.g. /MyNote -> /my-note). Also eliminates silent data loss on case-insensitive filesystems (macOS APFS, Windows NTFS) where Apple.md and apple.md previously produced conflicting HTML outputs that overwrote each other without warning.
84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
import { FullSlug, FilePath } from "@quartz-community/types";
|
|
export { FilePath, FullSlug } from "@quartz-community/types";
|
|
|
|
/** No '/index' ending, no file extension, can have trailing slash for folders. */
|
|
type SimpleSlug = string & {
|
|
_brand: "SimpleSlug";
|
|
};
|
|
/** Starts with './' or '../', used for navigation. */
|
|
type RelativeURL = string & {
|
|
_brand: "RelativeURL";
|
|
};
|
|
interface TransformOptions {
|
|
strategy: "absolute" | "relative" | "shortest";
|
|
allSlugs: FullSlug[];
|
|
}
|
|
declare function isFilePath(s: string): s is FilePath;
|
|
declare function isFullSlug(s: string): s is FullSlug;
|
|
declare function isSimpleSlug(s: string): s is SimpleSlug;
|
|
declare function isRelativeURL(s: string): s is RelativeURL;
|
|
declare function isAbsoluteURL(s: string): boolean;
|
|
declare function simplifySlug(fp: FullSlug | string): SimpleSlug;
|
|
declare function getFullSlug(window: Window): FullSlug;
|
|
declare function getFullSlugFromUrl(): FullSlug;
|
|
declare function slugifyFilePath(fp: FilePath, excludeExt?: boolean): FullSlug;
|
|
declare function joinSegments(...args: string[]): string;
|
|
declare function resolvePath(to: string): string;
|
|
/** Read the base path injected at build time via `data-basepath` on `<body>`.
|
|
* Returns `""` for root deployments, e.g. `"/repository"` for subdirectory. */
|
|
declare function getBasePath(): string;
|
|
/** Resolve a slug to an absolute URL path, prepending the site's base path.
|
|
* e.g. `resolveBasePath("features/Callouts")` → `"/repository/features/Callouts"` */
|
|
declare function resolveBasePath(to: string, basePath?: string): string;
|
|
declare function endsWith(s: string, suffix: string): boolean;
|
|
declare function trimSuffix(s: string, suffix: string): string;
|
|
declare function stripSlashes(s: string, onlyStripPrefix?: boolean): string;
|
|
declare function getFileExtension(s: string): string | undefined;
|
|
declare function isFolderPath(fplike: string): boolean;
|
|
declare function getAllSegmentPrefixes(path: string): string[];
|
|
declare function pathToRoot(slug: FullSlug): RelativeURL;
|
|
declare function resolveRelative(current: FullSlug, target: FullSlug | SimpleSlug): RelativeURL;
|
|
declare function splitAnchor(link: string): [string, string];
|
|
declare function slugTag(tag: string): string;
|
|
declare function transformInternalLink(link: string): RelativeURL;
|
|
declare function transformLink(src: FullSlug, target: string, opts: TransformOptions): RelativeURL;
|
|
/**
|
|
* Slugify a file path for use as an href.
|
|
* Replaces whitespace→hyphens, &→-and-, %→-percent, removes ? and #, and
|
|
* lowercases the result so that link matching is case-insensitive (matching
|
|
* Obsidian's link-resolution semantics). Operates per segment so directory
|
|
* separators are preserved.
|
|
*/
|
|
declare function slugifyPath(s: string): string;
|
|
|
|
export {
|
|
type RelativeURL,
|
|
type SimpleSlug,
|
|
type TransformOptions,
|
|
endsWith,
|
|
getAllSegmentPrefixes,
|
|
getBasePath,
|
|
getFileExtension,
|
|
getFullSlug,
|
|
getFullSlugFromUrl,
|
|
isAbsoluteURL,
|
|
isFilePath,
|
|
isFolderPath,
|
|
isFullSlug,
|
|
isRelativeURL,
|
|
isSimpleSlug,
|
|
joinSegments,
|
|
pathToRoot,
|
|
resolveBasePath,
|
|
resolvePath,
|
|
resolveRelative,
|
|
simplifySlug,
|
|
slugTag,
|
|
slugifyFilePath,
|
|
slugifyPath,
|
|
splitAnchor,
|
|
stripSlashes,
|
|
transformInternalLink,
|
|
transformLink,
|
|
trimSuffix,
|
|
};
|