|
1 | | -import type { Collections, PageCollections, CollectionQueryBuilder, SurroundOptions } from '@nuxt/content' |
| 1 | +import type { Collections, PageCollections, CollectionQueryBuilder, SurroundOptions, PageCollectionItemBase } from '@nuxt/content' |
| 2 | +import { hasProtocol, withTrailingSlash, withoutTrailingSlash, joinURL } from 'ufo' |
2 | 3 | import { collectionQureyBuilder } from './internal/query' |
3 | 4 | import { measurePerformance } from './internal/performance' |
4 | 5 | import { generateNavigationTree } from './internal/navigation' |
5 | 6 | import { generateItemSurround } from './internal/surround' |
6 | 7 | import { generateSearchSections } from './internal/search' |
7 | 8 | import { fetchQuery } from './internal/api' |
8 | | -import { tryUseNuxtApp } from '#imports' |
| 9 | +import { tryUseNuxtApp, useRuntimeConfig, useRoute, useHead } from '#imports' |
9 | 10 |
|
10 | 11 | export const queryCollection = <T extends keyof Collections>(collection: T): CollectionQueryBuilder<Collections[T]> => { |
11 | 12 | return collectionQureyBuilder<T>(collection, (collection, sql) => executeContentQuery(collection, sql)) |
@@ -45,3 +46,111 @@ async function queryContentSqlClientWasm<T extends keyof Collections, Result = C |
45 | 46 |
|
46 | 47 | return rows as Result[] |
47 | 48 | } |
| 49 | + |
| 50 | +export const useContentHead = (content: PageCollectionItemBase | null, { host, trailingSlash }: { host?: string, trailingSlash?: boolean } = {}) => { |
| 51 | + if (!content) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const config = useRuntimeConfig() |
| 56 | + const route = useRoute() |
| 57 | + // Default head to `data?.seo` |
| 58 | + const head = Object.assign({} as unknown as Exclude<PageCollectionItemBase['seo'], undefined>, content?.seo || {}) |
| 59 | + |
| 60 | + head.meta = [...(head.meta || [])] |
| 61 | + head.link = [...(head.link || [])] |
| 62 | + |
| 63 | + // Great basic informations from the content |
| 64 | + const title = head.title || content?.title |
| 65 | + if (title) { |
| 66 | + head.title = title |
| 67 | + if (import.meta.server && !head.meta?.some(m => m.property === 'og:title')) { |
| 68 | + head.meta.push({ |
| 69 | + property: 'og:title', |
| 70 | + content: title as string, |
| 71 | + }) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (import.meta.server && host) { |
| 76 | + const _url = joinURL(host ?? '/', config.app.baseURL, route.fullPath) |
| 77 | + const url = trailingSlash ? withTrailingSlash(_url) : withoutTrailingSlash(_url) |
| 78 | + if (!head.meta.some(m => m.property === 'og:url')) { |
| 79 | + head.meta.push({ |
| 80 | + property: 'og:url', |
| 81 | + content: url, |
| 82 | + }) |
| 83 | + } |
| 84 | + if (!head.link.some(m => m.rel === 'canonical')) { |
| 85 | + head.link.push({ |
| 86 | + rel: 'canonical', |
| 87 | + href: url, |
| 88 | + }) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Grab description from `head.description` or fallback to `data.description` |
| 93 | + const description = head?.description || content?.description |
| 94 | + |
| 95 | + // Shortcut for head.description |
| 96 | + if (description && head.meta.filter(m => m.name === 'description').length === 0) { |
| 97 | + head.meta.push({ |
| 98 | + name: 'description', |
| 99 | + content: description, |
| 100 | + }) |
| 101 | + } |
| 102 | + if (import.meta.server && description && !head.meta.some(m => m.property === 'og:description')) { |
| 103 | + head.meta.push({ |
| 104 | + property: 'og:description', |
| 105 | + content: description, |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + // Grab description from `head` or fallback to `data.description` |
| 110 | + const image = head?.image || content?.image |
| 111 | + |
| 112 | + // Shortcut for head.image to og:image in meta |
| 113 | + if (image && head.meta.filter(m => m.property === 'og:image').length === 0) { |
| 114 | + // Handles `image: '/image/src.jpg'` |
| 115 | + if (typeof image === 'string') { |
| 116 | + head.meta.push({ |
| 117 | + property: 'og:image', |
| 118 | + content: host && !hasProtocol(image) ? new URL(joinURL(config.app.baseURL, image), host).href : image, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + // Handles: `image.src: '/image/src.jpg'` & `image.alt: 200`... |
| 123 | + if (typeof image === 'object') { |
| 124 | + // https://ogp.me/#structured |
| 125 | + const imageKeys = [ |
| 126 | + 'src', |
| 127 | + 'secure_url', |
| 128 | + 'type', |
| 129 | + 'width', |
| 130 | + 'height', |
| 131 | + 'alt', |
| 132 | + ] |
| 133 | + |
| 134 | + // Look on available keys |
| 135 | + for (const key of imageKeys) { |
| 136 | + // `src` is a shorthand for the URL. |
| 137 | + if (key === 'src' && image.src) { |
| 138 | + const isAbsoluteURL = hasProtocol(image.src) |
| 139 | + const imageURL = isAbsoluteURL ? image.src : joinURL(config.app.baseURL, image.src ?? '/') |
| 140 | + head.meta.push({ |
| 141 | + property: 'og:image', |
| 142 | + content: host && !isAbsoluteURL ? new URL(imageURL, host).href : imageURL, |
| 143 | + }) |
| 144 | + } |
| 145 | + else if (image[key]) { |
| 146 | + head.meta.push({ |
| 147 | + property: `og:image:${key}`, |
| 148 | + content: image[key], |
| 149 | + }) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + useHead(head) |
| 156 | +} |
0 commit comments