Skip to main content
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. Snippets do not render as standalone pages. You must import them into other files.

Configure snippet folders

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. Add glob patterns to the snippets array in docs.json to specify which folders contain snippets.
docs.json
{
  "snippets": [
    "components/**",
    "shared/reusable/**",
    "shared/common-component.mdx"
  ]
}

Create snippets

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.

Import snippets into pages

To use snippets, import them into pages using either an absolute or relative path.
  • Absolute imports: Start with /snippets/ or your custom snippet location for imports from the root of your project.
  • Relative imports: Use ./ or ../ to import snippets relative to the current file’s location.
Relative imports enable IDE navigation. Press CMD and click a snippet name in your editor to jump directly to the snippet definition.

Import text

  1. Add content to your snippet file that you want to reuse.
snippets/my-snippet.mdx
Hello world! This is my content I want to reuse across pages.
  1. Import the snippet into your destination file using either an absolute or relative path.
---
title: "An example page"
description: "This is an example page that imports a snippet."
---

import MySnippet from '/snippets/my-snippet.mdx';

The snippet content displays beneath this sentence.

<MySnippet/>

Import variables

Reference variables from a snippet in a page.
  1. Export variables from a snippet file.
snippets/custom-variables.mdx
export const myName = "Ronan";

export const myObject = { fruit: "strawberries" };
  1. Import the snippet from your destination file and use the variable.
destination-file.mdx
---
title: "An example page"
description: "This is an example page that imports a snippet with variables."
---

import { myName, myObject } from '/snippets/custom-variables.mdx';

Hello, my name is {myName} and I like {myObject.fruit}.

Import snippets with variables

Use variables to pass data to a snippet when you import it.
  1. Add variables to your snippet and pass in properties when you import it. In this example, the variable is {word}.
snippets/my-snippet.mdx
My keyword of the day is {word}.
  1. Import the snippet into your destination file with the variable. The passed property replaces the variable in the snippet definition.
destination-file.mdx
---
title: "An example page"
description: "This is an example page that imports a snippet with a variable."
---

import MySnippet from '/snippets/my-snippet.mdx';

<MySnippet word="bananas" />

Import React components

  1. Create a snippet with a JSX component. See React components for more information.
snippets/my-jsx-snippet.jsx
export const MyJSXSnippet = () => {
  return (
    <div>
      <h1>Hello, world!</h1>
    </div>
  );
};
When creating JSX snippets, use arrow function syntax (=>) rather than function declarations. The function keyword is not supported in snippets.
  1. Import the snippet.
destination-file.mdx
---
title: "An example page"
description: "This is an example page that imports a snippet with a React component."
---

import { MyJSXSnippet } from '/snippets/my-jsx-snippet.jsx';

<MyJSXSnippet />