From 008bc85635981548fc40afea4bc1dc7fc1ac9b30 Mon Sep 17 00:00:00 2001 From: Stephen Tse Date: Sun, 4 May 2025 14:47:19 -0700 Subject: [PATCH] Moved getExtFromUrl to path.ts --- quartz/plugins/emitters/ogImage.tsx | 3 +-- quartz/util/path.test.ts | 30 +++++++++++++++++++++++++++-- quartz/util/path.ts | 14 ++++++++++++++ quartz/util/url.test.ts | 29 ---------------------------- quartz/util/url.ts | 13 ------------- 5 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 quartz/util/url.test.ts delete mode 100644 quartz/util/url.ts diff --git a/quartz/plugins/emitters/ogImage.tsx b/quartz/plugins/emitters/ogImage.tsx index faf77c317..79b7bd8d6 100644 --- a/quartz/plugins/emitters/ogImage.tsx +++ b/quartz/plugins/emitters/ogImage.tsx @@ -1,7 +1,7 @@ import { QuartzEmitterPlugin } from "../types" import { i18n } from "../../i18n" import { unescapeHTML } from "../../util/escape" -import { FullSlug, getFileExtension, isAbsoluteURL, joinSegments, QUARTZ } from "../../util/path" +import { FullSlug, getExtFromUrl, getFileExtension, isAbsoluteURL, joinSegments, QUARTZ } from "../../util/path" import { ImageOptions, SocialImageOptions, defaultImage, getSatoriFonts } from "../../util/og" import sharp from "sharp" import satori, { SatoriOptions } from "satori" @@ -12,7 +12,6 @@ import { BuildCtx } from "../../util/ctx" import { QuartzPluginData } from "../vfile" import fs from "node:fs/promises" import chalk from "chalk" -import { getExtFromUrl } from "../../util/url" import { imageExtsToOptimize, targetOptimizedImageExt } from "./assets" const defaultOptions: SocialImageOptions = { diff --git a/quartz/util/path.test.ts b/quartz/util/path.test.ts index e85f1d0ad..261bccd24 100644 --- a/quartz/util/path.test.ts +++ b/quartz/util/path.test.ts @@ -1,7 +1,7 @@ -import test, { describe } from "node:test" +import test, { describe, it } from "node:test" import * as path from "./path" import assert from "node:assert" -import { FullSlug, TransformOptions, SimpleSlug } from "./path" +import { FullSlug, TransformOptions, SimpleSlug, getExtFromUrl } from "./path" describe("typeguards", () => { test("isSimpleSlug", () => { @@ -361,3 +361,29 @@ describe("resolveRelative", () => { assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "ghi/" as SimpleSlug), "../ghi/") }) }) + +describe("getExtFromUrl", () => { + it("should return the correct file extension from a URL", () => { + const url = "https://example.com/image.jpg" + const ext = getExtFromUrl(url) + assert.strictEqual(ext, ".jpg") + }) + + it("should return undefined for URLs without an extension", () => { + const url = "https://example.com/image" + const ext = getExtFromUrl(url) + assert.strictEqual(ext, undefined) + }) + + it("should handle URLs with query parameters", () => { + const url = "https://example.com/image.jpg?size=large" + const ext = getExtFromUrl(url) + assert.strictEqual(ext, ".jpg") + }) + + it("should handle URLs with hash fragments", () => { + const url = "https://example.com/image.jpg#section1" + const ext = getExtFromUrl(url) + assert.strictEqual(ext, ".jpg") + }) +}) diff --git a/quartz/util/path.ts b/quartz/util/path.ts index b95770159..7eb10dee8 100644 --- a/quartz/util/path.ts +++ b/quartz/util/path.ts @@ -316,3 +316,17 @@ function _addRelativeToStart(s: string): string { return s } + +/** + * Extracts file extension from a file resource URL. + * + * @param url - URL to extract the file extension from + * @returns The file extension (with a preceding dot) or undefined if none found + */ +export function getExtFromUrl(url: string): string | undefined { + const urlObj = new URL(url) + const pathname = urlObj.pathname + // Remove any query parameters or hash first + const ext = pathname.split(/[#?]/)[0].split(".").pop()?.trim() + return ext === pathname ? undefined : "." + ext +} diff --git a/quartz/util/url.test.ts b/quartz/util/url.test.ts deleted file mode 100644 index 15d7e1263..000000000 --- a/quartz/util/url.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { describe, it } from "node:test" -import { getExtFromUrl } from "./url" -import assert from "node:assert/strict" - -describe("getExtFromUrl", () => { - it("should return the correct file extension from a URL", () => { - const url = "https://example.com/image.jpg" - const ext = getExtFromUrl(url) - assert.strictEqual(ext, ".jpg") - }) - - it("should return undefined for URLs without an extension", () => { - const url = "https://example.com/image" - const ext = getExtFromUrl(url) - assert.strictEqual(ext, undefined) - }) - - it("should handle URLs with query parameters", () => { - const url = "https://example.com/image.jpg?size=large" - const ext = getExtFromUrl(url) - assert.strictEqual(ext, ".jpg") - }) - - it("should handle URLs with hash fragments", () => { - const url = "https://example.com/image.jpg#section1" - const ext = getExtFromUrl(url) - assert.strictEqual(ext, ".jpg") - }) -}) diff --git a/quartz/util/url.ts b/quartz/util/url.ts deleted file mode 100644 index ed0e16be1..000000000 --- a/quartz/util/url.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Extracts file extension from a file resource URL. - * - * @param url - URL to extract the file extension from - * @returns The file extension (with a preceding dot) or undefined if none found - */ -export function getExtFromUrl(url: string): string | undefined { - const urlObj = new URL(url) - const pathname = urlObj.pathname - // Remove any query parameters or hash first - const ext = pathname.split(/[#?]/)[0].split(".").pop()?.trim() - return ext === pathname ? undefined : "." + ext -}