Skip to content

Commit afcf815

Browse files
authoredFeb 20, 2025··
fix(type): register module hooks types (#3166)
1 parent 7a1fe8d commit afcf815

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed
 

‎docs/content/docs/7.advanced/5.hooks.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ It can be used to modify the raw content from a `file` before it is transformed
1313
or modify the transform options.
1414

1515
```ts
16-
import type { FileBeforeParseHook } from '@nuxt/content'
17-
1816
export default defineNuxtConfig({
1917
hooks: {
20-
'content:file:beforeParse'(ctx: FileBeforeParseHook) {
18+
'content:file:beforeParse'(ctx) {
2119
// ...
2220
}
2321
}
@@ -29,11 +27,9 @@ export default defineNuxtConfig({
2927
This hook is called after the content is parsed and before it is saved to the database.
3028

3129
```ts
32-
import type { FileAfterParseHook } from '@nuxt/content'
33-
3430
export default defineNuxtConfig({
3531
hooks: {
36-
'content:file:afterParse'(ctx: FileAfterParseHook) {
32+
'content:file:afterParse'(ctx) {
3733
// ...
3834
}
3935
}

‎src/types/content.ts

+2
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ export interface MarkdownRoot extends MDCRoot {
7979
props?: Record<string, unknown>
8080
toc?: Toc
8181
}
82+
83+
export type ParsedContentFile = Record<string, unknown>

‎src/types/hooks.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ResolvedCollection } from './collection'
2+
import type { ContentFile, ParsedContentFile } from './content'
3+
import type { PathMetaOptions } from './path-meta'
4+
5+
// Parser options interface
6+
interface ParserOptions {
7+
pathMeta: PathMetaOptions
8+
markdown: {
9+
compress: boolean
10+
rehypePlugins: Record<string, unknown>
11+
remarkPlugins: Record<string, unknown>
12+
[key: string]: unknown
13+
}
14+
}
15+
16+
// Hook context for beforeParse
17+
export interface FileBeforeParseHook {
18+
file: ContentFile
19+
collection: ResolvedCollection
20+
parserOptions: ParserOptions
21+
}
22+
23+
// Hook context for afterParse
24+
export interface FileAfterParseHook {
25+
file: ContentFile
26+
content: ParsedContentFile
27+
collection: ResolvedCollection
28+
}
29+
30+
// Declare module to extend Nuxt hooks
31+
declare module '@nuxt/schema' {
32+
interface NuxtHooks {
33+
'content:file:beforeParse': (ctx: FileBeforeParseHook) => Promise<void> | void
34+
'content:file:afterParse': (ctx: FileAfterParseHook) => Promise<void> | void
35+
}
36+
}

‎src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type * from './collection'
2+
export type * from './hooks'
23
export type * from './module'
34
export type * from './navigation'
45
export type * from './surround'

‎src/types/module.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { BuiltinLanguage as ShikiLang, BuiltinTheme as ShikiTheme, LanguageRegistration, ThemeRegistrationAny, ThemeRegistrationRaw } from 'shiki'
22
import type { ListenOptions } from 'listhen'
33
import type { GitInfo } from '../utils/git'
4-
import type { ContentFile, MarkdownPlugin, TransformContentOptions } from './content'
4+
import type { MarkdownPlugin } from './content'
55
import type { PathMetaOptions } from './path-meta'
6-
import type { ResolvedCollection } from './collection'
76

87
export interface D1DatabaseConfig {
98
type: 'd1'
@@ -197,22 +196,3 @@ export interface PublicRuntimeConfig {
197196
iframeMessagingAllowedOrigins?: string
198197
}
199198
}
200-
201-
// TODO improve types
202-
export type ParsedContentFile = Record<string, unknown>
203-
204-
export interface FileBeforeParseHook {
205-
collection: Readonly<ResolvedCollection>
206-
file: ContentFile
207-
parserOptions: TransformContentOptions
208-
}
209-
export interface FileAfterParseHook {
210-
collection: Readonly<ResolvedCollection>
211-
file: ContentFile
212-
content: ParsedContentFile
213-
}
214-
215-
export interface ModuleHooks {
216-
'content:file:beforeParse': (hook: FileBeforeParseHook) => void | Promise<void>
217-
'content:file:afterParse': (hook: FileAfterParseHook) => void | Promise<void>
218-
}

‎src/utils/content/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import { defu } from 'defu'
99
import { createOnigurumaEngine } from 'shiki/engine/oniguruma'
1010
import { visit } from 'unist-util-visit'
1111
import type { ResolvedCollection } from '../../types/collection'
12-
import type { FileAfterParseHook, FileBeforeParseHook, ModuleOptions } from '../../types/module'
12+
import type { FileAfterParseHook, FileBeforeParseHook, ModuleOptions, ContentFile, ContentTransformer } from '../../types'
1313
import { logger } from '../dev'
14-
import type { ContentFile, ContentTransformer } from '../../types'
1514
import { transformContent } from './transformers'
1615

1716
let parserOptions = {
@@ -147,7 +146,6 @@ export async function createParser(collection: ResolvedCollection, nuxt?: Nuxt)
147146
file.extension = file.extension ?? file.path.includes('.') ? '.' + file.path.split('.').pop() : undefined
148147
}
149148
const beforeParseCtx: FileBeforeParseHook = { file, collection, parserOptions }
150-
// @ts-expect-error runtime type
151149
await nuxt?.callHook?.('content:file:beforeParse', beforeParseCtx)
152150
const { file: hookedFile } = beforeParseCtx
153151

@@ -183,7 +181,6 @@ export async function createParser(collection: ResolvedCollection, nuxt?: Nuxt)
183181
}
184182

185183
const afterParseCtx: FileAfterParseHook = { file: hookedFile, content: result, collection }
186-
// @ts-expect-error runtime type
187184
await nuxt?.callHook?.('content:file:afterParse', afterParseCtx)
188185
return afterParseCtx.content
189186
}

0 commit comments

Comments
 (0)
Please sign in to comment.