#
Code Block
Blocks of code or any preformatted text can be displayed by wrapping with triple backticks characters before and after.
```
A basic code block
```
A basic code block
#
Syntax highlighting
Optional syntax highlighting of the code within the code block can be configured by adding a language identifier immediately after the opening triple backticks.
In the following sample, we configure JavaScript syntax highlighting for the code block by adding the js
language identifier.
```js
const msg = "hello, world";
```
const msg = "hello, world";
Retype uses PrismJS for syntax highlighting. All PrismJS language modules are dynamically loaded as required and all Prism supported languages are supported by Retype.
#
Title
Retype includes the functionality to set a title on your markdown code blocks.
``` Code Block title
const msg = "Set a code block title";
```
const msg = "Set a code block title";
The title can be used in conjunction with the code reference type.
```js Code Block title
const msg = "Set a code block title";
```
const msg = "Set a code block title";
The title
should be separated from the opening fence by one space, for example the pattern ``` Code Block title
is recommended.
If a code language is used, separate the title
from the lang
by one space. The pattern ```js Code Block title
will work as expected.
#
Line highlighting
Highlight a specific line or range of lines in a code block component using the line highlighting syntax.
After the opening ```
of a code block component, add a space and then start your line highlighting configuration with a #
character. For instance, to highlight the first line, use ``` #1
.
Here are a few other common scenarios with additional samples below:
#
Single line
Highlight a single line number.
```js #2
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
```
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
#
Line range
Highlight a range of lines by separating the start and end line number with a -
dash.
```js #5-7
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
```
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
#
Multiple ranges
Configure multiple line ranges by separating each block with a ,
comma.
```js #2-3,5-7
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
```
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
#
With no line numbers
Disable the default line numbering but still highlight a line or range of lines.
```js !#2-3,5-7
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
```
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
#
Using attribute syntax
Configuring line highlighting using the highlight
attribute syntax is also supported by Retype.
```js:highlight="2-3,5-7"
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
```
export function calculateOffset(): number {
const siteHeaderEl = document.getElementById("docs-site-header");
const toolbarEl = document.getElementById("docs-toolbar");
let offset = 20; // 20 is extra offset added before the focused element
offset += siteHeaderEl ? siteHeaderEl.offsetHeight : 0;
offset += toolbarEl ? toolbarEl.offsetHeight : 0;
return -offset;
}
#
Line numbers
Adding or removing line numbering for your code blocks can be configured by adding the #
specifier character to the first line after the reference language.
```js #
const msg = "hello, world";
```
const msg = "hello, world";
You can also add a title after the #
:
```js # Your title here
const msg = "hello, world";
```
const msg = "hello, world";
The #
should be separated from the opening ```
by one space, for example the pattern ``` #
is recommended.
If a title is added, the title must also be separated from the #
by one space. For instance, the pattern ``` # Your title here
would work as expected and the pattern ``` #Your title here
would not.
Line numbering can also be configured at the project level using the snippets
config on your projects retype.yml file. For instance, instructing Retype to add line numbering to all js
and json
code blocks across the website would require the following config:
{
"snippets": {
"lineNumbers": [ "js", "json" ]
}
}
With the above snippets
config, then you would not have to add the #
specifier to each code block. All js
and json
code blocks would automatically get line numbers.
snippets
config
snippets
config
const msg = "Hello, world";
const msg = "Hello, world";
#
Disable line numbers
If you configure a site wide snippets
for a language and would like to explicitly remove the line numbering for a code block instance of that language, please add the !#
```js !#
const msg = "Hello, world";
```
const msg = "Hello, world";