2023-06-13 12:41:42 +07:00
|
|
|
import { slug as slugAnchor } from 'github-slugger'
|
2023-07-16 13:02:12 +07:00
|
|
|
import { trace } from './trace'
|
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 │
|
|
|
|
// │ ▼ ▼
|
|
|
|
// │
|
2023-07-16 13:02:12 +07:00
|
|
|
// │ 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 │
|
2023-07-16 13:02:12 +07:00
|
|
|
// note/abc/index.html ◄───────────── note/abc/index │
|
2023-07-13 14:19:35 +07:00
|
|
|
// │
|
|
|
|
// ▲ ┌────────┴────────┐
|
2023-07-16 13:02:12 +07:00
|
|
|
// slugifyFilePath() │ transformLink() │ │
|
2023-07-13 14:19:35 +07:00
|
|
|
// │ │ │
|
|
|
|
// ┌─────────┴──────────┐ ┌─────┴─────┐ ┌────────┴──────┐
|
|
|
|
// │ File Path │ │ Wikilinks │ │ Markdown Link │
|
2023-07-16 13:02:12 +07:00
|
|
|
// │ note/abc/index.md │ └───────────┘ └───────────────┘
|
2023-07-13 14:19:35 +07:00
|
|
|
// └────────────────────┘ ▲ ▲
|
|
|
|
// ▲ │ │
|
|
|
|
// │ ┌─────────┐ │ │
|
|
|
|
// └────────────┤ MD File ├─────┴─────────────────┘
|
|
|
|
// └─────────┘
|
|
|
|
|
2023-07-16 13:02:12 +07:00
|
|
|
const STRICT_TYPE_CHECKS = true
|
|
|
|
const HARD_EXIT_ON_FAIL = true
|
|
|
|
|
|
|
|
function conditionCheck<T>(name: string, label: 'pre' | 'post', s: T, chk: (x: any) => x is T) {
|
|
|
|
if (STRICT_TYPE_CHECKS && !chk(s)) {
|
|
|
|
trace(`${name} failed ${label}-condition check: ${s} does not pass ${chk.name}`, new Error())
|
|
|
|
if (HARD_EXIT_ON_FAIL) {
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-07-16 13:02:12 +07:00
|
|
|
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.
|
|
|
|
* On the client, this is normally stored in `document.body.dataset.slug`
|
|
|
|
*/
|
|
|
|
export type CanonicalSlug = SlugLike<"canonical">
|
|
|
|
export function isCanonicalSlug(s: string): s is CanonicalSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
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
|
|
|
|
* client-side navigation (e.g. search and graph)
|
|
|
|
*/
|
|
|
|
export type RelativeURL = SlugLike<"relative">
|
|
|
|
export function isRelativeURL(s: string): s is RelativeURL {
|
|
|
|
const validStart = /^\.{1,2}/.test(s)
|
2023-07-16 13:02:12 +07:00
|
|
|
const validEnding = !(s.endsWith("/") || s.endsWith("/index") || s === "index")
|
2023-07-13 14:19:35 +07:00
|
|
|
return validStart && validEnding && !_hasFileExtension(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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 {
|
2023-07-16 13:02:12 +07:00
|
|
|
const validStart = !(s.startsWith(".") || s.startsWith("/"))
|
2023-07-13 14:19:35 +07:00
|
|
|
const validEnding = !s.endsWith("/")
|
|
|
|
return validStart && validEnding && !_containsForbiddenCharacters(s) && !_hasFileExtension(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The real file path to a file on disk */
|
|
|
|
export type FilePath = SlugLike<"filepath">
|
|
|
|
export function isFilePath(s: string): s is FilePath {
|
2023-07-16 13:02:12 +07:00
|
|
|
const validStart = !s.startsWith(".")
|
|
|
|
return validStart && _hasFileExtension(s)
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getClientSlug(window: Window): ClientSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
const res = window.location.href as ClientSlug
|
|
|
|
conditionCheck(getClientSlug.name, 'post', res, isClientSlug)
|
|
|
|
return res
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getCanonicalSlug(window: Window): CanonicalSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
const res = window.document.body.dataset.slug! as CanonicalSlug
|
|
|
|
conditionCheck(getCanonicalSlug.name, 'post', res, isCanonicalSlug)
|
|
|
|
return res
|
2023-06-01 04:01:23 +07:00
|
|
|
}
|
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
export function canonicalizeClient(slug: ClientSlug): CanonicalSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
conditionCheck(canonicalizeClient.name, 'pre', slug, isClientSlug)
|
2023-07-13 14:19:35 +07:00
|
|
|
const { pathname } = new URL(slug)
|
2023-07-16 13:02:12 +07:00
|
|
|
let fp = pathname.slice(1)
|
|
|
|
fp = fp.replace(new RegExp(_getFileExtension(fp) + '$'), '')
|
|
|
|
const res = _canonicalize(fp) as CanonicalSlug
|
|
|
|
conditionCheck(canonicalizeClient.name, 'post', res, isCanonicalSlug)
|
|
|
|
return res
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function canonicalizeServer(slug: ServerSlug): CanonicalSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
conditionCheck(canonicalizeServer.name, 'pre', slug, isServerSlug)
|
2023-07-13 14:19:35 +07:00
|
|
|
let fp = slug as string
|
2023-07-16 13:02:12 +07:00
|
|
|
const res = _canonicalize(fp) as CanonicalSlug
|
|
|
|
conditionCheck(canonicalizeServer.name, 'post', res, isCanonicalSlug)
|
|
|
|
return res
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function slugifyFilePath(fp: FilePath): ServerSlug {
|
2023-07-16 13:02:12 +07:00
|
|
|
conditionCheck(slugifyFilePath.name, 'pre', fp, isFilePath)
|
|
|
|
fp = _stripSlashes(fp) as FilePath
|
|
|
|
const withoutFileExt = fp.replace(new RegExp(_getFileExtension(fp) + '$'), '')
|
2023-07-13 14:19:35 +07:00
|
|
|
const slug = withoutFileExt
|
2023-07-16 13:02:12 +07:00
|
|
|
.split('/')
|
2023-07-13 14:19:35 +07:00
|
|
|
.map((segment) => segment.replace(/\s/g, '-')) // slugify all segments
|
|
|
|
.join('/') // always use / as sep
|
|
|
|
.replace(/\/$/, '') // remove trailing slash
|
|
|
|
|
2023-07-16 13:02:12 +07:00
|
|
|
conditionCheck(slugifyFilePath.name, 'post', slug, isServerSlug)
|
2023-07-13 14:19:35 +07:00
|
|
|
return slug as ServerSlug
|
|
|
|
}
|
|
|
|
|
|
|
|
export function transformInternalLink(link: string): RelativeURL {
|
2023-07-16 13:02:12 +07:00
|
|
|
let [fplike, anchor] = splitAnchor(decodeURI(link))
|
2023-07-13 14:19:35 +07:00
|
|
|
let segments = fplike.split("/").filter(x => x.length > 0)
|
|
|
|
let prefix = segments.filter(_isRelativeSegment).join("/")
|
2023-07-16 13:02:12 +07:00
|
|
|
let fp = segments.filter(seg => !_isRelativeSegment(seg)).join("/")
|
|
|
|
|
|
|
|
// implicit markdown
|
|
|
|
if (!_hasFileExtension(fp)) {
|
|
|
|
fp += ".md"
|
|
|
|
}
|
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
fp = canonicalizeServer(slugifyFilePath(fp as FilePath))
|
|
|
|
|
|
|
|
if (fp.endsWith("index")) {
|
|
|
|
fp = fp.slice(0, -"index".length)
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:02:12 +07:00
|
|
|
let joined = joinSegments(_stripSlashes(prefix), _stripSlashes(fp))
|
|
|
|
const res = _addRelativeToStart(joined) + anchor as RelativeURL
|
|
|
|
conditionCheck(transformInternalLink.name, 'post', res, isRelativeURL)
|
|
|
|
return res
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve /a/b/c to ../../
|
|
|
|
export function pathToRoot(slug: CanonicalSlug): RelativeURL {
|
2023-07-16 13:02:12 +07:00
|
|
|
conditionCheck(pathToRoot.name, 'pre', slug, isCanonicalSlug)
|
2023-07-13 14:19:35 +07:00
|
|
|
let rootPath = slug
|
|
|
|
.split('/')
|
|
|
|
.filter(x => x !== '')
|
|
|
|
.map(_ => '..')
|
|
|
|
.join('/')
|
|
|
|
|
2023-07-16 13:02:12 +07:00
|
|
|
const res = _addRelativeToStart(rootPath) as RelativeURL
|
|
|
|
conditionCheck(pathToRoot.name, 'post', res, isRelativeURL)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveRelative(current: CanonicalSlug, target: CanonicalSlug): RelativeURL {
|
|
|
|
conditionCheck(resolveRelative.name, 'pre', current, isCanonicalSlug)
|
|
|
|
conditionCheck(resolveRelative.name, 'pre', target, isCanonicalSlug)
|
|
|
|
const res = joinSegments(pathToRoot(current), target) as RelativeURL
|
|
|
|
conditionCheck(resolveRelative.name, 'post', res, isRelativeURL)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitAnchor(link: string): [string, string] {
|
|
|
|
let [fp, anchor] = link.split("#", 2)
|
|
|
|
anchor = anchor === undefined ? "" : '#' + slugAnchor(anchor)
|
|
|
|
return [fp, anchor]
|
|
|
|
}
|
|
|
|
|
|
|
|
export function joinSegments(...args: string[]): string {
|
|
|
|
return args.filter(segment => segment !== "").join('/')
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export const QUARTZ = "quartz"
|
|
|
|
|
|
|
|
function _canonicalize(fp: string): string {
|
2023-07-03 03:08:29 +07:00
|
|
|
if (fp.endsWith("index")) {
|
|
|
|
fp = fp.slice(0, -"index".length)
|
|
|
|
}
|
|
|
|
|
2023-07-16 13:02:12 +07:00
|
|
|
return _stripSlashes(fp)
|
2023-07-03 03:08:29 +07:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-07-16 13:02:12 +07:00
|
|
|
return _getFileExtension(s) !== undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getFileExtension(s: string): string | undefined {
|
|
|
|
return s.match(/\.[A-Za-z]+$/)?.[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
|
|
|
}
|
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
function _stripSlashes(s: string): string {
|
|
|
|
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-06-01 04:01:23 +07:00
|
|
|
}
|
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
function _addRelativeToStart(s: string): string {
|
|
|
|
if (s === "") {
|
|
|
|
s = "."
|
|
|
|
}
|
2023-06-01 04:01:23 +07:00
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
if (!s.startsWith(".")) {
|
2023-07-16 13:02:12 +07:00
|
|
|
s = joinSegments(".", s)
|
2023-07-13 14:19:35 +07:00
|
|
|
}
|
2023-06-01 04:01:23 +07:00
|
|
|
|
2023-07-13 14:19:35 +07:00
|
|
|
return s
|
|
|
|
}
|