Link 到静态 JSON 文件

Link to static JSON file

在 Docusaurus V2 中如何 link 到静态文件夹中的 JSON 文件?

我在降价文件中尝试了以下内容:

An exemple, is the following [JSON dataset](../../static/data/solar-radiation.json).

但是 Docusaurus 会产生以下错误:

./static/data/solar-radiation.json (./node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./static/data/solar-radiation.json)
Module parse failed: Unexpected token e in JSON at position 0 while parsing near 'export default __web...'
File was processed with these loaders:
 * ./node_modules/file-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
SyntaxError: Unexpected token e in JSON at position 0 while parsing near 'export default __web...'
    at JSON.parse (<anonymous>)

我的文件有效 JSON。出于某种原因,Docusaurus 似乎没有显示静态文件而是试图解析它...

我看到了同样的问题。我的临时解决方案是:

  • 将文件名从 .md 更改为 .mdx
  • 将此 link 添加到您的 json 文件
<a target="_blank" href="/json/file.json" download="file.json">Download</a>

我也遇到了同样的问题!

根据 Issue #3561 on the Docusaurus GitHub,一位开发人员的说法是,当前的最佳做法是在 URL 前面使用 pathname:// 前缀。要使用您的示例:

An exemple, is the following
[JSON dataset](pathname://../../static/data/solar-radiation.json).

显然发生这种情况的原因是因为 babel-loader(用于加载代码)和 file-loader(用于加载静态资产)之间存在冲突。 pathname:// 前缀会跳过这两个加载程序,而只会创建一个 link。