Code
Code is highlighted once, at build time, with Shiki: the browser never loads a highlighter or a grammar for a plain code block. For editors on a slide that run code, see the live code guide.
Fences with steps
Section titled “Fences with steps”```ts {1|3-5|all} [example.ts]const answer = 42;
function ask(question: string) { return `${question}? ${answer}`;}```The meta after the language does three things:
| Meta | Meaning |
|---|---|
{1|3-5|all} |
highlight steps: line 1, then lines 3–5, then everything, one click each |
{hide|1|2} |
the block itself stays hidden until its first step |
[example.ts] |
a title bar |
{lines:true} |
line numbers for this block (deck-wide: lineNumbers: true) |
{2}{lines:true,startLine:10,maxHeight:'300px'} |
options after the steps: numbering from 10, scrolling past 300 px |
A step is a list of lines and ranges (1,3-5), all (or *) or none. N steps take N − 1
clicks: the first shows when the slide arrives.
reveal.js’s bracket form reads as steps too: ```js [1-2|3] is {1-2|3}, and
```js [712: 1-2|3] numbers the block from 712. As in reveal.js, the steps in the bracket
form count from the block’s first line. A bracket with a name in it, such as [example.ts], is
still a title bar.
Marks in the code
Section titled “Marks in the code”A comment at the end of a line marks it (Shiki’s notation, in any comment style); the comment itself is removed:
```tsconst tide = 6.1; // [!code --]const tide = 6.4; // [!code ++]const safe = tide < 7; // [!code highlight]report(tide); // [!code focus]```| Comment | Effect |
|---|---|
[!code ++], [!code --] |
an added or removed line |
[!code highlight], [!code highlight:3] |
highlight this line (or the next 3) |
[!code focus] |
blur every other line until the pointer is over the block |
[!code error], [!code warning] |
an error or warning line |
[!code word:tide] |
mark every tide in the block |
Marks work with click steps. Add your own Shiki transformers in vite.config.ts:
deck({ shiki: { transformers: [myTransformer()] } }).
Tabs: code groups
Section titled “Tabs: code groups”Fences between ::code-group and :: become tabs, each named by its title, with a file-type badge:
::code-group
```sh [npm]npm i @kadal/deck```
```sh [bun]bun add @kadal/deck```
::← and → move between the tabs once one has focus. ::code-group {clicks:true}
opens the next tab on each click; {print:'all'} prints every tab rather than the open one.
<CodeGroup> is the same thing as a component (clicks, at, active, print).
Snippets from files
Section titled “Snippets from files”<<< pulls code from a file, so the slide shows what the repository really has:
<<< @/snippets/tide.ts<<< @/snippets/tide.ts#ask {2-3} [tide.ts]<<< @/snippets/query.sql sql {lines:true}@ is the deck’s folder; ./x.ts is relative to the slide’s file. #ask takes one region of the
file, marked // #region ask … // #endregion (any comment style works). A language and any fence
meta may follow. The file is watched in kadal-deck dev, and a build carries the code with it. In
the hosted builder, which compiles the deck in the browser, the file has to be one of the deck’s
files beside deck.mdx.
Types on hover: TwoSlash
Section titled “Types on hover: TwoSlash”```ts twoslashconst tide = { height: 6.1, at: new Date() };// ^?tide.heigth;```The TypeScript compiler runs at build time: hover a name (or Tab to it) for its type,
errors are underlined with the compiler’s message, and // ^? pins the type of the token above it.
It needs typescript installed in the deck. deck({ twoslash: false }) turns it off. In the hosted
builder the block renders as plain highlighted code.
Magic move
Section titled “Magic move”A magic-move block holds one fence per step; each click morphs the code from one step to the
next, tokens sliding to their new places:
````md magic-move {lines: true}```tsconst total = items.length;```
```ts {*|1|2}const done = items.filter((i) => i.done);const total = done.length;```
```tsconst total = items.filter((i) => i.done).length;```````- The steps are tokenised at build time.
- An inner fence’s highlight steps are clicks of their own before the next morph:
{*|1|2}above shows the whole second step, then lights line 1, then line 2, then morphs on. Lines outside the step dim on screen, in print (a page per step) and in PPTX. - After the opening
magic-move:{lines: true}numbers the lines (or put it on one step’s fence),{duration: 1200}sets this block’s morph in milliseconds,{at: 2}moves its first step.magicMoveDurationin the headmatter sets the default for the deck (800 ms). - Under
prefers-reduced-motionthe steps change at once.
Live code: Monaco
Section titled “Live code: Monaco”A {monaco} fence is an editor on the slide (the editor VS Code uses). It loads only when a slide
with one is shown, and print, export and the phone remote show the highlighted code instead.
Esc hands the keys back to the deck.
```ts {monaco}{height:'auto'}const greeting = "hello";```Options: height (CSS, or 'auto' to grow with the code), lines, readonly, and editorOptions
(Monaco’s own).
Monaco ships with the engine and stays out of the main bundle: a deck without an editor never downloads it. The live code guide walks through all four kinds of block.
A diff
Section titled “A diff”{monaco-diff} holds the original, a line of ~~~, then the changed code. Print shows a line diff.
```ts {monaco-diff}const tide = 6.1;~~~const tide = 6.4;```{sideBySide:false} shows it inline.
Running code
Section titled “Running code”{monaco-run} adds a Run button (Ctrl/⌘ Enter) and shows the output
under the editor:
```ts {monaco-run}{showOutputAt:'+1'}console.log([1, 2, 3].map((n) => n * 2));```- JavaScript and TypeScript run in a Web Worker: the console is captured, errors keep their line, and a timeout stops an endless loop without freezing the slide.
- Python runs in Pyodide 0.28.3, in a worker shared by every Python block of the deck; the value of the last expression shows like a REPL’s, and imported packages load by themselves.
- Options:
autorun(default true: runs on arrival and again on each edit),showOutputAt(a click, e.g.'+1'),outputHeight,outputPosition('bottom'or'right'),timeout(ms, default 5000; Python gets 20000 once loaded),runnerOptions(handed to the runner).
Packages and types
Section titled “Packages and types”A bare import in a JavaScript or TypeScript {monaco-run} block loads from
esm.sh:
```ts {monaco-run}import { chunk } from "lodash-es";
console.log(chunk(["a", "b", "c", "d", "e"], 2));```The editors also know the imported packages’ types, so hovers, completions and errors are the package’s own. Four headmatter keys steer both:
| Key | Default | Meaning |
|---|---|---|
monacoPackages |
[] |
packages the editors know up front, and the versions {monaco-run} imports: ["zod", "lodash-es@4"] or { zod: "^3.23" }; unnamed ones are latest |
monacoTypes |
auto |
where types come from: auto (the deck’s node_modules, bundled by kadal-deck build, else the CDN), local, cdn (jsDelivr) or none |
monacoTypesIgnore |
[] |
packages never to fetch types for |
monacoRunImports |
"https://esm.sh" |
where a bare import is fetched: a CDN base, or a template with {name}, {version} and {path}; false makes a bare import an error |
Types fetched from the CDN are declaration files only, the ones the imports reach, and are cached in the browser. Offline with nothing cached, the editors work without them.
Other languages
Section titled “Other languages”Other languages take a runner of your own: a function from code to output. Put them in
setup/code-runners.ts beside deck.mdx:
import { defineCodeRunners } from "@kadal/deck";
export default defineCodeRunners({ async sql(code, ctx) { const rows = await fetch("/api/sql", { method: "POST", body: code, signal: ctx.signal }); return { text: await rows.text() }; },});A runner returns text, HTML (sanitised before it is shown), an element, or { error };
ctx.write() streams output as it comes, and ctx.signal aborts when the block runs again or the
slide goes away. registerCodeRunner("sql", fn) from @kadal/deck does the same at run time, and
replaces a built-in runner when you name its language.
Saving edits back
Section titled “Saving edits back”{monaco-write} saves what you type back to the source while kadal-deck dev runs: to the file a
<<< snippet came from, or else to the fence in deck.mdx. A save is refused if the file changed
underneath it. In a build and on deck.kadal.cc the editor is read-only.
<<< @/snippets/tide.ts#ask {monaco-write}Highlighting themes
Section titled “Highlighting themes”The Shiki themes come from the deck’s theme package when it names some (kadal.theme.shiki), else
GitHub light and dark. Code follows the theme menu: pick another theme with T and every
block, mark, TwoSlash popup, magic move and Monaco editor takes that theme’s Shiki pair, in light and
dark, on screen and in exports. A slide that forces a colour scheme gets that scheme’s code colours.
To use one pair under every theme, name it in the Vite plugin’s options; the theme’s CSS can still restyle the colours:
deck({ entry: "deck.mdx", shiki: { light: "min-light", dark: "min-dark" } });