mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 05:14:06 -06:00
feat(ogImage): enhance user-defined OG image path handling with absolute URL support
This commit is contained in:
parent
b861740214
commit
0805e9a3ec
@ -1,14 +1,7 @@
|
||||
import { QuartzEmitterPlugin } from "../types"
|
||||
import { i18n } from "../../i18n"
|
||||
import { unescapeHTML } from "../../util/escape"
|
||||
import {
|
||||
FullSlug,
|
||||
getFileExtension,
|
||||
isFullUrl,
|
||||
isRelativeURL,
|
||||
joinSegments,
|
||||
QUARTZ,
|
||||
} from "../../util/path"
|
||||
import { FullSlug, getFileExtension, isAbsoluteURL, joinSegments, QUARTZ } from "../../util/path"
|
||||
import { ImageOptions, SocialImageOptions, defaultImage, getSatoriFonts } from "../../util/og"
|
||||
import sharp from "sharp"
|
||||
import satori, { SatoriOptions } from "satori"
|
||||
@ -154,9 +147,9 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
|
||||
let userDefinedOgImagePath = pageData.frontmatter?.socialImage
|
||||
|
||||
if (userDefinedOgImagePath) {
|
||||
userDefinedOgImagePath = isRelativeURL(userDefinedOgImagePath)
|
||||
? `https://${baseUrl}/static/${userDefinedOgImagePath}`
|
||||
: userDefinedOgImagePath
|
||||
userDefinedOgImagePath = isAbsoluteURL(userDefinedOgImagePath)
|
||||
? userDefinedOgImagePath
|
||||
: `https://${baseUrl}/static/${userDefinedOgImagePath}`
|
||||
}
|
||||
|
||||
const generatedOgImagePath = isRealFile
|
||||
@ -164,7 +157,7 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
|
||||
: undefined
|
||||
const defaultOgImagePath = `https://${baseUrl}/static/og-image.png`
|
||||
const ogImagePath = userDefinedOgImagePath ?? generatedOgImagePath ?? defaultOgImagePath
|
||||
|
||||
console.log("ogImagePath", ogImagePath)
|
||||
const ogImageMimeType = `image/${getFileExtension(ogImagePath) ?? "png"}`
|
||||
return (
|
||||
<>
|
||||
|
||||
@ -40,6 +40,15 @@ export function isRelativeURL(s: string): s is RelativeURL {
|
||||
return validStart && validEnding && ![".md", ".html"].includes(getFileExtension(s) ?? "")
|
||||
}
|
||||
|
||||
export function isAbsoluteURL(s: string): boolean {
|
||||
try {
|
||||
new URL(s)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function getFullSlug(window: Window): FullSlug {
|
||||
const res = window.document.body.dataset.slug! as FullSlug
|
||||
return res
|
||||
|
||||
Loading…
Reference in New Issue
Block a user