mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 21:34:06 -06:00
Moved getExtFromUrl to path.ts
This commit is contained in:
parent
918d3374d7
commit
008bc85635
@ -1,7 +1,7 @@
|
|||||||
import { QuartzEmitterPlugin } from "../types"
|
import { QuartzEmitterPlugin } from "../types"
|
||||||
import { i18n } from "../../i18n"
|
import { i18n } from "../../i18n"
|
||||||
import { unescapeHTML } from "../../util/escape"
|
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 { ImageOptions, SocialImageOptions, defaultImage, getSatoriFonts } from "../../util/og"
|
||||||
import sharp from "sharp"
|
import sharp from "sharp"
|
||||||
import satori, { SatoriOptions } from "satori"
|
import satori, { SatoriOptions } from "satori"
|
||||||
@ -12,7 +12,6 @@ import { BuildCtx } from "../../util/ctx"
|
|||||||
import { QuartzPluginData } from "../vfile"
|
import { QuartzPluginData } from "../vfile"
|
||||||
import fs from "node:fs/promises"
|
import fs from "node:fs/promises"
|
||||||
import chalk from "chalk"
|
import chalk from "chalk"
|
||||||
import { getExtFromUrl } from "../../util/url"
|
|
||||||
import { imageExtsToOptimize, targetOptimizedImageExt } from "./assets"
|
import { imageExtsToOptimize, targetOptimizedImageExt } from "./assets"
|
||||||
|
|
||||||
const defaultOptions: SocialImageOptions = {
|
const defaultOptions: SocialImageOptions = {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import test, { describe } from "node:test"
|
import test, { describe, it } from "node:test"
|
||||||
import * as path from "./path"
|
import * as path from "./path"
|
||||||
import assert from "node:assert"
|
import assert from "node:assert"
|
||||||
import { FullSlug, TransformOptions, SimpleSlug } from "./path"
|
import { FullSlug, TransformOptions, SimpleSlug, getExtFromUrl } from "./path"
|
||||||
|
|
||||||
describe("typeguards", () => {
|
describe("typeguards", () => {
|
||||||
test("isSimpleSlug", () => {
|
test("isSimpleSlug", () => {
|
||||||
@ -361,3 +361,29 @@ describe("resolveRelative", () => {
|
|||||||
assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "ghi/" as SimpleSlug), "../ghi/")
|
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")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@ -316,3 +316,17 @@ function _addRelativeToStart(s: string): string {
|
|||||||
|
|
||||||
return s
|
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
|
||||||
|
}
|
||||||
|
|||||||
@ -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")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@ -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
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user