如何使用WebPack 5和Docusaurus读取Markdown File的原始内容

发布于 2025-02-09 15:05:03 字数 1375 浏览 2 评论 0原文

我目前正在重构一个Docusaurus应用程序,该应用程序显示了Markdown Docs的开发人员文档。该应用具有“快速启动”功能,该功能具有向导,用户填写信息,然后填充Markdown模板。

该应用程序用于使用webpack 4和raw-loader读取文档模板的内容,然后与用户传递的内容进行查找和替换。自从我切换到WebPack 5,raw--装载机不再起作用。我在WebPack 5中读到,由于该功能已内置在WebPack中,因此不再需要使用RAW-LOADERhttps://webpack.js.s.js.org/guides/guides/guides/asset-modules/asset-modules/asset-modules/# source-assets

我创建了webpack.config.js文件:

module.exports = {
  module: {
    rules: [
      {
        resourceQuery: /raw/,
        type: 'asset/source'
      }
    ]
  }
};

我有一个markdown文件/docs/template.md

# My template

## Some section 1

some text

## Some section 2

some text

在我的js中文件我试图导入类似的内容:

import template from '@site/docs/template.md?raw';

console.log(template);

但是,webpack没有将markdown文件的内容作为字符串加载,我会得到一些MDX内容:

ƒ MDXContent(_ref){let{components,...props}=_ref;return
(0,_mdx_js_react__WEBPACK_IMPORTED_MODULE_2__.mdx)(MDXLayout
(0,_Users_kevin_apps_docs_node_modules_babel_runtime_helpers_esm_ext…

对我做错了什么有任何想法吗?

I am currently refactoring a Docusaurus app that displays developer documentation from markdown docs. The app has a "quick start" feature which has a wizard that users fill out info and then it populates a markdown template.

The app used to use webpack 4 and raw-loader to read the contents of an documentation template and then do a find and replace with what the user passed in. Ever since I switched to webpack 5, raw-loader no longer works. I read in webpack 5 that the use of raw-loader was no longer necessary as that functionality is built into webpack; https://webpack.js.org/guides/asset-modules/#source-assets

I created a webpack.config.js file:

module.exports = {
  module: {
    rules: [
      {
        resourceQuery: /raw/,
        type: 'asset/source'
      }
    ]
  }
};

I have a markdown file /docs/template.md:

# My template

## Some section 1

some text

## Some section 2

some text

And in my js file I attempt to import the contents like so:

import template from '@site/docs/template.md?raw';

console.log(template);

However webpack is not loading the contents of markdown file as a string, I get some mdx content:

ƒ MDXContent(_ref){let{components,...props}=_ref;return
(0,_mdx_js_react__WEBPACK_IMPORTED_MODULE_2__.mdx)(MDXLayout
(0,_Users_kevin_apps_docs_node_modules_babel_runtime_helpers_esm_ext…

Any idea on what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-02-16 15:05:03

我不认为Docusaurus网站尊重webpack.config.js文件,所以我用docusaurus.config.js的Inline插件模块解决了此问题:

/** @type {import('@docusaurus/types').Config} */
module.exports = {
  // ...other configs...,
  plugins: [
    // ...other plugins...,
    () => ({
      name: 'my-raw-loader',
      configureWebpack() {
        return {
          module: {
            rules: [
              {
                resourceQuery: /raw/,
                type: 'asset/source',
              },
            ],
          },
        };
      },
    }),
  ],
}

I don't think Docusaurus sites respect webpack.config.js files, so I resolved this issue with an inline plugin module in docusaurus.config.js:

/** @type {import('@docusaurus/types').Config} */
module.exports = {
  // ...other configs...,
  plugins: [
    // ...other plugins...,
    () => ({
      name: 'my-raw-loader',
      configureWebpack() {
        return {
          module: {
            rules: [
              {
                resourceQuery: /raw/,
                type: 'asset/source',
              },
            ],
          },
        };
      },
    }),
  ],
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文