pre.hn/lib/pre_dot_hn/link_log.ex
2024-03-19 08:53:37 -05:00

84 lines
2.1 KiB
Elixir

defmodule PreDotHn.LinkLog do
alias PreDotHn.Markdown
use Phoenix.Component
import Phoenix.LiveViewTest, only: [rendered_to_string: 1]
def run do
links = links()
make_feed(links)
links
|> make_index()
|> PreDotHn.write_page()
end
def links do
link_path = Path.join(["site", "link-log.yaml"])
YamlElixir.read_from_file!(link_path)
end
def make_feed(links) do
links = Enum.take(links, 10)
assigns = %{links: links}
body =
EEx.eval_string(
"""
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>pre.hn - Links</title>
<description>Robert Prehn's Link Log</description>
<link>https://pre.hn/link-log/</link>
<atom:link href="https://pre.hn/link-log/feed.rss" rel="self" type="application/rss+xml" />
<%= for link <- @links do %>
<item>
<title>🔗 <%= HtmlSanitizeEx.strip_tags(link["name"]) %></title>
<description>
<%= link["summary"] %>
</description>
<link><%= link["url"] %></link>
<guid isPermaLink="true"><%= link["url"] %></guid>
</item>
<% end %>
</channel>
</rss>
""",
assigns: assigns
)
path = Path.join(["priv", "static", "link-log", "feed.rss"])
File.write!(path, body)
end
def make_index(links) do
assigns = %{links: links}
body =
~H"""
<%= for link <- @links do %>
<div style="margin-bottom: 1rem">
<a class="text-ellipsis overflow-hidden inline-block" style="padding-left: 2rem;text-indent: -2rem;" href={link["url"]}>
🔗 <%= link["name"] %>
</a>
<div class="link-log-body">
<%= {:safe, Markdown.render(link["summary"])} %>
</div>
</div>
<% end %>
"""
|> rendered_to_string()
%{
title: "Link Log",
slug: "link-log",
date: "2024-03-07",
body: body
}
end
end