Moved getExtFromUrl to path.ts

This commit is contained in:
Stephen Tse 2025-05-04 14:47:19 -07:00
parent 918d3374d7
commit 008bc85635
5 changed files with 43 additions and 46 deletions

View File

@ -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 = {

View File

@ -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")
})
})

View File

@ -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
}

View File

@ -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")
})
})

View File

@ -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
}