Skip to content

Commit c6eddf0

Browse files
authored
Snippets v3 docs (#2363)
* add snippets prop * update path info in React page * update snippets page * reviewer feedback
1 parent 2af31a5 commit c6eddf0

File tree

3 files changed

+121
-83
lines changed

3 files changed

+121
-83
lines changed

create/reusable-snippets.mdx

Lines changed: 110 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,129 +4,157 @@ description: "Create reusable snippets to maintain consistency across pages."
44
keywords: ["content snippets", "reusable content", "variables"]
55
---
66

7-
One of the core principles of software development is DRY (Don't Repeat
8-
Yourself), which applies to documentation as
9-
well. If you find yourself repeating the same content in multiple places, you
10-
should create a custom snippet to keep your content in sync.
7+
One of the core principles of software development is DRY (Don't Repeat Yourself), which applies to documentation too. If you find yourself repeating the same content in multiple places, create a custom snippet for that content. Snippets contain content that you can import into other files to reuse. You control where the snippet appears on a page. If you ever need to update the content, you only need to edit the snippet rather than every file where the snippet is used.
118

12-
## Creating a custom snippet
9+
Snippets do not render as standalone pages. You must import them into other files.
1310

14-
**Pre-condition**: You must create your snippet file in the `snippets` directory in order for the import to work.
11+
## Configure snippet folders
12+
13+
By default, any file in a folder named `snippets` is treated as a snippet. You can configure additional custom folders to contain snippets with the `snippets` field in your `docs.json`.
14+
15+
Add [glob patterns](https://code.visualstudio.com/docs/editor/glob-patterns) to the `snippets` array in `docs.json` to specify which folders contain snippets.
16+
17+
```json docs.json
18+
{
19+
"snippets": [
20+
"components/**",
21+
"shared/reusable/**",
22+
"shared/common-component.mdx"
23+
]
24+
}
25+
```
26+
27+
## Create snippets
28+
29+
Create a file in a snippet folder with the content you want to reuse. Snippets can contain all content types supported by Mintlify and they can import other snippets.
30+
31+
## Import snippets into pages
32+
33+
To use snippets, import them into pages using either an absolute or relative path.
34+
35+
- **Absolute imports**: Start with `/snippets/` or your custom snippet location for imports from the root of your project.
36+
- **Relative imports**: Use `./` or `../` to import snippets relative to the current file's location.
1537

1638
<Tip>
17-
Snippets support both absolute imports (starting with `/snippets/`) and
18-
relative imports (starting with `./` or `../`).
39+
Relative imports enable IDE navigation. Press <kbd>CMD</kbd> and click a snippet name in your editor to jump directly to the snippet definition.
1940
</Tip>
2041

21-
Any page in the `snippets` directory will be treated as a snippet and will not
22-
be rendered into a standalone page. If you want to create a standalone page
23-
from the snippet, import the snippet into another file and call it as a
24-
component.
42+
### Import text
2543

26-
### Default export
44+
1. Add content to your snippet file that you want to reuse.
2745

28-
1. Add content to your snippet file that you want to re-use.
46+
```mdx snippets/my-snippet.mdx
47+
Hello world! This is my content I want to reuse across pages.
48+
```
2949

30-
```typescript snippets/my-snippet.mdx
31-
Hello world! This is my content I want to reuse across pages.
32-
```
50+
2. Import the snippet into your destination file using either an absolute or relative path.
3351

34-
2. Import the snippet into your destination file.
52+
<CodeGroup>
3553

36-
```typescript destination-file.mdx
37-
---
38-
title: My title
39-
description: My Description
40-
---
54+
```mdx Absolute import
55+
---
56+
title: "An example page"
57+
description: "This is an example page that imports a snippet."
58+
---
4159

42-
import MySnippet from '/snippets/path/to/my-snippet.mdx';
60+
import MySnippet from '/snippets/my-snippet.mdx';
4361

44-
## Header
62+
The snippet content displays beneath this sentence.
4563

46-
Lorem impsum dolor sit amet.
64+
<MySnippet/>
65+
```
4766

48-
<MySnippet/>
67+
```mdx Relative import
68+
---
69+
title: "An example page"
70+
description: "This is an example page that imports a snippet."
71+
---
72+
73+
import MySnippet from '../snippets/my-snippet.mdx';
4974

50-
```
75+
The snippet content displays beneath this sentence.
5176

52-
### Exporting with variables
77+
<MySnippet/>
78+
```
5379

54-
1. Optionally, you can add variables that can be filled in via props when you import the snippet. In this example, our variable is word.
80+
</CodeGroup>
5581

56-
```typescript snippets/my-snippet.mdx
57-
My keyword of the day is {word}.
58-
```
82+
### Import variables
5983

60-
2. Import the snippet into your destination file with the variable. The property will fill in based on your specification.
84+
Reference variables from a snippet in a page.
6185

62-
```typescript destination-file.mdx
63-
---
64-
title: My title
65-
description: My Description
66-
---
86+
1. Export variables from a snippet file.
6787

68-
import MySnippet from '/snippets/path/to/my-snippet.mdx';
88+
```mdx snippets/custom-variables.mdx
89+
export const myName = "Ronan";
6990

70-
## Header
91+
export const myObject = { fruit: "strawberries" };
92+
```
7193

72-
Lorem impsum dolor sit amet.
94+
2. Import the snippet from your destination file and use the variable.
7395

74-
<MySnippet word="bananas" />
96+
```mdx destination-file.mdx
97+
---
98+
title: "An example page"
99+
description: "This is an example page that imports a snippet with variables."
100+
---
75101

76-
```
102+
import { myName, myObject } from '/snippets/custom-variables.mdx';
77103

78-
### Reusable variables
104+
Hello, my name is {myName} and I like {myObject.fruit}.
105+
```
79106

80-
1. Export a variable from your snippet file:
107+
### Import snippets with variables
81108

82-
```typescript snippets/path/to/custom-variables.mdx
83-
export const myName = "my name";
109+
Use variables to pass data to a snippet when you import it.
84110

85-
export const myObject = { fruit: "strawberries" };
86-
```
111+
1. Add variables to your snippet and pass in properties when you import it. In this example, the variable is `{word}`.
87112

88-
2. Import the snippet from your destination file and use the variable:
113+
```mdx snippets/my-snippet.mdx
114+
My keyword of the day is {word}.
115+
```
89116

90-
```typescript destination-file.mdx
91-
---
92-
title: My title
93-
description: My Description
94-
---
117+
2. Import the snippet into your destination file with the variable. The passed property replaces the variable in the snippet definition.
95118

96-
import { myName, myObject } from '/snippets/path/to/custom-variables.mdx';
119+
```mdx destination-file.mdx
120+
---
121+
title: "An example page"
122+
description: "This is an example page that imports a snippet with a variable."
123+
---
97124

98-
Hello, my name is {myName} and I like {myObject.fruit}.
99-
```
125+
import MySnippet from '/snippets/my-snippet.mdx';
100126

101-
### JSX snippets
127+
<MySnippet word="bananas" />
102128

103-
1. Export a JSX component from your snippet file. (See [React components](/customize/react-components) for more information):
129+
```
104130

105-
```js icon=square-js snippets/my-jsx-snippet.jsx
106-
export const MyJSXSnippet = () => {
107-
return (
108-
<div>
109-
<h1>Hello, world!</h1>
110-
</div>
111-
);
112-
};
113-
```
131+
### Import React components
132+
133+
1. Create a snippet with a JSX component. See [React components](/customize/react-components) for more information.
134+
135+
```js snippets/my-jsx-snippet.jsx
136+
export const MyJSXSnippet = () => {
137+
return (
138+
<div>
139+
<h1>Hello, world!</h1>
140+
</div>
141+
);
142+
};
143+
```
114144

115145
<Note>
116-
Important: When creating JSX snippets, use arrow function syntax (`=>`) rather
117-
than function declarations. The `function` keyword is not supported in this
118-
context.
146+
When creating JSX snippets, use arrow function syntax (`=>`) rather than function declarations. The `function` keyword is not supported in snippets.
119147
</Note>
120148

121-
2. Import the snippet from your destination file and use the component:
149+
2. Import the snippet.
122150

123-
```typescript destination-file.mdx
124-
---
125-
title: My title
126-
description: My Description
127-
---
151+
```mdx destination-file.mdx
152+
---
153+
title: "An example page"
154+
description: "This is an example page that imports a snippet with a React component."
155+
---
128156

129-
import { MyJSXSnippet } from '/snippets/my-jsx-snippet.jsx';
157+
import { MyJSXSnippet } from '/snippets/my-jsx-snippet.jsx';
130158

131-
<MyJSXSnippet />
132-
```
159+
<MyJSXSnippet />
160+
```

customize/react-components.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The counter renders as an interactive React component.
9292

9393
## Importing components
9494

95-
To import React components in your MDX files, the component files must be located in the `snippets` folder. You can then import them into any MDX page in your documentation. Learn more about [reusable snippets](/create/reusable-snippets).
95+
To import React components in your MDX files, the component files must be located in a snippet folder. By default, this is the `/snippets/` folder. You can [configure additional directories](/create/reusable-snippets#configure-snippet-folders) to contain snippets in your `docs.json`. Learn more about [reusable snippets](/create/reusable-snippets).
9696

9797
### Example
9898

organize/settings.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,16 @@ This section contains the full reference for the `docs.json` file.
643643
</Expandable>
644644
</ResponseField>
645645

646+
<ResponseField name="snippets" type="array of strings">
647+
Specify additional folders for reusable snippets using [glob patterns](https://code.visualstudio.com/docs/editor/glob-patterns). Any files in folders matching these patterns are snippets, in addition to the default `/snippets/` folder.
648+
649+
```json Example snippets configuration
650+
{
651+
"snippets": ["components/**", "shared/shared-file.mdx"]
652+
}
653+
```
654+
</ResponseField>
655+
646656
<ResponseField name="contextual" type="object">
647657
Contextual menu for AI-optimized content and integrations.
648658

0 commit comments

Comments
 (0)