-
-
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: call hooks before and after parse (#1160)
Co-authored-by: Sébastien Chopin <[email protected]>
- Loading branch information
Showing
6 changed files
with
154 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,79 @@ | ||
--- | ||
title: 'Advanced' | ||
description: 'Nuxt Content is highly customizable, giving you freedom and control over how the data is transformed.' | ||
icon: heroicons-outline:lightning-bolt | ||
--- | ||
|
||
## Hooks | ||
|
||
The module adds some hooks you can use: | ||
|
||
`content:file:*` hooks are available in nitro runtime, in order to use them you need to create custom [nitro plugin](https://nitro.unjs.io/guide/plugins.html). | ||
|
||
- Create a plugin in the `server/plugins/` directory | ||
|
||
```ts [server/plugins/content.ts] | ||
export default defineNitroPlugin((nitroApp) => { | ||
// ... | ||
}) | ||
``` | ||
|
||
- Register the plugin in `nuxt.config.ts` | ||
|
||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
// ... | ||
nitro: { | ||
plugins: ['~/server/plugins/content.ts'] | ||
} | ||
}) | ||
``` | ||
|
||
### `content:file:beforeParse` | ||
|
||
Allows you to modify the contents of a file before it is handled by the parsers. | ||
|
||
Arguments: | ||
- file | ||
- Type: `Object` | ||
- Properties: | ||
- _id: `String` | ||
- body: `String` | ||
|
||
### Example | ||
|
||
Changing all occurrences of react to vue in all Markdown files: | ||
|
||
|
||
```ts [server/plugins/content.ts] | ||
export default defineNitroPlugin((nitroApp) => { | ||
nitroApp.hooks.hook('content:file:beforeParse', (file) => { | ||
if (file._id.endsWith('.md')) { | ||
file.body = file.body.replace(/react/g, 'vue') | ||
} | ||
}) | ||
}) | ||
``` | ||
|
||
### `content:file:afterParse` | ||
|
||
Allows you to modify a document after being parsed by parsers. | ||
|
||
### Example | ||
|
||
Using content's first picture as cover image. | ||
|
||
```ts [server/plugins/content.ts] | ||
import { visit } from 'unist-util-visit' | ||
|
||
export default defineNitroPlugin((nitroApp) => { | ||
nitroApp.hooks.hook('content:file:afterParse', (file) => { | ||
if (file._id.endsWith('.md')) { | ||
visit(file.body, (n:any) => n.tag === 'img', (node) => { | ||
file.coverImage = node.props.src | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
``` |
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,37 @@ | ||
import { describe, test, expect, assert } from 'vitest' | ||
import { $fetch } from '@nuxt/test-utils' | ||
|
||
export const testParserHooks = () => { | ||
describe('parser:hooks', () => { | ||
test('beforeParse', async () => { | ||
const parsed = await $fetch('/api/parse', { | ||
method: 'POST', | ||
body: { | ||
id: 'content:index.md', | ||
content: '# hello' | ||
} | ||
}) | ||
|
||
expect(parsed).toHaveProperty('_id') | ||
assert(parsed._id === 'content:index.md') | ||
|
||
expect(parsed).toHaveProperty('__beforeParse', true) | ||
}) | ||
|
||
test('afterParse', async () => { | ||
const parsed = await $fetch('/api/parse', { | ||
method: 'POST', | ||
body: { | ||
id: 'content:index.md', | ||
content: '# hello' | ||
} | ||
}) | ||
|
||
expect(parsed).toHaveProperty('_id') | ||
assert(parsed._id === 'content:index.md') | ||
|
||
expect(parsed).haveOwnProperty('body') | ||
expect(parsed).toHaveProperty('__afterParse', true) | ||
}) | ||
}) | ||
} |
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,17 @@ | ||
export default defineNitroPlugin((nitroApp) => { | ||
nitroApp.hooks.hook('content:file:beforeParse', (file) => { | ||
if (file._id.endsWith('.md')) { | ||
if (file.body.startsWith('---')) { | ||
const lines = file.body.split('\n') | ||
lines.splice(1, 0, '__beforeParse: true') | ||
file.body = lines.join('\n') | ||
} else { | ||
file.body = '---\n__beforeParse: true\n---\n' + file.body | ||
} | ||
} | ||
}) | ||
|
||
nitroApp.hooks.hook('content:file:afterParse', (file) => { | ||
file.__afterParse = true | ||
}) | ||
}) |
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