Skip to content

Commit

Permalink
fix: default enabled content og images when strictNuxtContentPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 23, 2024
1 parent d8327a9 commit 10c21e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ declare module '#og-image/unocss-config' {
hasNuxtIcon: hasNuxtModule('nuxt-icon') || hasNuxtModule('@nuxt/icon'),
colorPreference,

strictNuxtContentPaths: config.strictNuxtContentPaths,
// @ts-expect-error runtime type
isNuxtContentDocumentDriven: config.strictNuxtContentPaths || !!nuxt.options.content?.documentDriven,
}
Expand Down
13 changes: 9 additions & 4 deletions src/runtime/server/util/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import { getOgImagePath, useOgImageRuntimeConfig } from '../../shared'
import { normaliseOptions } from './options'

export function nuxtContentPlugin(nitroApp: NitroApp) {
const { isNuxtContentDocumentDriven, defaults } = useOgImageRuntimeConfig()
const { isNuxtContentDocumentDriven, strictNuxtContentPaths, defaults } = useOgImageRuntimeConfig()
nitroApp.hooks.hook('content:file:afterParse', async (content: ParsedContent) => {
if (content._draft || content._extension !== 'md' || content._partial || content.indexable === false || content.index === false)
return

let path = content.path
if (isNuxtContentDocumentDriven && content.path)
if (isNuxtContentDocumentDriven && !path)
path = content._path
let shouldRenderOgImage = !!content.ogImage
// if an effort was made to get the og image working for content we always render it
if (typeof content.ogImage === 'undefined' && (strictNuxtContentPaths || typeof content.path !== 'undefined')) {
shouldRenderOgImage = true
}
// convert ogImage to head tags
if (path && content.ogImage) {
const ogImageConfig = typeof content.ogImage === 'object' ? content.ogImage : {}
if (path && shouldRenderOgImage) {
const ogImageConfig = (typeof content.ogImage === 'object' ? content.ogImage : {}) || {}
const optionsWithDefault = defu(ogImageConfig, defaults)
// Note: we can't resolve the site URL here because we don't have access to the request
// the plugin nuxt-content-canonical-urls.ts fixes this
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface OgImageRuntimeConfig {
colorPreference: 'light' | 'dark'

isNuxtContentDocumentDriven: boolean
strictNuxtContentPaths: boolean
zeroRuntime: boolean
}

Expand Down

0 comments on commit 10c21e5

Please sign in to comment.