diff --git a/src/util/lang.ts b/src/util/lang.ts index 5610168..5e39aa2 100644 --- a/src/util/lang.ts +++ b/src/util/lang.ts @@ -1,3 +1 @@ -export function classNames(...classes: (string | undefined | null | false)[]): string { - return classes.filter(Boolean).join(" "); -} +export { classNames } from "@quartz-community/utils/lang"; diff --git a/src/util/path.ts b/src/util/path.ts index e442be5..7c04991 100644 --- a/src/util/path.ts +++ b/src/util/path.ts @@ -1,60 +1,11 @@ -export type FullSlug = string & { __brand: "FullSlug" }; -export type SimpleSlug = string & { __brand: "SimpleSlug" }; -export type RelativeURL = string & { __brand: "relative" }; - -export function joinSegments(...args: string[]): string { - if (args.length === 0) return ""; - let joined = args - .filter((segment) => segment !== "" && segment !== "/") - .map((segment) => stripSlashes(segment)) - .join("/"); - const first = args[0]; - const last = args[args.length - 1]; - if (first?.startsWith("/")) joined = "/" + joined; - if (last?.endsWith("/")) joined = joined + "/"; - return joined; -} - -export function stripSlashes(s: string, onlyStripPrefix?: boolean): string { - if (s.startsWith("/")) s = s.substring(1); - if (!onlyStripPrefix && s.endsWith("/")) s = s.slice(0, -1); - return s; -} - -export function simplifySlug(fp: FullSlug): SimpleSlug { - const res = stripSlashes(trimSuffix(fp, "index"), true); - return (res.length === 0 ? "/" : res) as SimpleSlug; -} - -export function trimSuffix(s: string, suffix: string): string { - if (endsWith(s, suffix)) s = s.slice(0, -suffix.length); - return s; -} - -export function endsWith(s: string, suffix: string): boolean { - return s === suffix || s.endsWith("/" + suffix); -} - -export function pathToRoot(slug: FullSlug): RelativeURL { - let rootPath = slug - .split("/") - .filter((x) => x !== "") - .slice(0, -1) - .map((_) => "..") - .join("/"); - if (rootPath.length === 0) rootPath = "."; - return rootPath as RelativeURL; -} - -export function resolveRelative(current: FullSlug, target: FullSlug | SimpleSlug): RelativeURL { - return joinSegments(pathToRoot(current), simplifySlug(target as FullSlug)) as RelativeURL; -} - -export function isFolderPath(fplike: string): boolean { - return ( - fplike.endsWith("/") || - endsWith(fplike, "index") || - endsWith(fplike, "index.md") || - endsWith(fplike, "index.html") - ); -} +export type { FullSlug, SimpleSlug, RelativeURL } from "@quartz-community/utils/path"; +export { + joinSegments, + stripSlashes, + simplifySlug, + trimSuffix, + endsWith, + pathToRoot, + resolveRelative, + isFolderPath, +} from "@quartz-community/utils/path";