fix(ogImage): update socialImage path to include base URL if defined (#1858)
* fix(ogImage): update socialImage path to include base URL if defined * feat(path): add function to check if a file path is absolute * fix(ogImage): handle absolute paths for user defined og image paths * docs(CustomOgImages): update socialImage property to accept full URLs * fix(ogImage): typo * fix(ogImage): improve user-defined OG image path handling * Update docs/plugins/CustomOgImages.md Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com> * Update quartz/plugins/emitters/ogImage.tsx Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com> * refactor(path): remove isAbsoluteFilePath function * fix(ogImage): update user-defined OG image path handling to support relative URLs * feat(ogImage): enhance user-defined OG image path handling with absolute URL support * refactor(ogImage): remove debug log for ogImagePath * feat(path): add isAbsoluteURL function and corresponding tests * refactor(path): remove unused URL import for isomorphic compatibility --------- Co-authored-by: Karim H <karimh96@hotmail.com> Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
import { QuartzEmitterPlugin } from "../types"
 | 
			
		||||
import { i18n } from "../../i18n"
 | 
			
		||||
import { unescapeHTML } from "../../util/escape"
 | 
			
		||||
import { FullSlug, getFileExtension, 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"
 | 
			
		||||
@@ -144,13 +144,19 @@ export const CustomOgImages: QuartzEmitterPlugin<Partial<SocialImageOptions>> =
 | 
			
		||||
        additionalHead: [
 | 
			
		||||
          (pageData) => {
 | 
			
		||||
            const isRealFile = pageData.filePath !== undefined
 | 
			
		||||
            const userDefinedOgImagePath = pageData.frontmatter?.socialImage
 | 
			
		||||
            let userDefinedOgImagePath = pageData.frontmatter?.socialImage
 | 
			
		||||
 | 
			
		||||
            if (userDefinedOgImagePath) {
 | 
			
		||||
              userDefinedOgImagePath = isAbsoluteURL(userDefinedOgImagePath)
 | 
			
		||||
                ? userDefinedOgImagePath
 | 
			
		||||
                : `https://${baseUrl}/static/${userDefinedOgImagePath}`
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            const generatedOgImagePath = isRealFile
 | 
			
		||||
              ? `https://${baseUrl}/${pageData.slug!}-og-image.webp`
 | 
			
		||||
              : undefined
 | 
			
		||||
            const defaultOgImagePath = `https://${baseUrl}/static/og-image.png`
 | 
			
		||||
            const ogImagePath = userDefinedOgImagePath ?? generatedOgImagePath ?? defaultOgImagePath
 | 
			
		||||
 | 
			
		||||
            const ogImageMimeType = `image/${getFileExtension(ogImagePath) ?? "png"}`
 | 
			
		||||
            return (
 | 
			
		||||
              <>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user