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 a custom nitro plugin.
Create a plugin in the server/plugins/ directory
export default defineNitroPlugin((nitroApp) => {
// ...
})
content:file:beforeParseAllows you to modify the contents of a file before it is handled by the parsers.
Arguments:
{ _id: string, body: string }Example:
Changing all occurrences of React to Vue in all Markdown files:
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:afterParseAllows you to modify a document after being parsed by parsers.
Arguments:
{ _id: string, body: any }Example:
Using content's first picture as cover image.
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
})
}
})
})
[
{
"title": "useContent()",
"path": "/content-v2/document-driven/use-content",
"stem": "content-v2/5.document-driven/2.use-content",
"description": "The useContent() composable gives access to the current page, surrounding pages and global data."
},
{
"title": "Transformers",
"path": "/content-v2/recipes/transformers",
"stem": "content-v2/6.recipes/2.transformers",
"description": "Transformers are responsible for parsing and manipulating contents in Nuxt Content."
}
]