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";
Neko uses PrismJS for syntax highlighting. All PrismJS language modules are dynamically loaded as required and all Prism supported languages are supported by Neko.
Title
Neko includes the functionality to set a title on your markdown code blocks.
``` 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";
```
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.
Chrome Styles
You can customize the top window-chrome of your code blocks to mimic standard operating system window styles.
To use a chrome modifier, add the chrome="..." attribute to the first line after the reference language. Neko supports mac and windows.
macOS Chrome
The chrome="mac" modifier adds the standard three-dot window control buttons to the left of your title.
```js chrome="mac" main.js
const msg = "Set a macOS style chrome";
```
Windows Chrome
The chrome="windows" modifier adds standard Windows control buttons (minimize, maximize, close) to the right.
```js chrome="windows" main.js
const msg = "Set a Windows style chrome";
```
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
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
```
Line range
Highlight a range of lines by separating the start and end line number with a - dash.
```js #5-7
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
```
Multiple ranges
Configure multiple line ranges by separating each block with a , comma.
```js #2-3,5-7
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
```
With no line numbers
Disable the default line numbering but still highlight a line or range of lines.
```js !#2-3,5-7
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
```
Using attribute syntax
Configuring line highlighting using the highlight attribute syntax is also supported by Neko.
```js:highlight="2-3,5-7"
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
```
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";
```
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 neko.yml file. For instance, instructing Neko 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 configconst msg = "Hello, world";
snippets configconst 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.
```js !#
const msg = "Hello, world";
```
const msg = "Hello, world";
Code group
Code blocks can be grouped using a [[Tab]] component that lets you combine multiple code blocks into a tabbed interface. Each code block becomes a separate tab that users can switch between.
To create a code group in Neko, simply wrap each code block with +++ followed by the tab label. The tab label will be displayed as the tab button text.
Here's an example showing the same "Hello, world" program in different languages:
console.log("Hello, world!");
Copy Button
All code blocks automatically include a copy button in the top-right corner. When a title is present, the button is integrated into the title bar.