diff options
Diffstat (limited to 'lib/plugins/link/html.ex')
-rw-r--r-- | lib/plugins/link/html.ex | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/lib/plugins/link/html.ex b/lib/plugins/link/html.ex index 5899ed5..bef9640 100644 --- a/lib/plugins/link/html.ex +++ b/lib/plugins/link/html.ex @@ -10,14 +10,15 @@ defmodule Nola.Plugins.Link.HTML do @impl true def post_expand(url, body, _params, _opts) do - html = Floki.parse(body) + {:ok, html} = Floki.parse_document(body) opengraph = collect_open_graph(html) - text = if has_sufficient_opengraph_data?(opengraph) do - generate_text_from_opengraph(url, html, opengraph) - else - clean_text(collect_title(html)) - end + text = + if has_sufficient_opengraph_data?(opengraph) do + generate_text_from_opengraph(url, html, opengraph) + else + clean_text(collect_title(html)) + end {:ok, text} end @@ -55,16 +56,17 @@ defmodule Nola.Plugins.Link.HTML do _ -> acc end end - defp extract_meta_tag(_, acc), do: acc - defp is_valid_meta_tag?(name) do - String.starts_with?(name, "og:") || String.starts_with?(name, "article:") - end + defp extract_meta_tag(_, acc), do: acc defp is_valid_meta_tag?(nil) do false end + defp is_valid_meta_tag?(name) do + String.starts_with?(name, "og:") || String.starts_with?(name, "article:") + end + defp strip_prefix("og:" <> key), do: key defp strip_prefix(other), do: other @@ -82,6 +84,7 @@ defmodule Nola.Plugins.Link.HTML do _ -> acc end end + defp extract_itemprop(_, acc), do: acc defp collect_prefix_and_site_name(url, opengraph, itemprops) do @@ -95,7 +98,13 @@ defmodule Nola.Plugins.Link.HTML do end defp get_paywall_status(opengraph, itemprops) do - content_tier = Map.get(opengraph, "article:content_tier", Map.get(itemprops, "article:content_tier", "free")) + content_tier = + Map.get( + opengraph, + "article:content_tier", + Map.get(itemprops, "article:content_tier", "free") + ) + if content_tier == "free", do: "", else: "[paywall] " end @@ -112,7 +121,13 @@ defmodule Nola.Plugins.Link.HTML do end defp get_formatted_date(opengraph, itemprops) do - published_time = Map.get(opengraph, "article:published_time", Map.get(itemprops, "article:published_time", "")) + published_time = + Map.get( + opengraph, + "article:published_time", + Map.get(itemprops, "article:published_time", "") + ) + case DateTime.from_iso8601(published_time) do {:ok, date, _} -> "#{Timex.format!(date, "%d/%m/%y", :strftime)}. " _ -> "" @@ -120,15 +135,15 @@ defmodule Nola.Plugins.Link.HTML do end # TODO: Swap with AI description instead of truncating. + defp transform_description(nil, _), do: nil + defp transform_description(string, length) when is_binary(string) do if String.length(string) >= length, do: String.truncate(string, length), else: string end - defp transform_description(nil, _), do: nil defp clean_text(text) do text |> String.replace("\n", " ") |> HtmlEntities.decode() end - end |