Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(v2): add documentation about importing code files #4974

Merged
merged 3 commits into from
Jun 15, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions website/docs/guides/markdown-features/markdown-features-react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ Since all doc files are parsed using MDX, any HTML is treated as JSX. Therefore,

:::

## Importing code snippets {#importing-code-snippets}

You can not only import a file containing a component definition, but also import any code file as raw text, and then insert it in a code block, thanks to [Webpack raw-loader](https://webpack.js.org/loaders/raw-loader/).

```jsx title="myMarkdownFile.mdx"
import CodeBlock from '@theme/CodeBlock';
import MyComponentSource from '!!raw-loader!./myComponent';

<CodeBlock className="language-jsx">{MyComponentSource}</CodeBlock>;
```

import CodeBlock from '@theme/CodeBlock';
import MyComponentSource from '!!raw-loader!@site/src/pages/examples/_myComponent';

<BrowserWindow url="http://localhost:3000">

<CodeBlock className="language-jsx">{MyComponentSource}</CodeBlock>

</BrowserWindow>

<br />

:::note

You have to use `<CodeBlock>` rather than the Markdown triple-backtick ` ``` `, because the latter will ship out any of its content as-is, but you want JSX to insert the imported text here.

:::
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a warning here and mention that this is experimental and API breaking changes are expected on this

we'll replace raw loader by https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will work on this in a moment

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll replace raw loader by https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax

Should I change my demo case in the documentation, or is this just a plan?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't support ?raw syntax yet so we'll document it once we support it, for now you can keep documenting raw-loader usage

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add a warning then, or should we just keep it in mind and come back once the support is in place?


:::warning

The `className="language-jsx"` prop is experimental and might be subject to breaking API changes in the future.
slorber marked this conversation as resolved.
Show resolved Hide resolved

:::

## Importing Markdown {#importing-markdown}

You can use Markdown files as components and import them elsewhere, either in Markdown files or in React pages. Below we are importing from [another file](./markdown-features-intro.mdx) and inserting it as a component.
Expand All @@ -82,10 +116,10 @@ import Intro from './markdown-features-intro.mdx';
<Intro />;
```

<BrowserWindow url="http://localhost:3000">

import Intro from './markdown-features-intro.mdx';

<BrowserWindow url="http://localhost:3000">

<Intro />

</BrowserWindow>
Expand Down