nuxt / content

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

Dynamic content sources #2503

Closed 9uenther closed 9 months ago

9uenther commented 10 months ago

Is the use of dynamic sources possible?

Maybe in this simplified form:

export default defineNuxtConfig({
    modules: [
        '@nuxt/content',
    ],
    content: {
        sources: {
            content: {
                driver: 'fs',
                prefix: '*', // should be set dynamically
                base: '~/modules/*/content',
            }
        }
    }
})

I am currently using this less elegant solution:

export default defineNuxtConfig({
    modules: [
        '@nuxt/content',
    ],
    content: {
        sources: {
            ...fs.readdirSync(path.resolve(__dirname, 'modules')).reduce((acc, _module) => {
                const contentPath = path.join(path.resolve(__dirname, 'modules'), _module, 'content')
                if (fs.existsSync(contentPath) && fs.statSync(contentPath).isDirectory()) {
                    acc[_module] = {
                        driver: 'fs',
                        prefix: `/${_module}`,
                        base: contentPath
                    };
                }
                return acc
            }, {})
        }
    }
}
farnabaz commented 9 months ago

Thanks for the issue. Currently, there is no other way for creating dynamic sources. Your solution is perfectly fine IMO. Maybe you can create a util instead of inline approach.

This feature is not planned for the next major release. Since it can be achieved via small util, I'm closing it as not planned.