nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.09k stars 623 forks source link

No way to pass config options to a transformer #2086

Open fooooooooooooooo opened 1 year ago

fooooooooooooooo commented 1 year ago

Environment


Reproduction

https://codesandbox.io/p/github/fooooooooooooooo/content-config/master https://github.com/fooooooooooooooo/content-config

Describe the bug

It is not possible to pass any config to custom transformers, as the parseContent function is hardcoded to use the content options: https://github.com/nuxt/content/blob/main/src/runtime/server/storage.ts#L164-L191 The transformContent function then gets options[module name] which will always be empty for custom transformers: https://github.com/nuxt/content/blob/main/src/runtime/transformers/index.ts#L59

Additional context

parseContent isn't given any options here: https://github.com/nuxt/content/blob/main/src/runtime/server/storage.ts#L154 there are also various type errors:

Logs

No response

Skyost commented 10 months ago

This issue is still valid. Currently, custom transformers are almost unusable due to this.

platform-kit commented 8 months ago

@Skyost I'm trying to dynamically add a field to some markdown files. Do you know of any workarounds to this lack of support for custom transformers?

Skyost commented 8 months ago

It's not related, but sure.

Create a modules/your_module/index.ts file :

import { createResolver, defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  meta: {
    name: 'your-module',
    version: '0.0.1',
    compatibility: { nuxt: '^3.0.0' }
  },
  setup: (_, nuxt) => {
    const resolver = createResolver(import.meta.url)

    // Update Nitro config.
    nuxt.hook('nitro:config', (config) => {
      config.plugins = config.plugins || []
      config.plugins.push(resolver.resolve('plugin'))
    })
  }
})

Then create a modules/your_module/plugin.ts file :

export default defineNitroPlugin((nitroApp) => {
  // @ts-ignore
  nitroApp.hooks.hook('content:file:afterParse', (file) => {
    if (!file._id.endsWith('.md')) {
      return
    }
    file['custom-field'] = 'Custom value.'
  })
})
platform-kit commented 8 months ago

@Skyost thanks for the pointer. I'm actually trying to pass the original raw markdown, or at least the raw body/content (before parsing) as a file['_raw'] property , do you know how I can access that data in this context?