|
1 | | -import type { Collections, PageCollections, CollectionQueryBuilder, SurroundOptions, PageCollectionItemBase } from '@nuxt/content' |
2 | | -import { hasProtocol, withTrailingSlash, withoutTrailingSlash, joinURL } from 'ufo' |
| 1 | +import type { Collections, PageCollections, CollectionQueryBuilder, SurroundOptions } from '@nuxt/content' |
3 | 2 | import { collectionQureyBuilder } from './internal/query' |
4 | 3 | import { measurePerformance } from './internal/performance' |
5 | 4 | import { generateNavigationTree } from './internal/navigation' |
6 | 5 | import { generateItemSurround } from './internal/surround' |
7 | 6 | import { generateSearchSections } from './internal/search' |
8 | 7 | import { fetchQuery } from './internal/api' |
9 | | -import { tryUseNuxtApp, useRuntimeConfig, useRoute, useHead } from '#imports' |
| 8 | +import { tryUseNuxtApp } from '#imports' |
10 | 9 |
|
11 | 10 | export const queryCollection = <T extends keyof Collections>(collection: T): CollectionQueryBuilder<Collections[T]> => { |
12 | 11 | return collectionQureyBuilder<T>(collection, (collection, sql) => executeContentQuery(collection, sql)) |
@@ -46,111 +45,3 @@ async function queryContentSqlClientWasm<T extends keyof Collections, Result = C |
46 | 45 |
|
47 | 46 | return rows as Result[] |
48 | 47 | } |
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 | | - head.title = head.title || content?.title |
65 | | - if (head.title) { |
66 | | - if (import.meta.server && !head.meta?.some(m => m.property === 'og:title')) { |
67 | | - head.meta.push({ |
68 | | - property: 'og:title', |
69 | | - content: head.title as string, |
70 | | - }) |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - if (import.meta.server && host) { |
75 | | - const _url = joinURL(host ?? '/', config.app.baseURL, route.fullPath) |
76 | | - const url = trailingSlash ? withTrailingSlash(_url) : withoutTrailingSlash(_url) |
77 | | - if (!head.meta.some(m => m.property === 'og:url')) { |
78 | | - head.meta.push({ |
79 | | - property: 'og:url', |
80 | | - content: url, |
81 | | - }) |
82 | | - } |
83 | | - if (!head.link.some(m => m.rel === 'canonical')) { |
84 | | - head.link.push({ |
85 | | - rel: 'canonical', |
86 | | - href: url, |
87 | | - }) |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - // Grab description from `head.description` or fallback to `data.description` |
92 | | - const description = head?.description || content?.description |
93 | | - |
94 | | - // Shortcut for head.description |
95 | | - if (description && head.meta.filter(m => m.name === 'description').length === 0) { |
96 | | - head.meta.push({ |
97 | | - name: 'description', |
98 | | - content: description, |
99 | | - }) |
100 | | - } |
101 | | - if (import.meta.server && description && !head.meta.some(m => m.property === 'og:description')) { |
102 | | - head.meta.push({ |
103 | | - property: 'og:description', |
104 | | - content: description, |
105 | | - }) |
106 | | - } |
107 | | - |
108 | | - // Grab description from `head` or fallback to `data.description` |
109 | | - // @ts-expect-error - image does not exists in content |
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