@@ -1,6 +1,6 @@
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Conversations.Comment do
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Discussions.Comment do
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Conversations.Comment
|
||||
alias Mobilizon.Discussions.Comment
|
||||
|
||||
def build_tags(%Comment{} = comment, _locale \\ "en") do
|
||||
[
|
||||
|
||||
@@ -2,10 +2,9 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
alias Phoenix.HTML
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
alias Mobilizon.Web.MediaProxy
|
||||
import Mobilizon.Web.Gettext
|
||||
import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1]
|
||||
|
||||
def build_tags(%Event{} = event, locale \\ "en") do
|
||||
event = Map.put(event, :description, process_description(event.description, locale))
|
||||
@@ -41,24 +40,10 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
]
|
||||
end
|
||||
|
||||
defp process_description(nil, locale), do: process_description("", locale)
|
||||
|
||||
defp process_description("", locale) do
|
||||
Gettext.put_locale(locale)
|
||||
gettext("The event organizer didn't add any description.")
|
||||
end
|
||||
|
||||
defp process_description(description, _locale) do
|
||||
description
|
||||
|> HTMLFormatter.strip_tags()
|
||||
|> String.slice(0..200)
|
||||
|> (&"#{&1}…").()
|
||||
end
|
||||
|
||||
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
||||
defp json(%Event{title: title} = event) do
|
||||
"event.json"
|
||||
|> ObjectView.render(%{event: %{event | title: HTMLFormatter.strip_tags(title)}})
|
||||
|> ObjectView.render(%{event: %{event | title: strip_tags(title)}})
|
||||
|> Jason.encode!()
|
||||
end
|
||||
end
|
||||
|
||||
34
lib/service/metadata/post.ex
Normal file
34
lib/service/metadata/post.ex
Normal file
@@ -0,0 +1,34 @@
|
||||
defimpl Mobilizon.Service.Metadata, for: Mobilizon.Posts.Post do
|
||||
alias Phoenix.HTML
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
import Mobilizon.Service.Metadata.Utils, only: [process_description: 2, strip_tags: 1]
|
||||
|
||||
def build_tags(%Post{} = post, locale \\ "en") do
|
||||
post = Map.put(post, :body, process_description(post.body, locale))
|
||||
|
||||
tags = [
|
||||
Tag.tag(:meta, property: "og:title", content: post.title),
|
||||
Tag.tag(:meta, property: "og:url", content: post.url),
|
||||
Tag.tag(:meta, property: "og:description", content: post.body),
|
||||
Tag.tag(:meta, property: "og:type", content: "article"),
|
||||
Tag.tag(:meta, property: "twitter:card", content: "summary"),
|
||||
# Tell Search Engines what's the origin
|
||||
Tag.tag(:link, rel: "canonical", href: post.url)
|
||||
]
|
||||
|
||||
tags ++
|
||||
[
|
||||
Tag.tag(:meta, property: "twitter:card", content: "summary_large_image"),
|
||||
~s{<script type="application/ld+json">#{json(post)}</script>} |> HTML.raw()
|
||||
]
|
||||
end
|
||||
|
||||
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
|
||||
defp json(%Post{title: title} = post) do
|
||||
"post.json"
|
||||
|> ObjectView.render(%{post: %{post | title: strip_tags(title)}})
|
||||
|> Jason.encode!()
|
||||
end
|
||||
end
|
||||
@@ -3,10 +3,34 @@ defmodule Mobilizon.Service.Metadata.Utils do
|
||||
Tools to convert tags to string.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.Formatter.HTML, as: HTMLFormatter
|
||||
alias Phoenix.HTML
|
||||
import Mobilizon.Web.Gettext
|
||||
|
||||
@slice_limit 200
|
||||
|
||||
@spec stringify_tags(Enum.t()) :: String.t()
|
||||
def stringify_tags(tags), do: Enum.reduce(tags, "", &stringify_tag/2)
|
||||
|
||||
defp stringify_tag(tag, acc) when is_tuple(tag), do: acc <> HTML.safe_to_string(tag)
|
||||
defp stringify_tag(tag, acc) when is_binary(tag), do: acc <> tag
|
||||
|
||||
@spec strip_tags(String.t()) :: String.t()
|
||||
def strip_tags(text), do: HTMLFormatter.strip_tags(text)
|
||||
|
||||
@spec process_description(String.t(), String.t(), integer()) :: String.t()
|
||||
def process_description(description, locale \\ "en", limit \\ @slice_limit)
|
||||
def process_description(nil, locale, limit), do: process_description("", locale, limit)
|
||||
|
||||
def process_description("", locale, _limit) do
|
||||
Gettext.put_locale(locale)
|
||||
gettext("The event organizer didn't add any description.")
|
||||
end
|
||||
|
||||
def process_description(description, _locale, limit) do
|
||||
description
|
||||
|> HTMLFormatter.strip_tags()
|
||||
|> String.slice(0..limit)
|
||||
|> (&"#{&1}…").()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user