-
-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pre / post parsing hooks (#2925)
- Loading branch information
Showing
5 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: Hooks | ||
description: Modify your content using Nuxt build time hooks | ||
navigation: | ||
title: Hooks | ||
--- | ||
|
||
## `content:file:beforeParse`{lang="ts"} | ||
|
||
This hook is called before the content is parsed. | ||
|
||
It can be used to modify the raw content from a `file` before it is transformed | ||
or modify the transform options. | ||
|
||
```ts | ||
import type { FileBeforeParseHook } from '@nuxt/content' | ||
|
||
export default defineNuxtConfig({ | ||
hooks: { | ||
'content:file:beforeParse'(ctx: FileBeforeParseHook) { | ||
// ... | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
## `content:file:afterParse`{lang="ts"} | ||
|
||
This hook is called after the content is parsed and before it is saved to the database. | ||
|
||
```ts | ||
import type { FileAfterParseHook } from '@nuxt/content' | ||
|
||
export default defineNuxtConfig({ | ||
hooks: { | ||
'content:file:afterParse'(ctx: FileAfterParseHook) { | ||
// ... | ||
} | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { z } from 'zod' | ||
import { defineCollection } from '../../src/utils' | ||
import { resolveCollection } from '../../src/utils/collection' | ||
import { parseContent } from '../utils/content' | ||
import type { FileAfterParseHook, FileBeforeParseHook } from '../../src/types' | ||
|
||
describe('Hooks', () => { | ||
const collection = resolveCollection('hookTest', defineCollection({ | ||
type: 'data', | ||
source: 'content/**', | ||
schema: z.object({ | ||
body: z.any(), | ||
foo: z.any(), | ||
}), | ||
}))! | ||
it('content:file:beforeParse', async () => { | ||
let hookCtx: FileBeforeParseHook | ||
const nuxtMock = { | ||
callHook(hook: string, ctx: FileBeforeParseHook) { | ||
if (hook === 'content:file:beforeParse') { | ||
ctx.file.body = ctx.file.body.replace('replace-me', 'bar') | ||
hookCtx = ctx | ||
} | ||
}, | ||
} | ||
const content = await parseContent('content/index.md', `--- | ||
foo: 'replace-me' | ||
--- | ||
# Hello World | ||
`, collection, nuxtMock) | ||
expect(hookCtx.file.id).toEqual('content/index.md') | ||
expect(content.foo).toEqual('bar') | ||
}) | ||
it('content:file:afterParse', async () => { | ||
let hookCtx: FileAfterParseHook | ||
const nuxtMock = { | ||
callHook(hook: string, ctx: FileAfterParseHook) { | ||
if (hook === 'content:file:afterParse') { | ||
// augment | ||
ctx.content.bar = 'foo' | ||
hookCtx = ctx | ||
} | ||
}, | ||
} | ||
const parsed = await parseContent('content/index.md', `--- | ||
foo: 'bar' | ||
--- | ||
# Hello World | ||
`, collection, nuxtMock) | ||
expect(hookCtx.collection.name).toEqual('hookTest') | ||
expect(parsed.id).toEqual('content/index.md') | ||
expect(parsed.foo).toEqual('bar') | ||
expect(parsed.bar).toEqual('foo') | ||
}) | ||
}) |