Support empty titles

This commit is contained in:
Daniel Kempkens 2023-05-25 22:07:28 +02:00
parent 64b189ab23
commit c819ff2f20
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM

View file

@ -104,7 +104,7 @@ defmodule BdfrBrowser.HTTP.Plug do
{:ok, content} = [post_dir, post] |> Path.join() |> File.read!() |> Jason.decode() {:ok, content} = [post_dir, post] |> Path.join() |> File.read!() |> Jason.decode()
%{ %{
title: content["title"], title: extract_title(content["title"]),
author: content["author"], author: content["author"],
num_comments: content["num_comments"], num_comments: content["num_comments"],
created_utc: content["created_utc"], created_utc: content["created_utc"],
@ -115,12 +115,21 @@ defmodule BdfrBrowser.HTTP.Plug do
Enum.sort_by(compact_posts, fn p -> p.created_utc end, sort) Enum.sort_by(compact_posts, fn p -> p.created_utc end, sort)
end end
defp extract_title(title) do
if String.length(title) == 0 do
"Empty Title"
else
title
end
end
defp read_post(post, args) do defp read_post(post, args) do
base_directory = Application.fetch_env!(:bdfr_browser, :base_directory) base_directory = Application.fetch_env!(:bdfr_browser, :base_directory)
post_dir = Path.join([base_directory | Keyword.fetch!(args, :paths)]) post_dir = Path.join([base_directory | Keyword.fetch!(args, :paths)])
post_file = "#{post}.json" post_file = "#{post}.json"
{:ok, content} = [post_dir, post_file] |> Path.join() |> File.read!() |> Jason.decode(keys: :atoms) {:ok, content} = [post_dir, post_file] |> Path.join() |> File.read!() |> Jason.decode(keys: :atoms)
content content
end end