The <CodeBlock> component displays code examples with syntax highlighting. Code blocks support line highlighting, focusing, titles, and deep linking to make your code examples more readable and interactive.
Use three backticks with an optional language identifier.
Fern supports Shiki syntax highlighting for the following languages:
javascript (aliases: js, node)pythonjavatypescript (aliases: ts)csharpcppcphpgorustrubyswiftkotlinsqlbash (aliases: shell, sh)curlmarkdown (aliases: md)httpIf you specify a language that’s not on this list, Fern will still display the code block but without syntax highlighting.
Add a title to your code snippet by adding a title after the language identifier. Alternatively, use a title prop (title="Snippet with title") or filename prop (filename="Snippet with title") to achieve the same result.
Highlight specific lines in your code snippet by placing a numeric range inside {}
after the language identifier. The range is inclusive and can be a single number, a comma-separated list of numbers, or ranges.
Focus on specific lines by adding a comment [!code focus] or by adding a
focus attribute after the language identifier.
Control which line appears first in your code block by adding a startLine attribute after the language identifier. This is useful for longer code snippets where you want to highlight the main logic while still providing the complete context.
Control the max height of the code block by adding
a maxLines attribute after the language identifier. The
maxLines attribute should be a number representing the maximum
number of lines to display. By default, the code block will display up to 20 lines. (To disable the default 20 lines limit, set maxLines to 0.)
When you use maxLines, an expand button automatically appears on hover in the top-right corner, allowing users to view the full code content in an expanded overlay that displays over the page.
To hide the expand button or add custom styling, target the .fern-expand-button selector:
By default, long lines that exceed the width of the code block become scrollable:
To disable scrolling and wrap overflow onto the next line, use the wordWrap prop:
By default, code blocks display line numbers (and $ / > prefixes in bash/shell code blocks) in the gutter. To hide line numbers and prefixes, set showLineNumbers to false:
Make specific text within code blocks clickable by defining a links map. This is useful for linking to documentation, API references, or related resources directly from your code examples.
The links property accepts a map where keys are matching patterns (exact strings or regex) and values are the URLs to link to.
The links property uses JSON format. Each key in the map is matched exactly against text in the code block, and matching text becomes a clickable link to the corresponding URL.
You can use regex patterns for more flexible matching. This is useful when you want to link multiple variations or patterns of text.
In the example below, the pattern /get\\w+/ matches both getPlant and getGarden, while /Plant(Store|Client)/ matches both PlantStore and PlantClient.
When using regex patterns, remember to escape special characters with double backslashes (e.g., \\w+, \\d+) in the JSON string.
You can embed code from local or external files using the <Code> component with the src prop. For local files, reference files relative to your docs directory. For external files, use full URLs to pull code samples directly from remote sources like GitHub.
The <Code> component supports the same props as <CodeBlock>, including title, language, and maxLines.
Display multiple code blocks in a tabbed interface.
Code blocks with the same language automatically synchronize across your documentation site. When a user selects a language, all code blocks with that language switch to match. Language preferences persist across browser sessions.
Code blocks automatically synchronize with tabs in that same language.
You can link directly to content in a specific language by adding ?language=<some-language> to the end of a URL. This sets which language tab wil be displayed by default when users visit the page.
For example, the following link opens with Java tabs displayed: https://buildwithfern.com/learn/docs/writing-content/components/tabs?language=java
This works with both CodeBlocks and Tab components that have a language property.
Use the for prop to create custom synchronization groups independent of language. This is useful for grouping code blocks by other criteria like package managers or frameworks.
These can be added after the language identifier in markdown code blocks or as props on the <CodeBlock> component.
The programming language for syntax highlighting. Supported languages: javascript (aliases: js, node), python, java, typescript (alias: ts), csharp, cpp, c, php, go, rust, ruby, swift, kotlin, sql, bash (aliases: shell, sh), curl, markdown (alias: md), http.
Title displayed above the code block. Can be specified inline after the language identifier or using title="..." or filename="..." props.
Lines to highlight, specified as {2-4, 6} syntax. Supports single numbers, ranges, and comma-separated lists.
Lines to focus on, specified as focus={2-4}. Works the same way as highlight but dims non-focused lines.
The line number to start displaying from. Useful for showing specific portions of longer code files.
Maximum number of lines to display before adding scrolling. Set to 0 to disable the limit.
Whether to wrap long lines instead of making them scrollable.
Whether to display line numbers in the gutter, including line numbers and command prompt symbols ($ and >) in bash/shell code blocks. Set to false to hide line numbers for a cleaner look in code examples.
Custom synchronization group identifier. Overrides default language-based synchronization.
Map of text patterns to URLs for creating clickable links within code. Keys can be exact strings or regex patterns (e.g., {"/get\\w+/": "/api-docs"}).
<Code> propertiesThe <Code> component is used for embedding code from files. It accepts all code block attributes listed above, plus:
Path to a local file (relative to docs directory) or full URL to an external file (e.g., GitHub raw URL).
Lines to extract from the source file. Supports single lines ([5]), ranges ([2-4]), and combinations ([1-3,5,7-10]). Lines are 1-indexed.