Skip to content

Commit

Permalink
feat: gen rss on assets.deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
abehidek committed Nov 16, 2023
1 parent 017ec58 commit 8c1bf18
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ hidek_xyz-*.tar
# Ignore digested assets cache.
/priv/static/cache_manifest.json

# Ignore generated rss.xml
/priv/static/rss.xml

# In case you use Node.js/npm, you want to ignore these.
npm-debug.log
/assets/node_modules/
Expand Down
1 change: 1 addition & 0 deletions lib/hidek_xyz/contents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule HidekXyz.Contents do
defexception [:message, plug_status: 404]
end

@spec all_articles() :: [%Article{}]
def all_articles, do: @articles

def get_article_by_slug!(slug) when is_binary(slug) do
Expand Down
2 changes: 1 addition & 1 deletion lib/hidek_xyz_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule HidekXyzWeb do
those modules here.
"""

def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt rss.xml)

def router do
quote do
Expand Down
43 changes: 43 additions & 0 deletions lib/mix/tasks/rss.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Mix.Tasks.Rss do
use Mix.Task

@destination "priv/static/rss.xml"

@impl Mix.Task
def run(_args) do
items = HidekXyz.Contents.all_articles()
|> Enum.map(&link_xml/1)
|> Enum.join()
|> IO.inspect()

document = """
<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
#{items}
</channel>
</rss>
"""

File.write(@destination, document)
end

defp link_xml(%HidekXyz.Article{} = article) do
host = case System.get_env("PHX_HOST") do
nil -> "http://localhost:4000"
host -> "https://#{host}"
end

link = "#{host}/contents/#{article.slug}"

"""
<item>
<title>#{article.title}</title>
<description>#{article.title}</description>
<pubDate>#{Calendar.strftime(article.publish_date, "%a, %d %B %Y 00:00:00 +0000")}</pubDate>
<link>#{link}</link>
<guid isPermaLink="true">#{link}</guid>
</item>
"""
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule HidekXyz.MixProject do
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
"assets.setup": ["tailwind.install --if-missing", "esbuild.install --if-missing"],
"assets.build": ["tailwind default", "esbuild default"],
"assets.deploy": ["tailwind default --minify", "esbuild default --minify", "phx.digest"]
"assets.deploy": ["tailwind default --minify", "esbuild default --minify", "rss", "phx.digest"]
]
end
end

0 comments on commit 8c1bf18

Please sign in to comment.