# Code Block

Blocks of code or any preformatted text can be displayed by wrapping with triple backticks characters before and after.

Source
Demo
```
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.

Source
Demo
```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";
```
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";
```
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:

Scenario Sample Description
Single line #2 Highlight line #2
Line range #2-5 Highlight lines #2 to #5
Single line and a range #2,4-8 Highlight line #2 and lines #4 to #8
Multiple line ranges #1-2,4-8 Highlight lines #1 to #2 and lines #4 to #8.
Remove line numbering !#4-8 Remove line numbering and highlight lines #4 to #8

# 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.

Source
Demo
```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";
```
Your title here
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:

Enable line numbering for js and json code blocks site wide
{
  "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.

Without snippets config
With 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 !# specifier to the code block instance.

Source
Demo
```js !#
const msg = "Hello, world";
```
const msg = "Hello, world";