defmodule ZoeyscomputerWeb.CodeBlock do use Phoenix.Component alias Phoenix.LiveView.JS @moduledoc """ A code block component with syntax highlighting using Shiki. ## Features: - Syntax highlighting with Shiki - Optional line numbers - Optional highlighted lines - Optional title - Copy button - Catppuccin theme styling """ @doc """ Renders a code block with syntax highlighting. ## Examples <.code_block code="def hello, do: :world" language="elixir" title="Example Code" line_numbers={true} highlighted_lines={[1, 3]} /> ## Options * `:code` - Required. The code string to highlight * `:language` - Required. Programming language for syntax highlighting * `:title` - Optional. Title displayed above the code block * `:line_numbers` - Optional. Show line numbers (default: false) * `:highlighted_lines` - Optional. List of line numbers to highlight """ attr :code, :string, required: true attr :language, :string, required: true attr :title, :string, default: nil attr :line_numbers, :boolean, default: false attr :highlighted_lines, :list, default: [] def code_block(assigns) do # Calculate the number of lines for line numbers assigns = assign(assigns, :num_lines, String.split(assigns.code, "\n") |> length()) ~H"""
<%= if @title do %>

<%= @title %>

<% end %>
<%= if @line_numbers do %>
<%= for line_num <- 1..@num_lines do %> <%= line_num %> <% end %>
<% end %>
<%= @code %>
""" end end