Skip to content

Commit d8805bb

Browse files
authored
[Docs Site] Use dedent in Markdown component (#20129)
* [Docs Site] Use dedent in Markdown component * Add warning about MDX and Astro functionality
1 parent cbc3b45 commit d8805bb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/components/Markdown.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
22
import { z } from "astro:schema";
33
import { marked } from "marked";
4+
import dedent from "dedent";
45
56
type Props = z.infer<typeof props>;
67
78
const props = z.object({
8-
text: z.string(),
9+
text: z.string().transform((val) => dedent(val)),
910
inline: z.boolean().default(true),
1011
});
1112

src/content/docs/style-guide/components/markdown.mdx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
title: Markdown
33
---
44

5-
This component uses `marked` to turn the `text` prop into HTML. This is useful with, for example partial files that have variables you need to format.
5+
This component uses [`marked`](https://marked.js.org/) to render [CommonMark and various other Markdown flavours](https://marked.js.org/#specifications).
6+
7+
:::caution
8+
9+
This component can not use [MDX](https://mdxjs.com/) or [Astro](https://docs.astro.build/en/guides/markdown-content/) features, such as [optimised images in the assets directory](https://docs.astro.build/en/guides/images/#images-in-mdx-files).
10+
11+
Headings should not be used with this component, as they will not receive an `id`, copyable link or appear in the table of contents.
12+
13+
Code blocks should not be used with this component, as they will not receive syntax highlighting or a copy to clipboard button.
14+
15+
:::
616

717
```mdx live
818
import { Markdown } from "~/components";
@@ -18,4 +28,27 @@ If you have a variable that needs to be formatted in any special way (for exampl
1828
<Markdown text={props.foo} />
1929
```
2030

21-
Note that you need to wrap your variable in curly braces, as well as use `text=` or this will not work.
31+
Note that you need to wrap your variable in curly braces, as well as use `text=` or this will not work.
32+
33+
## Multi-line strings
34+
35+
The Markdown component uses the [`dedent`](https://www.npmjs.com/package/dedent) library to remove indentation from multi-line strings.
36+
37+
This is because the [CommonMark spec](https://spec.commonmark.org/0.22/#indented-code-blocks) treats indented text as code blocks, unlike [MDX](https://mdxjs.com/docs/what-is-mdx/#:~:text=Indented%20code%20does%20not%20work%20in%20MDX%3A).
38+
39+
```mdx live
40+
import { Markdown } from "~/components";
41+
42+
<>
43+
<Markdown
44+
text={`
45+
You need to purchase [Magic WAN](https://www.cloudflare.com/magic-wan/) before you can purchase and use the Magic WAN Connector. The Magic WAN Connector can function as your primary edge device for your network, or be deployed in-line with existing network gear.
46+
47+
You also need to purchase a Magic WAN Connector before you can start configuring your settings in the Cloudflare dashboard. After buying a Magic WAN Connector, the device will be registered with your Cloudflare account and show up in your Cloudflare dashboard.
48+
49+
Contact your account representative to learn more about purchasing options for the Magic WAN Connector device.
50+
`}
51+
inline={false}
52+
/>
53+
</>
54+
```

0 commit comments

Comments
 (0)