Choreo.Livebook (Choreo v0.11.0)

Copy Markdown View Source

Shared helpers for parsing and headlessly evaluating Choreo Livebooks.

Used by Mix.Tasks.Choreo.TestLivebooks and Choreo.MCP so that both tools validate notebooks with the same parser, mock suite, and evaluation semantics.

Summary

Functions

Evaluates the extracted Elixir blocks in a headless environment.

Extracts elixir code blocks from a Livebook markdown string.

Parses markdown headers and their contents, skipping lines inside fenced code blocks so that commented Elixir code is not mistaken for a section.

Functions

evaluate_blocks(blocks)

@spec evaluate_blocks([String.t()]) ::
  :ok | {:error, struct(), Exception.stacktrace()}

Evaluates the extracted Elixir blocks in a headless environment.

  • Strips Mix.install([...]) calls (the host already has dependencies).
  • Redirects Kino.* calls to Choreo.Livebook.KinoMock.
  • Replaces visual renderers with Kernel.inspect so they return plain values.

Returns :ok on success or {:error, exception, stacktrace} on failure.

extract_elixir_blocks(content)

@spec extract_elixir_blocks(String.t()) :: [String.t()]

Extracts elixir code blocks from a Livebook markdown string.

The parser is nested-fence aware: it matches the exact opening backtick sequence and only closes the block on the same sequence. This avoids truncating blocks that contain triple-backtick strings.

parse_sections(content)

@spec parse_sections(String.t()) :: [%{section: String.t(), content: String.t()}]

Parses markdown headers and their contents, skipping lines inside fenced code blocks so that commented Elixir code is not mistaken for a section.