Skip to content
Last updated

Code snippets are small sections of code you can include in your Markdown and OpenAPI documents. In Markdown code snippets are formatted by wrapping the text in two sets of three backticks, placed at the start and end of the text block.

For example, the following is a code snippet with yaml:

codeSnippet with YAML
```yaml
codeSnippet:
  copy:
    hide: true
```

Markdown code snippets include a report and copy icon in the top right corner.

In OpenAPI reference documentation, code snippets are generated based on the information in the description. The following is an example of a request and response code snippet in OpenAPI reference documentation:

Screenshot of a request and response in OpenAPI reference documentation

OpenAPI reference documentation code snippets include report, copy, and expand and collapse icons in the top right corner.

You can configure the codeSnippet element to hide the copy, expand, and collapse buttons. You can also configure the report element's tooltip and dialog label text.

The codeSnippet option also supports page-level configuration using front matter.

Options

OptionTypeDescription
elementFormatstringA value that specifies the style for the control icons. Possible values: icon, text. Default: icon
copyCopyAn object with the list of specific settings for a code snippet's copy button.
reportReportAn object with the list of specific settings for a code snippet's report button.
expandExpandAn object with the list of specific settings for a code snippet's expand button.
collapseCollapseAn object with the list of specific settings for a code snippet's collapse button.

Copy object

Users can use copy button to put code snippet raw content into the clipboard.

OptionTypeDescription
hidebooleanSpecifies if the copy button should be hidden. Default value: false.

Report object

Users can use the report button to send problem feedback about the code snippet's content. When users click the report button or text, they are provided a comment feedback form.

OptionTypeDescription
hidebooleanSpecifies if the report button should be hidden. Default value: true.
labelstringLabel inside the report dialog form. Default value: What is wrong with the code?.
tooltipTextstringText of the tooltip of the report button. Default value: Report a problem.

Expand object

Use the expand button to show all the nested properties inside a JSON object that is included as a sample request or response in an OpenAPI definition.

OptionTypeDescription
hidebooleanSpecifies if the expand button should be hidden. Default value: false.

Collapse object

Users can use collapse button to hide all the nested properties inside a JSON object that is included as a sample request or response in an OpenAPI definition.

OptionTypeDescription
hidebooleanSpecifies if the collapse button should be hidden. Default value: false.

Configuration scope

Configure code snippets globally in your redocly.yaml file or for individual pages using front matter.

Global configuration

Configuration added to the redocly.yaml file applies to all Markdown and API reference pages:

codeSnippet:
  elementFormat: text
  report:
    label: Please tell us what is wrong with this code sample.
    tooltipText: Send feedback about this code sample

Page-level configuration

Configure code snippets for individual pages in the front matter. Front matter configurations take precedence over global settings:

---
codeSnippet:
  report:
    label: What is wrong with this code sample?
---

Examples

Hide or customize icons

Hide the copy button:

codeSnippet:
  copy:
    hide: true

Display all icons as text instead of icons:

codeSnippet:
  elementFormat: text

Hide all icons:

codeSnippet:
  copy:
    hide: true
  report:
    hide: true
  expand:
    hide: true
  collapse:
    hide: true

Configure report feedback

Enable the report button with custom label and tooltip:

codeSnippet:
  report:
    label: Please tell us what is wrong with this code sample.
    tooltipText: Send feedback about this code sample

Hide expand and collapse buttons

Hide the expand and collapse buttons on JSON objects in OpenAPI reference documentation:

codeSnippet:
  expand:
    hide: true
  collapse:
    hide: true

Add file names to code snippets

Add a file name to display in the code snippet header using the title attribute:

```js {% title="scripts.js" %}
function test() {
  console.log('Hello World!');
}
```

Result:

scripts.js
function test() {
  console.log('Hello World!');
}

Highlight lines and text

Highlight a specific line using [!code highlight]:

```js
function test() {
  console.log('Hello World!'); // [!code highlight]
}
```

Result:

function test() {
  console.log('Hello World!'); 
}

Highlight multiple consecutive lines:

```js 
// [!code highlight:3]
function test() { 
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);
}
```

Result:


function test() { 
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);
}

Highlight non-consecutive lines using the highlight attribute:

```js {% highlight="{1,3-4}" %}
function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);
}
```

Result:

function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);
}

Highlight specific words or symbols:

```js
// [!code word:Hello]
function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);  // prints "Hello World"
}
```

Result:


function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);  // prints "Hello World"
}

Focus on specific lines by dimming others with a code comment:

```js
function test() {
  const hello = 'Hello'; // [!code focus]
  const world = 'World';
  console.log(hello + " " + world);
}
```

Or with a Markdoc tag and a pattern.

```js {% highlight="/Hello/" %}
function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world);  // prints "Hello World"
}
```

Result:

function test() {
  const hello = 'Hello'; 
  const world = 'World';
  console.log(hello + " " + world);
}

Mark lines with error and warning levels:

```js
function test() {
  console.log('No errors or warnings');
  console.error('Error'); // [!code error]
  console.warn('Warning'); // [!code warning]
}
```

Result:

function test() {
  console.log('No errors or warnings');
  console.error('Error'); 
  console.warn('Warning'); 
}

Show added and removed lines:

```js
function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world); // [!code --]
  console.log(`${hello} ${world}`); // [!code ++]
}
```

Result:

function test() {
  const hello = 'Hello';
  const world = 'World';
  console.log(hello + " " + world); 
  console.log(`${hello} ${world}`); 
}

Tree view

Display file and directory structures using the treeview language:

```treeview
.
├── guides/                 # Guides
│   ├── guide-1.md
│   └── guide-2.md
├── images/                 # Various shared images
│   ├── favicon.png
│   └── header-image.png
├── tutorials/              # Tutorials
│   ├── tutorial-1.md
│   ├── tutorial-2.md
│   ├── index.md
│   └── sidebars.yaml       # Sidebar specific to the 'tutorials' section
├── static/                 # Static assets copied directly to build output
│   └── robots.txt
├── index.page.tsx          # Custom React component for the landing page
├── package.json            # Node.js project manifest
└── redocly.yaml            # Main Redocly configuration file
```

Result:

.
├──guides/                 # Guides
│   ├──guide-1.md
│   └──guide-2.md
├──images/                 # Various shared images
│   ├──favicon.png
│   └──header-image.png
├──tutorials/              # Tutorials
│   ├──tutorial-1.md
│   ├──tutorial-2.md
│   ├──index.md
│   └──sidebars.yaml       # Sidebar specific to the 'tutorials' section
├──static/                 # Static assets copied directly to build output
│   └──robots.txt
├──index.page.tsx          # Custom React component for the landing page
├──package.json            # Node.js project manifest
└──redocly.yaml            # Main Redocly configuration file

Customizing labels and tooltips

Translation keys provide customization options for code snippet control texts. For complete details, refer to the Translation Keys Reference.

Resources

  • Code-snippet Markdoc tag - Display code snippets loaded from local files with syntax highlighting and interactive features
  • Feedback configuration - Configure the feedback mechanism that appears on all pages and code snippets for user input collection
  • Front matter configuration - Configure code snippet behavior and appearance on individual pages using front matter settings
  • Configuration options - Explore other project configuration options for comprehensive documentation and platform customization