Skip to content

Commit

Permalink
fix(content): export util from @nuxtjs/robots/content
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 20, 2025
1 parent 1a70b9f commit d632b5e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 28 deletions.
19 changes: 19 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
declaration: true,
rollup: {
emitCJS: true,
},
entries: [
{ input: 'src/content', name: 'content' },
],
externals: [
'h3',
'std-env',
'nitropack',
'consola',
'@nuxt/content',
'zod'
],
})
1 change: 1 addition & 0 deletions content.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/content'
5 changes: 3 additions & 2 deletions docs/content/2.guides/3.content.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ In Nuxt Content v3 we need to use the `asRobotsCollection()`{lang="ts"} function
to be able to use the `robots` frontmatter key.

```ts [content.config.ts]
import { defineCollection, defineContentConfig, z } from '@nuxt/content'
import { asRobotsCollection } from '@nuxtjs/robots'
import { defineCollection, defineContentConfig } from '@nuxt/content'
import { asRobotsCollection } from '@nuxtjs/robots/content'

export default defineContentConfig({
collections: {
Expand All @@ -39,6 +39,7 @@ const route = useRoute()
const { data: page } = await useAsyncData(`page-${route.path}`, () => {
return queryCollection('content').path(route.path).first()
})
// Ensure the SEO meta tags are rendered
useSeoMeta(page.value.seo)
</script>
```
Expand Down
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,29 @@
"./dist/runtime/util": {
"types": "./dist/runtime/util.d.ts",
"import": "./dist/runtime/util.js"
},
"./content": {
"types": "./dist/content.d.ts",
"import": "./dist/content.mjs",
"require": "./dist/content.cjs"
}
},
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"typesVersions": {
"*": {
"content": [
"dist/content"
],
"utils": [
"dist/utils"
]
}
},
"files": [
"dist"
"content.d.ts",
"dist",
"util.d.ts"
],
"scripts": {
"lint": "eslint . --fix",
Expand Down Expand Up @@ -100,12 +117,5 @@
},
"resolutions": {
"typescript": "5.6.3"
},
"build": {
"externals": [
"h3",
"consola",
"@nuxt/content"
]
}
}
27 changes: 12 additions & 15 deletions src/content.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import type { Collection } from '@nuxt/content'
import type { TypeOf, ZodRawShape } from 'zod'
import { z } from '@nuxt/content'

export function asRobotsCollection(collection: any) {
if (collection.type !== 'page') {
return
}
if (!collection.schema) {
collection.schema = z.object({
robots: z.union([z.string(), z.boolean()]).optional(),
})
}
else {
collection.schema = collection.schema.extend({
robots: z.union([z.string(), z.boolean()]).optional(),
})
export const schema = z.object({
robots: z.union([z.string(), z.boolean()]).optional(),
})

export type RobotSchema = TypeOf<typeof schema>

export function asRobotsCollection<T extends ZodRawShape>(collection: Collection<T>): Collection<T> {
if (collection.type === 'page') {
// @ts-expect-error untyped
collection.schema = collection.schema ? collection.schema.extend(schema) : schema
}
collection._integrations = collection._integrations || []
collection._integrations.push('robots')
return collection
}
2 changes: 0 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ export interface ModulePublicRuntimeConfig {
['nuxt-robots']: ResolvedModuleOptions
}

export * from './content'

export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxtjs/robots',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/content-v3/content.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineCollection, defineContentConfig, z } from '@nuxt/content'
import { asRobotsCollection } from '../../../src/module'
import { asRobotsCollection } from '../../../src/content'

export default defineContentConfig({
collections: {
Expand Down
1 change: 1 addition & 0 deletions util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/runtime/util'

0 comments on commit d632b5e

Please # to comment.