From 44abf3a30d198c860550529ac40ed6473bf78452 Mon Sep 17 00:00:00 2001 From: Karim H Date: Fri, 21 Mar 2025 19:32:00 -0400 Subject: [PATCH] feat(path): add isAbsoluteURL function and corresponding tests --- quartz/util/path.test.ts | 11 +++++++++++ quartz/util/path.ts | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/quartz/util/path.test.ts b/quartz/util/path.test.ts index 29d845d95..9f94c68ec 100644 --- a/quartz/util/path.test.ts +++ b/quartz/util/path.test.ts @@ -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")) diff --git a/quartz/util/path.ts b/quartz/util/path.ts index 23ae40a13..e21379998 100644 --- a/quartz/util/path.ts +++ b/quartz/util/path.ts @@ -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 {