# code: the highlighter, and the code blocks

`Highlight` is a kit component, written in Mote over the `syntax` battery
([text.md](../text.md#syntax)): what Shiki is to shadcn's and AI Elements'
code blocks. `kit/code.mote` holds it, and those blocks over it, under
their names. Gallery: `examples/kit/code.mote`.
Test: `tests/kit_code.real.mjs`.

    state src = "const n: number = 1;\n"

    CodeBlock code=src language="ts" showLineNumbers title="app.ts"

    CodeBlock
      CodeBlockHeader
        CodeBlockTitle
          CodeBlockFilename "app.ts"
        CodeBlockActions
          CodeBlockLanguageSelector value=lang
          CodeBlockCopyButton value=src
      CodeBlockContent code=src language=lang lineNumbers=true
      CodeBlockFooter
        text "1 line"

## Highlight

    Highlight text=src lang="ts" lines start=10 wrap marks=[{line: 12, kind: "added"}]

A `<pre>`. Monospace, 13px/1.6, tab-size 2; it scrolls sideways unless
`wrap`. Styles written on it win over its own (`font-size=12 padding=16
max-height=300`). It draws again when any prop changes.

| prop | |
|---|---|
| `text` | the code |
| `lang` | a language or an alias (below); unknown or empty: the text, uncoloured, at the same size |
| `lines` | a gutter of line numbers. They are not in the text: selecting or copying the code leaves them out |
| `start` | the first line's number (1) |
| `wrap` | long lines wrap under themselves, the number staying at the top |
| `marks` | a list of `{line, kind}`: `kind` is `"added"`, `"removed"` or `"highlighted"`; the line (as numbered, so counting from `start`) is tinted to the block's edges, and the first two get `+` / `-` in the gutter (with or without `lines`) |

It does not take a value that may be `none`: a kit component that passes
one on types the prop `json`, as `CodeBlockContent` does.

The lines come from `syntax.lines` in a worker, through a resource: the code
is shown plain at once and coloured when the answer comes. Each line is an
element of its own (class `hk-l`), each token a `text` with a class for its
kind (`hk-keyword`, `hk-string`, …, `hk-plain`).

## The components

| | |
|---|---|
| `CodeBlock` | The frame: border, radius, `card` background. `variant="default" \| "destructive"` (a `danger` border), `size="default" \| "sm"` (13px / 12px). |
| `CodeBlock code=src language="ts" title="app.ts" showLineNumbers wrap startLine=10 marks=[…]` | The whole block in one line (AI Elements' form): a header with the title (or, without one, the language's name), a copy button, the content. With neither title nor language there is no header and the copy button sits over the code's corner. What is written under it comes after the content. |
| `CodeBlockHeader` | The bar at the top. |
| `CodeBlockTitle` | Its left side: an icon, a filename, a label. |
| `CodeBlockFilename "app.ts"` | A name, in monospace, cut short with an ellipsis. |
| `CodeBlockActions` | Its right side: buttons, the selector. |
| `CodeBlockCopyButton value=src label="Copy"` | Copies `value` (the built-in `copy()`), then shows a check and says "Copied" for two seconds. `label` is its aria-label and title. Anything written under it follows the icon (`text "Copy"`). |
| `CodeBlockContent code=src language="ts" lineNumbers=true wrap=true startLine=10 marks=[…] size="sm"` | The code: a `Highlight`, padded, focusable so the keyboard can scroll it. Styles fall through to the `<pre>`: `max-height=320`. `size` as on CodeBlock. |
| `CodeBlockLine "text" variant="added" number=3` | A line of a block built by hand. `variant="default" \| "added" \| "removed" \| "highlighted"`. Its text is written under it, or given as `code=… language=…` to be coloured. `number` shows a line number. |
| `CodeBlockFooter` | The bar at the bottom. |
| `CodeBlockLanguageSelector value=lang options=["js", "ts"]` | A select; `value` is written back. Without `options` it lists every language the highlighter knows. |

There is no context between components, so in a hand-built block `size`
goes to `CodeBlockContent` as well as to `CodeBlock`, and the selector and
the content are given the same state (`value=lang`, `language=lang`).

## What differs from the originals

- No `useHighlightedCode`, no `highlightCode()`: a hook cannot exist here,
  and the element colours the code itself, synchronously.
- No themes prop (Shiki's `themes={{light, dark}}`): one palette, following
  the page's scheme. Restyle the classes below instead.
- `CodeBlockCopyButton` has no `onCopy`, `onError` or `timeout`: it is two
  seconds, and a refused clipboard is silent.
- `CodeBlockLanguageSelector` is the browser's `<select>`, so its
  Trigger / Value / Content / Item parts do not exist.
- AI Elements puts a one-line CodeBlock's children among its header's
  actions; here they come after the content (a component has one `slot`).
  For other actions, build the block from the parts.
- `CodeBlockContainer` is `CodeBlock` without `code`.
- `showLineNumbers` on CodeBlock, `lineNumbers` on CodeBlockContent, as in
  the originals' two shapes.
- `asChild`, `className`: not props. Styles, ids and events written on a
  component go to its first element.
- Snippet is another file's.

## Languages

37, by canonical name, then the aliases `lang` also takes (case does not
matter):

| name | aliases |
|---|---|
| `js` | javascript mjs cjs node |
| `ts` | typescript mts cts |
| `jsx` `tsx` | |
| `json` | jsonc json5 webmanifest (comments are allowed) |
| `html` | htm xhtml vue svelte (`<style>` and `<script>` are CSS and JavaScript) |
| `xml` | svg xsl xslt rss atom plist |
| `css` | |
| `scss` | sass less |
| `python` | py py3 python3 |
| `rust` | rs |
| `go` | golang |
| `java` | |
| `c` | h |
| `cpp` | c++ cc cxx hpp hh hxx |
| `csharp` | cs c# dotnet |
| `php` | (HTML until `<?php`; PHP throughout if there is no `<?`) |
| `ruby` | rb |
| `swift` | |
| `kotlin` | kt kts |
| `sql` | mysql postgres postgresql pgsql sqlite plsql |
| `bash` | sh zsh shell shellscript ksh |
| `powershell` | ps1 ps pwsh |
| `yaml` | yml |
| `toml` | |
| `ini` | cfg conf properties env dotenv editorconfig |
| `markdown` | md mdx mkd (a fence is coloured as its language; front matter as YAML) |
| `diff` | patch |
| `dockerfile` | docker |
| `makefile` | make mk |
| `lua` `r` `dart` `zig` `mote` | |
| `scala` | sc |
| `graphql` | gql |

## Token classes

Every token is a `<span class="hk-…">`; no span holds another, and none
crosses a line end. The stylesheet (`<style id="hk-css">`, put in the page
once) uses `light-dark()`, so the colours follow the page's scheme. To
restyle, write rules for these classes after it (`pre.hk .hk-k { … }`).

| class | | default |
|---|---|---|
| `hk-k` | keyword | red |
| `hk-s` | string | navy / pale blue |
| `hk-n` | number (and a colour in Mote) | blue |
| `hk-b` | constant, builtin (`true`, `None`, `MAX_N`, a CSS value word, a symbol) | blue |
| `hk-a` | property, attribute, key (JSON, YAML, TOML keys; HTML and JSX attributes; CSS properties and `.class` `#id`; a shell option) | blue |
| `hk-c` | comment | grey |
| `hk-f` | function, at its call and definition; a macro; a shell command | purple |
| `hk-m` | meta: decorator, annotation, attribute `#[…]`, preprocessor line, doctype, lifetime, CSS pseudo-class, fence | purple |
| `hk-t` | type, class; a TOML table, an INI section | orange |
| `hk-v` | variable (`$x`, `@x`, `self`, `--custom`) | orange |
| `hk-g` | tag (HTML, JSX, a CSS element selector, a Mote element) | green |
| `hk-r` | regex literal; an escape or the brackets of an interpolation in a string (`${`, `}`, `#{`, `\(`) | teal |
| `hk-o` `hk-p` | operator, punctuation (only some: brackets and commas are left bare) | the text's colour, lighter |
| `hk-h` | Markdown heading | blue, bold |
| `hk-x` `hk-e` | Markdown strong, emphasis | bold, italic |
| `hk-i` `hk-d` | a diff's inserted and deleted lines | green / red, tinted |

The element's own: `hk` (the `<pre>`), `hk-wrap`, `hk-num` (numbered),
`hk-gut` (it has a gutter), `hk-l` (a line, when numbered or marked),
`hk-add` `hk-rem` `hk-hi` (a marked line), and the variable `--hk-g` (the
gutter's width).

## For other widgets

    HLH(text, lang)   the HTML: escaped, tokens in spans, "\n" kept, no span over a line end
    HLS()             puts the stylesheet in the page (HLH's callers call it; HL does)
    HLL(lang)         the canonical name, or ""
    HLN               the canonical names

They are in section `HL` of the runtime: a section that uses them lists `HL`
among what it needs.

## The gallery's samples

`examples/kit/code.mote` holds a sample per language as string literals. To
add a language: a function in the `L` table of `highlight.js` (states of
`[pattern, class, next]` rules; the comment at the top of the file is the
whole format), an alias line if it has any, a sample in the gallery's
`samples`, and a case in the test's `cases`.
