2023-07-26 07:10:37 +03:00
import { slug } from "github-slugger"
2023-12-03 03:50:55 +03:00
import type { ElementContent , Element as HastElement } from "hast"
2023-08-14 03:47:07 +03:00
// this file must be isomorphic so it can't use node libs (e.g. path)
2023-05-30 18:02:20 +03:00
2023-08-13 07:16:34 +03:00
export const QUARTZ = "quartz"
2023-07-13 10:19:35 +03:00
/// Utility type to simulate nominal types in TypeScript
type SlugLike < T > = string & { __brand : T }
2023-08-20 01:52:25 +03:00
/** Cannot be relative and must have a file extension. */
export type FilePath = SlugLike < "filepath" >
export function isFilePath ( s : string ) : s is FilePath {
const validStart = ! s . startsWith ( "." )
return validStart && _hasFileExtension ( s )
2023-07-13 10:19:35 +03:00
}
2023-08-20 01:52:25 +03:00
/** Cannot be relative and may not have leading or trailing slashes. It can have `index` as it's last segment. Use this wherever possible is it's the most 'general' interpretation of a slug. */
export type FullSlug = SlugLike < "full" >
export function isFullSlug ( s : string ) : s is FullSlug {
2023-07-16 09:02:12 +03:00
const validStart = ! ( s . startsWith ( "." ) || s . startsWith ( "/" ) )
2023-08-20 01:52:25 +03:00
const validEnding = ! s . endsWith ( "/" )
return validStart && validEnding && ! _containsForbiddenCharacters ( s )
}
/** Shouldn't be a relative path and shouldn't have `/index` as an ending or a file extension. It _can_ however have a trailing slash to indicate a folder path. */
export type SimpleSlug = SlugLike < "simple" >
export function isSimpleSlug ( s : string ) : s is SimpleSlug {
const validStart = ! ( s . startsWith ( "." ) || s . startsWith ( "/" ) )
const validEnding = ! ( s . endsWith ( "/index" ) || s === "index" )
2023-07-16 09:02:12 +03:00
return validStart && ! _containsForbiddenCharacters ( s ) && validEnding && ! _hasFileExtension ( s )
2023-07-13 10:19:35 +03:00
}
2023-08-20 01:52:25 +03:00
/** Can be found on `href`s but can also be constructed for client-side navigation (e.g. search and graph) */
2023-07-13 10:19:35 +03:00
export type RelativeURL = SlugLike < "relative" >
export function isRelativeURL ( s : string ) : s is RelativeURL {
const validStart = /^\.{1,2}/ . test ( s )
2023-08-17 08:04:15 +03:00
const validEnding = ! ( s . endsWith ( "/index" ) || s === "index" )
2023-08-17 10:55:52 +03:00
return validStart && validEnding && ! [ ".md" , ".html" ] . includes ( _getFileExtension ( s ) ? ? "" )
2023-07-13 10:19:35 +03:00
}
2023-08-20 01:52:25 +03:00
export function getFullSlug ( window : Window ) : FullSlug {
const res = window . document . body . dataset . slug ! as FullSlug
2023-07-16 09:02:12 +03:00
return res
2023-07-13 10:19:35 +03:00
}
2023-08-20 01:52:25 +03:00
export function slugifyFilePath ( fp : FilePath , excludeExt? : boolean ) : FullSlug {
2023-07-16 09:02:12 +03:00
fp = _stripSlashes ( fp ) as FilePath
2023-08-17 10:55:28 +03:00
let ext = _getFileExtension ( fp )
const withoutFileExt = fp . replace ( new RegExp ( ext + "$" ) , "" )
if ( excludeExt || [ ".md" , ".html" , undefined ] . includes ( ext ) ) {
ext = ""
}
2023-07-16 09:33:06 +03:00
let slug = withoutFileExt
2023-07-23 03:27:41 +03:00
. split ( "/" )
2023-09-17 21:02:00 +03:00
. map ( ( segment ) = > segment . replace ( /\s/g , "-" ) . replace ( /%/g , "-percent" ) . replace ( /\?/g , "-q" ) ) // slugify all segments
2023-07-23 03:27:41 +03:00
. join ( "/" ) // always use / as sep
. replace ( /\/$/ , "" ) // remove trailing slash
2023-07-13 10:19:35 +03:00
2023-07-16 09:33:06 +03:00
// treat _index as index
if ( _endsWith ( slug , "_index" ) ) {
slug = slug . replace ( /_index$/ , "index" )
}
2023-08-20 01:52:25 +03:00
return ( slug + ext ) as FullSlug
}
export function simplifySlug ( fp : FullSlug ) : SimpleSlug {
2023-12-03 03:50:55 +03:00
const res = _stripSlashes ( _trimSuffix ( fp , "index" ) , true )
return ( res . length === 0 ? "/" : res ) as SimpleSlug
2023-07-13 10:19:35 +03:00
}
export function transformInternalLink ( link : string ) : RelativeURL {
2023-07-16 09:02:12 +03:00
let [ fplike , anchor ] = splitAnchor ( decodeURI ( link ) )
2023-08-17 08:04:15 +03:00
2023-08-20 01:52:25 +03:00
const folderPath = _isFolderPath ( fplike )
2023-07-23 03:27:41 +03:00
let segments = fplike . split ( "/" ) . filter ( ( x ) = > x . length > 0 )
2023-07-13 10:19:35 +03:00
let prefix = segments . filter ( _isRelativeSegment ) . join ( "/" )
2023-08-20 01:52:25 +03:00
let fp = segments . filter ( ( seg ) = > ! _isRelativeSegment ( seg ) && seg !== "" ) . join ( "/" )
2023-07-16 09:02:12 +03:00
2023-08-17 10:55:28 +03:00
// manually add ext here as we want to not strip 'index' if it has an extension
2023-08-20 01:52:25 +03:00
const simpleSlug = simplifySlug ( slugifyFilePath ( fp as FilePath ) )
const joined = joinSegments ( _stripSlashes ( prefix ) , _stripSlashes ( simpleSlug ) )
2023-08-17 08:04:15 +03:00
const trail = folderPath ? "/" : ""
2023-08-17 10:55:28 +03:00
const res = ( _addRelativeToStart ( joined ) + trail + anchor ) as RelativeURL
2023-07-16 09:02:12 +03:00
return res
2023-07-13 10:19:35 +03:00
}
2023-11-19 05:46:58 +03:00
// from micromorph/src/utils.ts
// https://github.com/natemoo-re/micromorph/blob/main/src/utils.ts#L5
2023-12-03 03:50:55 +03:00
const _rebaseHtmlElement = ( el : Element , attr : string , newBase : string | URL ) = > {
const rebased = new URL ( el . getAttribute ( attr ) ! , newBase )
el . setAttribute ( attr , rebased . pathname + rebased . hash )
}
2023-11-19 05:46:58 +03:00
export function normalizeRelativeURLs ( el : Element | Document , destination : string | URL ) {
el . querySelectorAll ( '[href^="./"], [href^="../"]' ) . forEach ( ( item ) = >
2023-12-03 03:50:55 +03:00
_rebaseHtmlElement ( item , "href" , destination ) ,
2023-11-19 05:46:58 +03:00
)
el . querySelectorAll ( '[src^="./"], [src^="../"]' ) . forEach ( ( item ) = >
2023-12-03 03:50:55 +03:00
_rebaseHtmlElement ( item , "src" , destination ) ,
2023-11-19 05:46:58 +03:00
)
}
2023-12-03 03:50:55 +03:00
const _rebaseHastElement = (
el : HastElement ,
attr : string ,
curBase : FullSlug ,
newBase : FullSlug ,
) = > {
if ( el . properties ? . [ attr ] ) {
if ( ! isRelativeURL ( String ( el . properties [ attr ] ) ) ) {
return
}
const rel = joinSegments ( resolveRelative ( curBase , newBase ) , ".." , el . properties [ attr ] as string )
el . properties [ attr ] = rel
}
}
export function normalizeHastElement ( el : HastElement , curBase : FullSlug , newBase : FullSlug ) {
_rebaseHastElement ( el , "src" , curBase , newBase )
_rebaseHastElement ( el , "href" , curBase , newBase )
if ( el . children ) {
el . children = el . children . map ( ( child ) = >
normalizeHastElement ( child as HastElement , curBase , newBase ) ,
)
}
return el
}
2023-08-20 01:52:25 +03:00
// resolve /a/b/c to ../..
export function pathToRoot ( slug : FullSlug ) : RelativeURL {
2023-07-13 10:19:35 +03:00
let rootPath = slug
2023-07-23 03:27:41 +03:00
. split ( "/" )
. filter ( ( x ) = > x !== "" )
2023-08-20 01:52:25 +03:00
. slice ( 0 , - 1 )
2023-07-23 03:27:41 +03:00
. map ( ( _ ) = > ".." )
. join ( "/" )
2023-07-13 10:19:35 +03:00
2023-08-20 01:52:25 +03:00
if ( rootPath . length === 0 ) {
rootPath = "."
}
return rootPath as RelativeURL
2023-07-16 09:02:12 +03:00
}
2023-08-20 01:52:25 +03:00
export function resolveRelative ( current : FullSlug , target : FullSlug | SimpleSlug ) : RelativeURL {
const res = joinSegments ( pathToRoot ( current ) , simplifySlug ( target as FullSlug ) ) as RelativeURL
2023-07-16 09:02:12 +03:00
return res
}
export function splitAnchor ( link : string ) : [ string , string ] {
let [ fp , anchor ] = link . split ( "#" , 2 )
2023-07-23 03:27:41 +03:00
anchor = anchor === undefined ? "" : "#" + slugAnchor ( anchor )
2023-07-16 09:02:12 +03:00
return [ fp , anchor ]
}
2023-07-26 07:10:37 +03:00
export function slugAnchor ( anchor : string ) {
return slug ( anchor )
}
export function slugTag ( tag : string ) {
return tag
. split ( "/" )
. map ( ( tagSegment ) = > slug ( tagSegment ) )
. join ( "/" )
}
2023-07-16 09:02:12 +03:00
export function joinSegments ( . . . args : string [ ] ) : string {
2023-09-13 07:29:57 +03:00
return args
. filter ( ( segment ) = > segment !== "" )
. join ( "/" )
. replace ( /\/\/+/g , "/" )
2023-07-13 10:19:35 +03:00
}
2023-07-26 07:10:37 +03: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
}
2023-08-13 07:16:34 +03:00
export interface TransformOptions {
strategy : "absolute" | "relative" | "shortest"
2023-08-20 01:52:25 +03:00
allSlugs : FullSlug [ ]
2023-08-13 07:16:34 +03:00
}
2023-08-20 01:52:25 +03:00
export function transformLink ( src : FullSlug , target : string , opts : TransformOptions ) : RelativeURL {
let targetSlug = transformInternalLink ( target )
2023-08-13 07:16:34 +03:00
if ( opts . strategy === "relative" ) {
2023-08-20 01:52:25 +03:00
return targetSlug as RelativeURL
2023-08-13 07:16:34 +03:00
} else {
2023-08-20 01:52:25 +03:00
const folderTail = _isFolderPath ( targetSlug ) ? "/" : ""
2023-08-17 10:55:28 +03:00
const canonicalSlug = _stripSlashes ( targetSlug . slice ( "." . length ) )
let [ targetCanonical , targetAnchor ] = splitAnchor ( canonicalSlug )
2023-08-13 07:16:34 +03:00
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 ) {
2023-08-20 01:52:25 +03:00
const targetSlug = matchingFileNames [ 0 ]
2023-08-13 07:16:34 +03:00
return ( resolveRelative ( src , targetSlug ) + targetAnchor ) as RelativeURL
}
}
// if it's not unique, then it's the absolute path from the vault root
2023-08-17 10:55:52 +03:00
return ( joinSegments ( pathToRoot ( src ) , canonicalSlug ) + folderTail ) as RelativeURL
2023-08-13 07:16:34 +03:00
}
}
2023-07-13 10:19:35 +03:00
2023-08-20 01:52:25 +03:00
function _isFolderPath ( fplike : string ) : boolean {
return (
fplike . endsWith ( "/" ) ||
_endsWith ( fplike , "index" ) ||
_endsWith ( fplike , "index.md" ) ||
_endsWith ( fplike , "index.html" )
)
2023-07-02 23:08:29 +03:00
}
2023-07-16 09:33:06 +03:00
function _endsWith ( s : string , suffix : string ) : boolean {
2023-07-23 03:27:41 +03:00
return s === suffix || s . endsWith ( "/" + suffix )
2023-07-16 09:33:06 +03:00
}
function _trimSuffix ( s : string , suffix : string ) : string {
if ( _endsWith ( s , suffix ) ) {
2023-07-23 03:27:41 +03:00
s = s . slice ( 0 , - suffix . length )
2023-07-16 09:33:06 +03:00
}
return s
}
2023-07-13 10:19:35 +03:00
function _containsForbiddenCharacters ( s : string ) : boolean {
return s . includes ( " " ) || s . includes ( "#" ) || s . includes ( "?" )
}
2023-06-17 05:41:59 +03:00
2023-07-13 10:19:35 +03:00
function _hasFileExtension ( s : string ) : boolean {
2023-07-16 09:02:12 +03:00
return _getFileExtension ( s ) !== undefined
}
function _getFileExtension ( s : string ) : string | undefined {
2023-08-09 07:31:36 +03:00
return s . match ( /\.[A-Za-z0-9]+$/ ) ? . [ 0 ]
2023-06-17 05:41:59 +03:00
}
2023-07-13 10:19:35 +03:00
function _isRelativeSegment ( s : string ) : boolean {
return /^\.{0,2}$/ . test ( s )
2023-05-30 18:02:20 +03:00
}
2023-08-20 01:52:25 +03:00
export function _stripSlashes ( s : string , onlyStripPrefix? : boolean ) : string {
2023-07-13 10:19:35 +03:00
if ( s . startsWith ( "/" ) ) {
s = s . substring ( 1 )
}
2023-05-30 18:02:20 +03:00
2023-08-20 01:52:25 +03:00
if ( ! onlyStripPrefix && s . endsWith ( "/" ) ) {
2023-07-13 10:19:35 +03:00
s = s . slice ( 0 , - 1 )
2023-06-03 22:07:19 +03:00
}
2023-07-13 10:19:35 +03:00
return s
2023-06-01 00:01:23 +03:00
}
2023-07-13 10:19:35 +03:00
function _addRelativeToStart ( s : string ) : string {
if ( s === "" ) {
s = "."
}
2023-06-01 00:01:23 +03:00
2023-07-13 10:19:35 +03:00
if ( ! s . startsWith ( "." ) ) {
2023-07-16 09:02:12 +03:00
s = joinSegments ( "." , s )
2023-07-13 10:19:35 +03:00
}
2023-06-01 00:01:23 +03:00
2023-07-13 10:19:35 +03:00
return s
}