ash_blog/lib/data_layer/file_namer.ex

34 lines
833 B
Elixir
Raw Normal View History

2022-10-29 04:21:13 +13:00
defmodule AshBlog.FileNamer do
2022-10-29 04:40:14 +13:00
@moduledoc """
The default file namer, uses the current timestamp and the title attribute of the post in the form `YYYY/YYYY-MM-DD-name.md`
"""
2022-10-29 04:21:13 +13:00
def name_file(changeset) do
name =
2022-10-29 04:40:14 +13:00
case Ash.Changeset.get_attribute(
changeset,
AshBlog.DataLayer.Info.title_attribute(changeset.resource)
) do
2022-10-29 04:21:13 +13:00
nil ->
nil
name ->
name
|> String.replace(~r/[^a-zA-Z0-9 _]/, "")
|> String.replace(~r/[^a-zA-Z0-9]/, "-")
|> String.trim("-")
end
if name do
Calendar.strftime(
DateTime.utc_now(),
Path.join(["%Y", "%Y-%m-%d-#{name}.md"])
)
else
Calendar.strftime(
DateTime.utc_now(),
Path.join(["%Y", "%Y-%m-%d.md"])
)
end
end
end