Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document transformers #1453

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/content/4.api/4.advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,72 @@ export default defineNitroPlugin((nitroApp) => {
})

```

## Transformers

Transformers are responsible for parsing and manipulating contents in the content module.
Internally, the module has specific transformer for each content type to parse the raw content and prepare it for querying and rendering.
farnabaz marked this conversation as resolved.
Show resolved Hide resolved

You can create custom transformers to support new content types or improve functionalities of supported content types.

1. Create your transformer. A transformer consists of 4 parts:
- `name`: Transformer name
- `extensions`: List of valid file extensions
- `parse`: If provided this function will be used to parsed the raw content
- `transform`: Received that parsed content and manipulate the content.

```ts [my-transformer.ts]
// filename: my-transformer.ts
import { defineTransformer } from '@nuxt/content/transformers'

export default defineTransformer({
name: 'my-transformer',
extensions: ['.names'],
parse (_id, rawContent) {
return {
_id,
body: rawContent.trim().split('\n').map(line => line.trim()).sort()
}
}
})

```

2. Define simple module to register transformer

```ts [my-module.mjs]
// filename: my-module.mjs
import { resolve } from 'path'
import { defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
setup (_options, nuxt) {
nuxt.options.nitro.externals = nuxt.options.nitro.externals || {}
nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || []
nuxt.options.nitro.externals.inline.push(resolve('./my-module'))
// @ts-ignore
nuxt.hook('content:context', (contentContext) => {
contentContext.transformers.push(resolve('./my-module/my-transformer.ts'))
})
}
})

```

3. Register your module

```ts [nuxt.config.ts]
// filename: my-module.mjs
import { resolve } from 'path'
import { defineNuxtConfig } from '@nuxt/kit'
import MyModule from './my-module'

export default defineNuxtConfig({
modules: [
MyModule,
'@nuxt/content'
]
})
```

That's it. You can create `.names` files in content directory. Chechout transformer example.
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions examples/advanced/transformer/content/1.index.names
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
John
Joes
Jessi
Jason
14 changes: 14 additions & 0 deletions examples/advanced/transformer/my-module/my-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { resolve } from 'path'
import { defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
setup (_options, nuxt) {
nuxt.options.nitro.externals = nuxt.options.nitro.externals || {}
nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || []
nuxt.options.nitro.externals.inline.push(resolve('./my-module'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any way to simplify these 3 lines above @pi0 ?

Someting like

addServerExternal(resolve('./my-module'))

// @ts-ignore
nuxt.hook('content:context', (contentContext) => {
contentContext.transformers.push(resolve('./my-module/my-transformer.ts'))
})
}
})
12 changes: 12 additions & 0 deletions examples/advanced/transformer/my-module/my-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineTransformer } from '@nuxt/content/transformers'

export default defineTransformer({
name: 'my-transformer',
extensions: ['.names'],
parse (_id, rawContent) {
return {
_id,
body: rawContent.trim().split('\n').map(line => line.trim()).sort()
}
}
})
9 changes: 9 additions & 0 deletions examples/advanced/transformer/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineNuxtConfig } from 'nuxt'
import MyModule from './my-module/my-module'

export default defineNuxtConfig({
modules: [
MyModule,
'@nuxt/content'
]
})
14 changes: 14 additions & 0 deletions examples/advanced/transformer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "example-hello-world",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxt/content": "npm:@nuxt/content-edge@latest",
"nuxt": "npm:nuxt3@latest"
}
}
25 changes: 25 additions & 0 deletions examples/advanced/transformer/pages/[...slug].vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div class="content-page">
<ContentDoc v-slot="{ doc }">
<div>
{{ doc }}
</div>
</ContentDoc>
</div>
</template>

<script setup lang="ts">
definePageMeta({
layout: 'default',
layoutTransition: false
})
</script>

<style>
.content-page {
height: calc(100vh - 60px);
max-height: calc(100vh - 60px);
padding: 1rem;
margin: 0;
}
</style>