highlightYAML syntax-colorizes YAML using chroma with the active TUI theme. On any tokenisation error it returns the (sanitized) source unchanged.
(src string)
| 463 | // highlightYAML syntax-colorizes YAML using chroma with the active TUI theme. |
| 464 | // On any tokenisation error it returns the (sanitized) source unchanged. |
| 465 | func highlightYAML(src string) string { |
| 466 | src = sanitizeYAML(src) |
| 467 | lexer := lexers.Get("yaml") |
| 468 | if lexer == nil { |
| 469 | return src |
| 470 | } |
| 471 | iterator, err := chroma.Coalesce(lexer).Tokenise(nil, src) |
| 472 | if err != nil { |
| 473 | return src |
| 474 | } |
| 475 | |
| 476 | style := styles.ChromaStyle() |
| 477 | var b strings.Builder |
| 478 | for _, token := range iterator.Tokens() { |
| 479 | b.WriteString(chromaTokenStyle(token.Type, style).Render(token.Value)) |
| 480 | } |
| 481 | return b.String() |
| 482 | } |
| 483 | |
| 484 | // sanitizeYAML normalizes line endings, expands tabs, and strips terminal |
| 485 | // control characters from config content that may come from untrusted (remote) |