# Highlight

```ts
import { highlightTemplate } from '@memomori/obol/highlight'

highlightTemplate('{{ if .ok }}yes{{ end }}')
// [
//   { className: 'meta', start: 0, end: 2 },
//   { className: 'keyword', start: 3, end: 5 },
//   { className: 'property', start: 6, end: 9 },
//   { className: 'meta', start: 10, end: 12 },
//   { className: 'meta', start: 15, end: 17 },
//   { className: 'keyword', start: 18, end: 21 },
//   { className: 'meta', start: 22, end: 24 },
// ]
```

`highlightTemplate(text, options)` returns ranges of `text`, sorted by
`start`, each with a class. It reads the text with obol's port of Go's lexer,
so it splits an action where Go does. Wrap each range in a `<span>`, or hand
the ranges to a range-based highlighter such as `@tanstack/highlight`.

## Classes

| Class      | Covers                                                                                   |
| ---------- | ---------------------------------------------------------------------------------------- |
| `meta`     | Delimiters, with their trim markers                                                      |
| `comment`  | A whole comment, delimiters and all                                                      |
| `keyword`  | `if`, `else`, `end`, `range`, `with`, `define`, `block`, `template`, `break`, `continue` |
| `function` | Function names, such as `len` or one from `funcs`                                        |
| `property` | Fields, such as `.name`                                                                  |
| `variable` | `$`, `$name` and `.`                                                                     |
| `string`   | Quoted and raw strings, and character constants                                          |
| `number`   | Numbers, complex ones too                                                                |
| `literal`  | `true`, `false` and `nil`                                                                |
| `operator` | `\|`, `:=` and `=`                                                                       |

Parentheses, commas and spaces get no range. Neither does the text between
actions, unless you pass `highlightText`.

## Text between actions

`highlightText` colors the text between actions, such as HTML. It gets the
template with every action blanked to spaces, line breaks kept, so its
positions match the source:

```text
<a href="{{ .url }}">{{ .name }}</a>
<a href="          ">           </a>
```

It returns ranges: any objects with `start` and `end`. `highlightTemplate`
cuts each one around the actions, so the HTML string `"{{ .url }}"` above
comes back as two ranges, `"` and `"`, and merges them with its own. This
example hands the text to `@tanstack/highlight`, which colors it as HTML:

```ts
import { highlightTemplate } from '@memomori/obol/highlight'
import {
  allLanguages,
  createHighlighter,
  defineLanguage,
} from '@tanstack/highlight'

const goTemplate = defineLanguage({
  name: 'go-template',
  aliases: ['gotmpl'],
  tokenize: (code, context) =>
    highlightTemplate(code, {
      highlightText: (masked) => context.tokenize(masked, 'html'),
    }),
})

const highlighter = createHighlighter({
  languages: [...allLanguages, goTemplate],
})
```

## Broken templates

It reads the text with the lexer and does not parse it, so a fragment such
as `{{ range .items }}` with no `{{ end }}` highlights too. It never throws.
Where the lexer rejects an action, as it does an unclosed string, the rest of
that action up to the next right delimiter gets no range, and highlighting
picks up after it.

Go reads `{{ /*` as an action, not a comment, and so does the highlighter.

## Delimiters

```ts
highlightTemplate('[[ .name ]]', { leftDelim: '[[', rightDelim: ']]' })
```
