|
| 1 | +# MDX |
| 2 | + |
| 3 | +With Nextra, all your `.md` and `.mdx` files under the pages directory will be rendered with [MDX](https://mdxjs.com/about), it's an |
| 4 | +advanced Markdown format with React component support. |
| 5 | + |
| 6 | +You can use import and use React components inside your Markdown files like this: |
| 7 | + |
| 8 | +```markdown |
| 9 | +import Callout from 'nextra-theme-docs/callout' |
| 10 | + |
| 11 | +**Markdown With React Components** |
| 12 | + |
| 13 | +<Callout emoji="✅"> |
| 14 | + **MDX** (the library), at its core, transforms MDX (the syntax) to JSX. |
| 15 | + It receives an MDX string and outputs a _JSX string_. It does this by parsing |
| 16 | + the MDX document to a syntax tree and then generates a JSX document from that tree. |
| 17 | +</Callout> |
| 18 | +``` |
| 19 | + |
| 20 | +Generates: |
| 21 | + |
| 22 | +import Callout from 'nextra-theme-docs/callout' |
| 23 | + |
| 24 | +<div className="p-4 border border-gray-200 dark:border-gray-900 rounded mt-6"> |
| 25 | +**Markdown With React Components** |
| 26 | + |
| 27 | +<Callout emoji="✅"> |
| 28 | + **MDX** (the library), at its core, transforms MDX (the syntax) to JSX. It |
| 29 | + receives an MDX string and outputs a _JSX string_. It does this by parsing the |
| 30 | + MDX document to a syntax tree and then generates a JSX document from that |
| 31 | + tree. |
| 32 | +</Callout> |
| 33 | +</div> |
| 34 | + |
| 35 | +## Heading |
| 36 | + |
| 37 | +<br /> |
| 38 | + |
| 39 | +# **Hello**, This Is a _Title_ Inside `h1` |
| 40 | + |
| 41 | +<h2>**Hello**, This Is a _Title_ Inside `h2`</h2> |
| 42 | +{/* using html tag to avoid being rendered in the sidebar */} |
| 43 | + |
| 44 | +### **Hello**, This Is a _Title_ Inside `h3` |
| 45 | + |
| 46 | +#### **Hello**, This Is a _Title_ Inside `h4` |
| 47 | + |
| 48 | +##### **Hello**, This Is a _Title_ Inside `h5` |
| 49 | + |
| 50 | +###### **Hello**, This Is a _Title_ Inside `h6` |
| 51 | + |
| 52 | +## List |
| 53 | + |
| 54 | +1. one |
| 55 | +2. two |
| 56 | +3. three |
| 57 | + |
| 58 | +- one |
| 59 | +- two |
| 60 | +- three |
| 61 | + |
| 62 | +## Task List |
| 63 | + |
| 64 | +```markdown |
| 65 | +- [x] Write the press release |
| 66 | +- [ ] Update the website |
| 67 | +- [ ] Contact the media |
| 68 | +``` |
| 69 | + |
| 70 | +Renders |
| 71 | + |
| 72 | +- [x] Write the press release |
| 73 | +- [ ] Update the website |
| 74 | +- [ ] Contact the media |
| 75 | + |
| 76 | +## Syntax Highlighting |
| 77 | + |
| 78 | +Automatic syntax highlighting: |
| 79 | + |
| 80 | +````markdown |
| 81 | +```js |
| 82 | +console.log('hello, world') |
| 83 | +``` |
| 84 | +```` |
| 85 | + |
| 86 | +Renders: |
| 87 | + |
| 88 | +```js |
| 89 | +console.log('hello, world') |
| 90 | +``` |
| 91 | + |
| 92 | +You can also add the `highlight=<line|range>` modifier to highlight specific lines: |
| 93 | + |
| 94 | +````markdown |
| 95 | +```jsx highlight=4,6-8 |
| 96 | +import useSWR from 'swr' |
| 97 | + |
| 98 | +function Profile() { |
| 99 | + const { data, error } = useSWR('/api/user', fetcher) |
| 100 | + |
| 101 | + if (error) return <div>failed to load</div> |
| 102 | + if (!data) return <div>loading...</div> |
| 103 | + return <div>hello {data.name}!</div> |
| 104 | +} |
| 105 | +``` |
| 106 | +```` |
| 107 | + |
| 108 | +```jsx highlight=4,6-8 |
| 109 | +import useSWR from 'swr' |
| 110 | + |
| 111 | +function Profile() { |
| 112 | + const { data, error } = useSWR('/api/user', fetcher) |
| 113 | + |
| 114 | + if (error) return <div>failed to load</div> |
| 115 | + if (!data) return <div>loading...</div> |
| 116 | + return <div>hello {data.name}!</div> |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Inline Code |
| 121 | + |
| 122 | +You can use \`content\` to wrap inline code content like: `let x = 1`. |
| 123 | + |
| 124 | +## Blockquote |
| 125 | + |
| 126 | +> Where some people measure progress in answers-right per test or tests-passed per year, we are more interested in Sistine-Chapel-Ceilings per Lifetime. |
| 127 | +> |
| 128 | +> — Alan Kay, A Personal Computer for Children of All Ages |
| 129 | +
|
| 130 | +Nested quotes: |
| 131 | + |
| 132 | +> > Where some people measure progress in answers-right per test or tests-passed per year, we are more interested in Sistine-Chapel-Ceilings per Lifetime. |
| 133 | +> > |
| 134 | +> > — Alan Kay, A Personal Computer for Children of All Ages |
| 135 | +> |
| 136 | +> This is **great**. |
| 137 | +> |
| 138 | +> — Shu Ding. |
| 139 | +
|
| 140 | +## Table |
| 141 | + |
| 142 | +| Syntax | Description | Test Text | |
| 143 | +| :------------ | :---------: | ----------: | |
| 144 | +| Header | Title | Here's this | |
| 145 | +| Paragraph | Text | And more | |
| 146 | +| Strikethrough | | ~~Text~~ | |
| 147 | + |
| 148 | +## React Components |
| 149 | + |
| 150 | +React components and Markdown can be **mixed together**, for instance: |
| 151 | + |
| 152 | +```markdown |
| 153 | +> <Callout> |
| 154 | +> Give [**Nextra**](https://github.com/shuding/nextra) a star! |
| 155 | +> </Callout> |
| 156 | +``` |
| 157 | + |
| 158 | +Renders: |
| 159 | + |
| 160 | +> <Callout> |
| 161 | +> Give [**Nextra**](https://github.com/shuding/nextra) a star! |
| 162 | +> </Callout> |
0 commit comments