feat(path): add isAbsoluteURL function and corresponding tests

This commit is contained in:
Karim H 2025-03-21 19:32:00 -04:00
parent d1168170b7
commit 44abf3a30d
2 changed files with 14 additions and 2 deletions

View File

@ -38,6 +38,17 @@ describe("typeguards", () => {
assert(!path.isRelativeURL("./abc/def.md"))
})
test("isAbsoluteURL", () => {
assert(path.isAbsoluteURL("https://example.com"))
assert(path.isAbsoluteURL("http://example.com"))
assert(path.isAbsoluteURL("ftp://example.com/a/b/c"))
assert(path.isAbsoluteURL("http://host/%25"))
assert(path.isAbsoluteURL("file://host/twoslashes?more//slashes"))
assert(!path.isAbsoluteURL("example.com/abc/def"))
assert(!path.isAbsoluteURL("abc"))
})
test("isFullSlug", () => {
assert(path.isFullSlug("index"))
assert(path.isFullSlug("abc/def"))

View File

@ -1,6 +1,8 @@
import { slug as slugAnchor } from "github-slugger"
import type { Element as HastElement } from "hast"
import { clone } from "./clone"
import { URL } from "url"
// this file must be isomorphic so it can't use node libs (e.g. path)
export const QUARTZ = "quartz"
@ -15,7 +17,6 @@ export function isFilePath(s: string): s is FilePath {
return validStart && _hasFileExtension(s)
}
/** 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 {
@ -43,10 +44,10 @@ export function isRelativeURL(s: string): s is RelativeURL {
export function isAbsoluteURL(s: string): boolean {
try {
new URL(s)
return true
} catch {
return false
}
return true
}
export function getFullSlug(window: Window): FullSlug {