mirror of
https://github.com/quartz-community/folder-page.git
synced 2026-07-22 02:50:24 +00:00
refactor: use shared classNames from @quartz-community/utils
This commit is contained in:
parent
47e3441b24
commit
6090bd8bab
2 changed files with 12 additions and 63 deletions
|
|
@ -1,3 +1 @@
|
|||
export function classNames(...classes: (string | undefined | null | false)[]): string {
|
||||
return classes.filter(Boolean).join(" ");
|
||||
}
|
||||
export { classNames } from "@quartz-community/utils/lang";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Reference in a new issue