webpack可以从多个markdown入口点生成frontmatter的单个json文件吗?

问题描述

我有一个webpack.config.js,它使用动态入口点来获取文件夹中的markdown文件

module.exports = {
  async entry() {
    // get a list of .mdx files
    const mdxFiles = await glob('./content/**/*.md{,x}',{ dot: devMode })
      // map each file to a "pretty" keyed tuple
      .then(list => list.map(filename => [filename.replace(/\.mdx?$/,''),filename]))
      .then(Object.fromEntries) // into an object
    return { main: './src/client/index',...mdxFiles } 
    // semantically i'd prefer mdxFiles was an array,but this
    // is the easiest way to get separate output files for each input
  },// more webpack config...
}

使用custom loader from the mdx docs

const matter = require('gray-matter')
const stringifyObject = require('stringify-object')
module.exports = function mdxLoader(src) {
  const { content,data } = matter(src)
  return `export const Metadata = ${stringifyObject(data)};\n\n${content}`
}

这会将markdown转换为jsx,然后使用@mdx-js/loaderbabel-loader转换为React调用。每个markdown文件中的前题都转换为

export const Metadata = { title: "hello world",description,...etc};

// export default Mdxcomponent...

我正在尝试编写a custom plugin以将所有元数据提取到单个文件中,但据我所知,所有babel解析上下文都丢失了,并且代码只是准备好了一大串JavaScript写入磁盘。

不是将正则表达式解析为hack,而是将每个文件中的元数据export提取到单个对象中以进行字符串化并添加到Webpack清单中的最佳方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)