| 19 | } |
| 20 | |
| 21 | export function CodeClient({tokens}: CodeClientProps) { |
| 22 | let i = 0; |
| 23 | let children: ReactNode[] = []; |
| 24 | while (i < tokens.length) { |
| 25 | let value = tokens[i++]; |
| 26 | if (typeof value === 'number') { |
| 27 | // A number represents a token type. The following value is its children. |
| 28 | let type = value; |
| 29 | value = tokens[i++]; |
| 30 | let child = Array.isArray(value) ? <CodeClient tokens={value} /> : value; |
| 31 | children.push( |
| 32 | <span key={i} className={styles[type]}> |
| 33 | {child} |
| 34 | </span> |
| 35 | ); |
| 36 | } else if (Array.isArray(value)) { |
| 37 | // A nested array of tokens. |
| 38 | children.push(<CodeClient key={i} tokens={value} />); |
| 39 | } else { |
| 40 | children.push(value); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return children; |
| 45 | } |