content icon indicating copy to clipboard operation
content copied to clipboard

No way to pass config options to a transformer

Open fooooooooooooooo opened this issue 1 year ago • 4 comments

Environment


  • Operating System: Windows_NT
  • Node Version: v19.9.0
  • Nuxt Version: 3.5.2
  • Nitro Version: 2.4.1
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: modules, content
  • Runtime Modules: normalizedModule(), @nuxt/[email protected]
  • Build Modules: -

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:

  • missing nuxt hook key https://github.com/fooooooooooooooo/content-config/blob/master/my-module.ts#L17-L18
  • defineTransformer import only resolves at runtime https://github.com/fooooooooooooooo/content-config/blob/master/my-transformer.ts#L2-L3
  • NuxtModule<MyModuleOptions> is not assignable to ___ https://github.com/fooooooooooooooo/content-config/blob/master/nuxt.config.ts#L4-L5

Logs

No response

fooooooooooooooo avatar May 30 '23 19:05 fooooooooooooooo

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

Skyost avatar Dec 04 '23 11:12 Skyost

@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?

platform-kit avatar Jan 08 '24 07:01 platform-kit

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.'
  })
})

Skyost avatar Jan 08 '24 21:01 Skyost

@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?

platform-kit avatar Jan 11 '24 02:01 platform-kit