Raxol.UI.Components.CodeBlock (Raxol v2.6.1)

View Source

Renders a block of code with structured syntax highlighting for the terminal.

Reuses Raxol.UI.SyntaxHighlighter — the same Makeup-backed, token-based path that Pierre-style diffs (Harness.DiffViewer) use. Tokens carry hex fg + font styles and are painted as styled text/1 spans; raw ANSI is never embedded (only applied at the final Terminal.Renderer).

The previous implementation called Makeup.highlight_inner_html/2 and then stripped the HTML tags, which discarded every color. That path is gone.

Props

  • :content (string) — source code (default "")
  • :language (string) — language name or extension for Makeup.Registry (e.g. "elixir", "ex", "python"). Unknown languages degrade to unstyled plain text, never raise.
  • :theme (atom | %Makeup.Styles.HTML.Style{}) — Makeup style (default :one_dark, same as DiffViewer)

Summary

Functions

Handles events for the component. No events are handled by default.

Initializes the component state from props.

Renders the code block as a column of token-span rows.

Highlights source and returns one row element per line (token spans).

Updates the component state. No updates are handled by default.

Functions

broadcast(msg)

command(cmd)

handle_event(event, state, context)

@spec handle_event(term(), map(), map()) :: {map(), list()}

Handles events for the component. No events are handled by default.

init(props)

@spec init(map()) :: {:ok, map()}

Initializes the component state from props.

render(state, context)

@spec render(map(), map()) :: map()

Renders the code block as a column of token-span rows.

render_lines(source, language \\ "text", theme \\ :one_dark)

@spec render_lines(String.t(), String.t() | nil, atom() | term()) :: [map()]

Highlights source and returns one row element per line (token spans).

Public so other renderers (e.g. MarkdownRenderer fenced blocks) can reuse the exact CodeBlock line shape without nesting a full column. Empty lines yield a single empty text cell so vertical spacing holds.

schedule(msg, delay)

update(props, state)

@spec update(term(), map()) :: map()

Updates the component state. No updates are handled by default.