generate() function transforms an array of tokens (from tokenize()) into an array of line objects. Each line contains styled token elements with appropriate CSS classes.
Signature
Parameters
An array of tokens produced by the
tokenize() function. Each token is a tuple of [type: number, value: string].Returns
An array of line objects. Each line has the following structure:Each child token within a line has this structure:
Example
Token to CSS Class Mapping
Thegenerate() function applies CSS classes based on token types:
| Token Type | CSS Class | CSS Variable |
|---|---|---|
| identifier | css__token--identifier | var(--css-identifier) |
| keyword | css__token--keyword | var(--css-keyword) |
| string | css__token--string | var(--css-string) |
| class | css__token--class | var(--css-class) |
| property | css__token--property | var(--css-property) |
| entity | css__token--entity | var(--css-entity) |
| jsxliterals | css__token--jsxliterals | var(--css-jsxliterals) |
| sign | css__token--sign | var(--css-sign) |
| comment | css__token--comment | var(--css-comment) |
| break | N/A (line separator) | N/A |
| space | css__token--space | var(--css-space) |
Line Structure Details
Multi-Line Token Handling
Notes
The
generate() function automatically splits tokens that contain newline characters into multiple lines, ensuring correct line-by-line rendering.Break tokens (type
9) are used as line separators. They create a new line but don’t add visible content.Each line has the CSS class
css__line, which is useful for styling individual lines or adding line numbers with CSS counters.The function uses CSS custom properties (variables) for token colors, allowing easy theme customization:
This is a low-level API. Most users should use the
highlight() function instead, which calls generate() internally after tokenization.