Code Blocks

Code blocks render syntax-highlighted snippets with a copy button and an optional title. In Markdown they're written as standard fenced code blocks — Scalar reads extra options from the info string after the language name. In MDX, the same fenced syntax works directly.

Properties

Properties are passed through the info string on the opening fence. The first space-separated token (before any name="value" pair) is the title.

PropTypeDefaultDescription
langstringrequiredThe language identifier directly after the opening fence (e.g. js, python).
titlestring—A filename or label shown in the code block header. The first bare token after lang is treated as the title.
lineNumbersbooleanfalseShow line numbers. Also accepted as line-numbers or lines.
lineNumberStartnumber0Number of the first line. Line ranges count from it too, even when line numbers are hidden.
highlightstring—Comma-separated line numbers or ranges to highlight (e.g. 1,3-5).
focusstring—Lines to focus while dimming the rest.
addedstring—Lines marked as added (diff-style green).
removedstring—Lines marked as removed (diff-style red).

Lines count from 0 unless you set lineNumberStart, so highlight="1" marks the second line of a block. Set lineNumberStart="1" to count from one.

Removed lines give up their number. removed counts every line in the block, while highlight, focus and added skip any removed lines above them, so they match the line numbers on screen.

Examples

Basic Code Block

function greet(name) {
  return `Hello, ${name}!`
}
```javascript
function greet(name) {
  return `Hello, ${name}!`
}
```

With a Title

function greet(name) {
  return `Hello, ${name}!`
}
```javascript greet.js
function greet(name) {
  return `Hello, ${name}!`
}
```

Line Numbers and Highlighting

function greet(name: string): string {
  const greeting = `Hello, ${name}!`
  return greeting
}
```typescript example.ts lineNumbers highlight="1-2"
function greet(name: string): string {
  const greeting = `Hello, ${name}!`
  return greeting
}
```

Diff Style (Added and Removed Lines)

{
  "version": "1.0.0",
  "version": "2.0.0",
  "name": "scalar"
}
```diff config.json added="1" removed="1"
{
  "version": "1.0.0",
  "version": "2.0.0",
  "name": "scalar"
}
```

What did you think of this content?