Raxol.Playground.Snippet (Raxol v2.9.2)

View Source

Extracts a demo module's marked region as its catalog snippet.

A demo brackets its most illustrative lines with two comment lines:

# snippet:start
...the code the catalog shows...
# snippet:end

and Raxol.Playground.Catalog derives code_snippet from that region at compile time. The point is provenance: a hand-written snippet beside a demo can drift from it silently, but a region extracted from the demo's own source is code the suite already boots and renders. The markers are comments, so they change no rendered frame and no recorded preview.

Extraction is text slicing, never compilation, so it cannot disturb the frame recorder the way compiling shown source once did. It reads the file the formatter owns, so a derived snippet is formatter-clean by construction.

Summary

Functions

The marked region of path, or :no_markers.

The source path a demo module's snippet extracts from, by convention: Raxol.Playground.Demos.ButtonDemo -> demos/button_demo.ex beside this file. Register it as @external_resource so editing the demo recompiles the catalog.

Whether subject says no more than name already does.

The component call a snippet demonstrates ("Viewport.init"), or nil.

Functions

extract(path)

@spec extract(Path.t()) :: {:ok, String.t()} | :no_markers

The marked region of path, or :no_markers.

Returns the lines strictly between exactly one marker pair, with their common indent stripped and a trailing newline, which is the shape the hand-written heredoc snippets had. Raises on a lone or duplicated marker and on a region past 24 lines: both are authoring mistakes the build should name, not states to render.

path_for(module)

@spec path_for(module()) :: Path.t()

The source path a demo module's snippet extracts from, by convention: Raxol.Playground.Demos.ButtonDemo -> demos/button_demo.ex beside this file. Register it as @external_resource so editing the demo recompiles the catalog.

redundant?(name, subject)

@spec redundant?(String.t(), String.t() | nil) :: boolean()

Whether subject says no more than name already does.

"button" under a card named "Button", or "Table.init" under "Table", is noise; "Viewport.init" under "Scroll Anchor" is the answer to a real question. Compared with case and separators dropped, against each dotted part of the subject, so both the module and the function form of a name count as already said.

subject(snippet)

@spec subject(String.t()) :: String.t() | nil

The component call a snippet demonstrates ("Viewport.init"), or nil.

This is what the gallery card shows under a plain-language name, so a reader hunting for the module is not left guessing: the card may say "Scroll Anchor", the sub-line says Viewport.init. Only a qualified non-stdlib call qualifies — a DSL one-liner's subject IS the card's name, and a generic answer ("row", an assignment's left side) is worse than none. Shallow module paths win over deep ones, so a utility import inside the region cannot shadow the component the region is about. Derived from the snippet rather than written beside it, so it cannot name something the snippet stopped showing.