MDX 的一个优点是:你可以为 markdown 标记指定某个组件进行渲染 而不是使用 HTML 元素。 下表列出了可以被覆盖的 HTML 元素 以及其所对应的 markdown 标记, 还有默认所对应的 JSX 组件。 但是,以第一行的 a 为例,可以使用 <FancyLink> 来替换超链接,步骤如下:
TypeScript
importReadmefrom'./readme.md'// Assumes an integration is used to compile MDX -> JS.import {FancyLink} from'./components/fancy-link.js'
<Readmecomponents={{a: FancyLink}} />
<>
<p>Two <strong>asterisks</strong> for strong.</p>
</>
ul
* asterisks for unordered
<>
<ul>
<li>asterisks for unordered</li>
</ul>
</>
The components you can overwrite use their standard HTML names. Normally, in markdown, those names are: a, blockquote, br, code, em, h1, h2, h3, h4, h5, h6, hr, img, li, ol, p, pre, strong, and ul. With remark-gfm (see guide), you can also use: del, input, section, sup, table, tbody, td, th, thead, and tr. Other remark plugins that add support for new constructs and advertise that they work with rehype, will also work with MDX.