Adding Markdown rendering to a Phoenix LiveView project can enhance the user experience by allowing you to display richly formatted content. Below is a step-by-step guide to integrate Markdown rendering into your Phoenix LiveView application.
Step 1: Add Dependencies
First, add the necessary dependencies to your mix.exs file. You'll need a Markdown parser library like Earmark or Markdown.
defp deps do
[
{:phoenix, "~> 1.6"},
{:phoenix_live_view, "~> 0.17"},
{:earmark, "~> 1.4"}, # or {:markdown, "~> 0.1"}
# Other dependencies...
]
end
Run mix deps.get to fetch and compile the new dependencies.
Step 2: Create a Markdown Helper Module
Create a helper module to handle Markdown rendering. This module will use the Markdown parser to convert Markdown text into HTML.
# lib/my_app/markdown.ex
defmodule MyApp.Markdown do
@moduledoc """
Helper module for rendering Markdown content.
"""
@doc """
Renders Markdown content as HTML.
"""
def to_html(markdown) do
case Earmark.as_html(markdown) do
{:ok, html, _messages} -> html
{:error, _messages} -> ""
end
end
end
Step 3: Use Markdown Rendering in LiveView
Now, you can use the MyApp.Markdown.to_html/1 function in your LiveView templates or logic to render Markdown content.
Example LiveView Module
# lib/my_app_web/live/markdown_live.ex
defmodule MyAppWeb.MarkdownLive do
use Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok, assign(socket, markdown_content: """
# Hello, Markdown!
This is a **bold** example of *Markdown* rendering in Phoenix LiveView.
- List item 1
- List item 2
"""
)}
end
def render(assigns) do
~H"""
<div>
<h1>Markdown Preview</h1>
<div class="markdown-content">
<%= raw MyApp.Markdown.to_html(@markdown_content) %>
</div>
</div>
"""
end
end
Example LiveView Template
If you're using a .leex or .heex template, you can directly call the to_html/1 function:
<!-- lib/my_app_web/templates/markdown/show.html.heex -->
<h1>Markdown Preview</h1>
<div class="markdown-content">
<%= raw MyApp.Markdown.to_html(@markdown_content) %>
</div>
Step 4: Add Styling (Optional)
You might want to add some CSS to style the rendered Markdown content. For example, you can add a markdown-content class and style it in your CSS file.
/* assets/css/app.css */
.markdown-content {
max-width: 800px;
margin: 0 auto;
padding: 20px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.markdown-content h1 {
color: #333;
}
.markdown-content p {
line-height: 1.6;
}
.markdown-content a {
color: #007bff;
text-decoration: none;
}
.markdown-content a:hover {
text-decoration: underline;
}
Step 5: Test Your Implementation
Start your Phoenix server with mix phx.server and navigate to the route where your LiveView is displayed. You should see the Markdown content rendered as HTML.
Additional Considerations
- Security: Be cautious when using
raw/1 to render HTML, especially if the Markdown content is user-generated. Sanitize the input if necessary.
- Performance: For large Markdown files, consider caching the rendered HTML to avoid re-rendering on every request.
- Extensions: Libraries like
Earmark support extensions (e.g., GitHub Flavored Markdown). Check the library's documentation for additional features.
By following these steps, you can seamlessly integrate Markdown rendering into your Phoenix LiveView project.