Gatsby MDX和根相对路径

问题描述

在MDX文件中有两次,我想使用root相对而不是相对路径。 MDX页面是深层嵌套的。所以我最终遇到了许多../../..

文件夹示例为:

/content/products/
/content/products/brands/some-brand/index.mdx
/content/products/brands/some-brand/some-feature/first-product.mdx
/content/images/brand-images/some-brand.png
/content/images/brand-images/other-brand.png
/src/components/products/

前题图像路径

我关注了this example

---
title: Some title
brand-image: ../../../images/brand-images/some-brand.png
---

组件

import Products from '../../../../src/components/products';

这可能吗?

---
title: Some title
brand-image: ${PROJECT_ROOT}/content/images/brand-images/some-brand.png
---

import Products from '${PROJECT_ROOT}/src/components/products';

Hi,I'm a page with many products. It's just to show,how I use the MDX pages.

<Products pkey="some-brand" tags={/.*Corp.*Gov.*/}/>

解决方法

看到一个赞成票并提出了这个老问题。所以,这是解决方案。

Frontmatter Image Paths and Components

https://www.gatsbyjs.com/plugins/gatsby-remark-normalize-paths/ 解决了这个问题。

仅组件

使用 shortcodes 我们可以完全消除显式组件的需要 在 MDX 文件中导入。

假设您有一个“围绕”MDX 内容的模板。

...
<MDXRenderer>{body}</MDXRenderer>
...

假设您在 MDX 内容文件中使用了自定义组件 <BlueBox />。在模板中导入这个组件并用 <MDXRenderer>

包裹 <MDXProvider>
...
import BlueBox from "../components/mdx/blue-box"
import AnotherComponent from "../components/mdx/another-component"
...
<MDXProvider components={{BlueBox,AnotherComponent}}><MDXRenderer>{body}</MDXRenderer></MDXProvider>
...