Markdown Features
导入 Component
import BrowserWindow from "@site/src/components/BrowserWindow";
<BrowserWindow>Hello, world</BrowserWindow>;
https://hcynus.eu.org
Hello, world
导入代码片段
可以将任何代码文件作为原始文本导入,然后将其插入到代码块中,这要归功于Webpack raw-loader。
使用前需要安装 raw-loader
:
- npm
- Yarn
- pnpm
npm install --save raw-loader
yarn add raw-loader
pnpm add raw-loader
使用:
myMarkdownFile.mdx
import CodeBlock from "@theme/CodeBlock";
import MyComponentSource from "!!raw-loader!./assets/myComponent";
<CodeBlock language="jsx">{MyComponentSource}</CodeBlock>;
https://hcynus.eu.org
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, { useState } from "react";
export default function MyComponent() {
const [bool, setBool] = useState(false);
return (
<div>
<p>MyComponent rendered !</p>
<p>bool={bool ? "true" : "false"}</p>
<p>
<button onClick={() => setBool((b) => !b)}>toggle bool</button>
</p>
</div>
);
}
导入 Markdown
可以将 Markdown 文件作为组件使用,并在其他地方导入,无论是在 Markdown 文件中还是在 React 页面中。
_markdown-partial-example.mdx
<span>Hello {props.name}</span>
This is text some content from `_markdown-partial-example.mdx`.
import PartialExample from "./assets/_markdown-partial-example.mdx";
<PartialExample name="Sebastien" />;