rizaldy.club/quartz/util/path.ts

301 lines
12 KiB
TypeScript
Raw Normal View History

2023-07-26 11:10:37 +07:00
import { slug } from "github-slugger"
// this file must be isomorphic so it can't use node libs (e.g. path)
2023-05-30 22:02:20 +07:00
2023-07-13 14:19:35 +07:00
// Quartz Paths
// Things in boxes are not actual types but rather sources which these types can be acquired from
//
// ┌────────────┐
// ┌───────────┤ Browser ├────────────┐
// │ └────────────┘ │
// │ │
// ▼ ▼
// ┌────────┐ ┌─────────────┐
// ┌───────────────────┤ Window │ │ LinkElement │
// │ └────┬───┘ └──────┬──────┘
// │ │ │
// │ getClientSlug() │ .href │
// │ ▼ ▼
// │
// │ Client Slug ┌───► Relative URL
// getCanonicalSlug() │ https://test.ca/note/abc#anchor?query=123 │ ../note/def#anchor
// │ │
// │ canonicalizeClient() │ │ ▲ ▲
// │ ▼ │ │ │
// │ pathToRoot() │ │ │
// └───────────────► Canonical Slug ────────────────┘ │ │
// note/abc │ │
// ──────────────────────────┘ │
// ▲ resolveRelative() │
2023-07-13 14:19:35 +07:00
// canonicalizeServer() │ │
// │
// HTML File Server Slug │
// note/abc/index.html ◄───────────── note/abc/index │
2023-07-13 14:19:35 +07:00
// │
// ▲ ┌────────┴────────┐
// slugifyFilePath() │ transformLink() │ │
2023-07-13 14:19:35 +07:00
// │ │ │
// ┌─────────┴──────────┐ ┌─────┴─────┐ ┌────────┴──────┐
// │ File Path │ │ Wikilinks │ │ Markdown Link │
// │ note/abc/index.md │ └───────────┘ └───────────────┘
2023-07-13 14:19:35 +07:00
// └────────────────────┘ ▲ ▲
// ▲ │ │
// │ ┌─────────┐ │ │
// └────────────┤ MD File ├─────┴─────────────────┘
// └─────────┘
export const QUARTZ = "quartz"
2023-07-13 14:19:35 +07:00
/// Utility type to simulate nominal types in TypeScript
type SlugLike<T> = string & { __brand: T }
/** Client-side slug, usually obtained through `window.location` */
export type ClientSlug = SlugLike<"client">
export function isClientSlug(s: string): s is ClientSlug {
const res = /^https?:\/\/.+/.test(s)
return res
2023-07-13 14:19:35 +07:00
}
/** Canonical slug, should be used whenever you need to refer to the location of a file/note.
2023-07-23 07:27:41 +07:00
* On the client, this is normally stored in `document.body.dataset.slug`
*/
2023-07-13 14:19:35 +07:00
export type CanonicalSlug = SlugLike<"canonical">
export function isCanonicalSlug(s: string): s is CanonicalSlug {
const validStart = !(s.startsWith(".") || s.startsWith("/"))
const validEnding = !(s.endsWith("/") || s.endsWith("/index") || s === "index")
return validStart && !_containsForbiddenCharacters(s) && validEnding && !_hasFileExtension(s)
2023-07-13 14:19:35 +07:00
}
/** A relative link, can be found on `href`s but can also be constructed for
2023-07-23 07:27:41 +07:00
* client-side navigation (e.g. search and graph)
*/
2023-07-13 14:19:35 +07:00
export type RelativeURL = SlugLike<"relative">
export function isRelativeURL(s: string): s is RelativeURL {
const validStart = /^\.{1,2}/.test(s)
const validEnding = !(s.endsWith("/index") || s === "index")
return validStart && validEnding && ![".md", ".html"].includes(_getFileExtension(s) ?? "")
2023-07-13 14:19:35 +07:00
}
/** A server side slug. This is what Quartz uses to emit files so uses index suffixes */
export type ServerSlug = SlugLike<"server">
export function isServerSlug(s: string): s is ServerSlug {
const validStart = !(s.startsWith(".") || s.startsWith("/"))
2023-07-13 14:19:35 +07:00
const validEnding = !s.endsWith("/")
return validStart && validEnding && !_containsForbiddenCharacters(s)
2023-07-13 14:19:35 +07:00
}
/** The real file path to a file on disk */
export type FilePath = SlugLike<"filepath">
export function isFilePath(s: string): s is FilePath {
const validStart = !s.startsWith(".")
return validStart && _hasFileExtension(s)
2023-07-13 14:19:35 +07:00
}
export function getClientSlug(window: Window): ClientSlug {
const res = window.location.href as ClientSlug
return res
2023-07-13 14:19:35 +07:00
}
export function getCanonicalSlug(window: Window): CanonicalSlug {
const res = window.document.body.dataset.slug! as CanonicalSlug
return res
}
2023-07-13 14:19:35 +07:00
export function canonicalizeClient(slug: ClientSlug): CanonicalSlug {
const { pathname } = new URL(slug)
let fp = pathname.slice(1)
2023-07-23 07:27:41 +07:00
fp = fp.replace(new RegExp(_getFileExtension(fp) + "$"), "")
const res = _canonicalize(fp) as CanonicalSlug
return res
2023-07-13 14:19:35 +07:00
}
export function canonicalizeServer(slug: ServerSlug): CanonicalSlug {
let fp = slug as string
const res = _canonicalize(fp) as CanonicalSlug
return res
2023-07-13 14:19:35 +07:00
}
export function slugifyFilePath(fp: FilePath, excludeExt?: boolean): ServerSlug {
fp = _stripSlashes(fp) as FilePath
let ext = _getFileExtension(fp)
const withoutFileExt = fp.replace(new RegExp(ext + "$"), "")
if (excludeExt || [".md", ".html", undefined].includes(ext)) {
ext = ""
}
2023-07-16 13:33:06 +07:00
let slug = withoutFileExt
2023-07-23 07:27:41 +07:00
.split("/")
.map((segment) => segment.replace(/\s/g, "-")) // slugify all segments
.join("/") // always use / as sep
.replace(/\/$/, "") // remove trailing slash
2023-07-13 14:19:35 +07:00
2023-07-16 13:33:06 +07:00
// treat _index as index
if (_endsWith(slug, "_index")) {
slug = slug.replace(/_index$/, "index")
}
return slug + ext as ServerSlug
2023-07-13 14:19:35 +07:00
}
export function transformInternalLink(link: string): RelativeURL {
let [fplike, anchor] = splitAnchor(decodeURI(link))
const folderPath =
fplike.endsWith("index") ||
fplike.endsWith("index.md") ||
fplike.endsWith("index.html") ||
fplike.endsWith("/")
2023-07-23 07:27:41 +07:00
let segments = fplike.split("/").filter((x) => x.length > 0)
2023-07-13 14:19:35 +07:00
let prefix = segments.filter(_isRelativeSegment).join("/")
2023-07-23 07:27:41 +07:00
let fp = segments.filter((seg) => !_isRelativeSegment(seg)).join("/")
// manually add ext here as we want to not strip 'index' if it has an extension
fp = canonicalizeServer(slugifyFilePath(fp as FilePath) as ServerSlug)
const joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
const trail = folderPath ? "/" : ""
const res = (_addRelativeToStart(joined) + trail + anchor) as RelativeURL
return res
2023-07-13 14:19:35 +07:00
}
// resolve /a/b/c to ../../..
2023-07-13 14:19:35 +07:00
export function pathToRoot(slug: CanonicalSlug): RelativeURL {
let rootPath = slug
2023-07-23 07:27:41 +07:00
.split("/")
.filter((x) => x !== "")
.map((_) => "..")
.join("/")
2023-07-13 14:19:35 +07:00
const res = _addRelativeToStart(rootPath) as RelativeURL
return res
}
export function resolveRelative(current: CanonicalSlug, target: CanonicalSlug): RelativeURL {
const res = joinSegments(pathToRoot(current), target) as RelativeURL
return res
}
export function splitAnchor(link: string): [string, string] {
let [fp, anchor] = link.split("#", 2)
2023-07-23 07:27:41 +07:00
anchor = anchor === undefined ? "" : "#" + slugAnchor(anchor)
return [fp, anchor]
}
2023-07-26 11:10:37 +07:00
export function slugAnchor(anchor: string) {
return slug(anchor)
}
export function slugTag(tag: string) {
return tag
.split("/")
.map((tagSegment) => slug(tagSegment))
.join("/")
}
export function joinSegments(...args: string[]): string {
2023-07-23 07:27:41 +07:00
return args.filter((segment) => segment !== "").join("/")
2023-07-13 14:19:35 +07:00
}
2023-07-26 11:10:37 +07:00
export function getAllSegmentPrefixes(tags: string): string[] {
const segments = tags.split("/")
const results: string[] = []
for (let i = 0; i < segments.length; i++) {
results.push(segments.slice(0, i + 1).join("/"))
}
return results
}
export interface TransformOptions {
strategy: "absolute" | "relative" | "shortest"
allSlugs: ServerSlug[]
}
export function transformLink(
src: CanonicalSlug,
target: string,
opts: TransformOptions,
): RelativeURL {
let targetSlug: string = transformInternalLink(target)
if (opts.strategy === "relative") {
return _addRelativeToStart(targetSlug) as RelativeURL
} else {
const folderTail = targetSlug.endsWith("/") ? "/" : ""
const canonicalSlug = _stripSlashes(targetSlug.slice(".".length))
let [targetCanonical, targetAnchor] = splitAnchor(canonicalSlug)
if (opts.strategy === "shortest") {
// if the file name is unique, then it's just the filename
const matchingFileNames = opts.allSlugs.filter((slug) => {
const parts = slug.split("/")
const fileName = parts.at(-1)
return targetCanonical === fileName
})
// only match, just use it
if (matchingFileNames.length === 1) {
const targetSlug = canonicalizeServer(matchingFileNames[0])
return (resolveRelative(src, targetSlug) + targetAnchor) as RelativeURL
}
}
// if it's not unique, then it's the absolute path from the vault root
return joinSegments(pathToRoot(src), canonicalSlug) + folderTail as RelativeURL
}
}
2023-07-13 14:19:35 +07:00
function _canonicalize(fp: string): string {
2023-07-16 13:33:06 +07:00
fp = _trimSuffix(fp, "index")
2023-07-23 07:27:41 +07:00
return _stripSlashes(fp)
2023-07-03 03:08:29 +07:00
}
2023-07-16 13:33:06 +07:00
function _endsWith(s: string, suffix: string): boolean {
2023-07-23 07:27:41 +07:00
return s === suffix || s.endsWith("/" + suffix)
2023-07-16 13:33:06 +07:00
}
function _trimSuffix(s: string, suffix: string): string {
if (_endsWith(s, suffix)) {
2023-07-23 07:27:41 +07:00
s = s.slice(0, -suffix.length)
2023-07-16 13:33:06 +07:00
}
return s
}
2023-07-13 14:19:35 +07:00
function _containsForbiddenCharacters(s: string): boolean {
return s.includes(" ") || s.includes("#") || s.includes("?")
}
2023-06-17 09:41:59 +07:00
2023-07-13 14:19:35 +07:00
function _hasFileExtension(s: string): boolean {
return _getFileExtension(s) !== undefined
}
function _getFileExtension(s: string): string | undefined {
return s.match(/\.[A-Za-z0-9]+$/)?.[0]
2023-06-17 09:41:59 +07:00
}
2023-07-13 14:19:35 +07:00
function _isRelativeSegment(s: string): boolean {
return /^\.{0,2}$/.test(s)
2023-05-30 22:02:20 +07:00
}
export function _stripSlashes(s: string): string {
2023-07-13 14:19:35 +07:00
if (s.startsWith("/")) {
s = s.substring(1)
}
2023-05-30 22:02:20 +07:00
2023-07-13 14:19:35 +07:00
if (s.endsWith("/")) {
s = s.slice(0, -1)
2023-06-04 02:07:19 +07:00
}
2023-07-13 14:19:35 +07:00
return s
}
2023-07-13 14:19:35 +07:00
function _addRelativeToStart(s: string): string {
if (s === "") {
s = "."
}
2023-07-13 14:19:35 +07:00
if (!s.startsWith(".")) {
s = joinSegments(".", s)
2023-07-13 14:19:35 +07:00
}
2023-07-13 14:19:35 +07:00
return s
}