forked from AkkomaGang/akkoma
Attempt to use <title> from HTML as a fallback
This commit is contained in:
parent
822a9f28d0
commit
097fdf6a5d
1 changed files with 7 additions and 1 deletions
|
@ -1,12 +1,14 @@
|
||||||
defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
|
defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
|
||||||
def parse(html, data, prefix, error_message, key_name, value_name \\ "content") do
|
def parse(html, data, prefix, error_message, key_name, value_name \\ "content") do
|
||||||
with elements = [_ | _] <- get_elements(html, key_name, prefix),
|
with elements = [_ | _] <- get_elements(html, key_name, prefix),
|
||||||
|
page_title = get_page_title(html),
|
||||||
meta_data =
|
meta_data =
|
||||||
Enum.reduce(elements, data, fn el, acc ->
|
Enum.reduce(elements, data, fn el, acc ->
|
||||||
attributes = normalize_attributes(el, prefix, key_name, value_name)
|
attributes = normalize_attributes(el, prefix, key_name, value_name)
|
||||||
|
|
||||||
Map.merge(acc, attributes)
|
Map.merge(acc, attributes)
|
||||||
end) do
|
end)
|
||||||
|
|> Map.put_new(:title, page_title) do
|
||||||
{:ok, meta_data}
|
{:ok, meta_data}
|
||||||
else
|
else
|
||||||
_e -> {:error, error_message}
|
_e -> {:error, error_message}
|
||||||
|
@ -27,4 +29,8 @@ defp normalize_attributes(html_node, prefix, key_name, value_name) do
|
||||||
|
|
||||||
%{String.to_atom(data[key_name]) => data[value_name]}
|
%{String.to_atom(data[key_name]) => data[value_name]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_page_title(html) do
|
||||||
|
Floki.find(html, "title") |> Floki.text()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue