render collections correctly in markdownrenderer

This commit is contained in:
Luna D 2021-09-29 22:24:38 +02:00
parent 1739373bf7
commit fd17684882
No known key found for this signature in database
GPG key ID: 81AF416F2CC36FC8
19 changed files with 61 additions and 64 deletions

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.Admin.DnpEntryController do defmodule PhilomenaWeb.Admin.DnpEntryController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.DnpEntries.DnpEntry alias Philomena.DnpEntries.DnpEntry
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query import Ecto.Query
@ -45,7 +45,7 @@ defmodule PhilomenaWeb.Admin.DnpEntryController do
bodies = bodies =
dnp_entries dnp_entries
|> Enum.map(&%{body: &1.conditions}) |> Enum.map(&%{body: &1.conditions})
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)} dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.Admin.ModNoteController do defmodule PhilomenaWeb.Admin.ModNoteController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.ModNotes.ModNote alias Philomena.ModNotes.ModNote
alias Philomena.Polymorphic alias Philomena.Polymorphic
alias Philomena.ModNotes alias Philomena.ModNotes
@ -29,7 +29,7 @@ defmodule PhilomenaWeb.Admin.ModNoteController do
|> order_by(desc: :id) |> order_by(desc: :id)
|> Repo.paginate(conn.assigns.scrivener) |> Repo.paginate(conn.assigns.scrivener)
bodies = TextRenderer.render_collection(mod_notes, conn) bodies = MarkdownRenderer.render_collection(mod_notes, conn)
preloaded = Polymorphic.load_polymorphic(mod_notes, notable: [notable_id: :notable_type]) preloaded = Polymorphic.load_polymorphic(mod_notes, notable: [notable_id: :notable_type])
mod_notes = %{mod_notes | entries: Enum.zip(bodies, preloaded)} mod_notes = %{mod_notes | entries: Enum.zip(bodies, preloaded)}

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Reports.Report alias Philomena.Reports.Report
alias Philomena.Reports.Query alias Philomena.Reports.Query
alias Philomena.Polymorphic alias Philomena.Polymorphic
@ -73,7 +73,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
reportable: [reportable_id: :reportable_type] reportable: [reportable_id: :reportable_type]
) )
body = TextRenderer.render_one(%{body: report.reason}, conn) body = MarkdownRenderer.render_one(%{body: report.reason}, conn)
render(conn, "show.html", title: "Showing Report", report: report, body: body) render(conn, "show.html", title: "Showing Report", report: report, body: body)
end end
@ -125,7 +125,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
mod_notes = mod_notes =
mod_notes mod_notes
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes) |> Enum.zip(mod_notes)
assign(conn, :mod_notes, mod_notes) assign(conn, :mod_notes, mod_notes)

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.CommentController do defmodule PhilomenaWeb.CommentController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.{Comments.Query, Comments.Comment} alias Philomena.{Comments.Query, Comments.Comment}
import Ecto.Query import Ecto.Query
@ -39,7 +39,7 @@ defmodule PhilomenaWeb.CommentController do
preload(Comment, [:deleted_by, image: [tags: :aliases], user: [awards: :badge]]) preload(Comment, [:deleted_by, image: [tags: :aliases], user: [awards: :badge]])
) )
rendered = TextRenderer.render_collection(comments.entries, conn) rendered = MarkdownRenderer.render_collection(comments.entries, conn)
comments = %{comments | entries: Enum.zip(rendered, comments.entries)} comments = %{comments | entries: Enum.zip(rendered, comments.entries)}

View file

@ -3,7 +3,7 @@ defmodule PhilomenaWeb.ConversationController do
alias PhilomenaWeb.NotificationCountPlug alias PhilomenaWeb.NotificationCountPlug
alias Philomena.{Conversations, Conversations.Conversation, Conversations.Message} alias Philomena.{Conversations, Conversations.Conversation, Conversations.Message}
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query import Ecto.Query
@ -72,7 +72,7 @@ defmodule PhilomenaWeb.ConversationController do
rendered = rendered =
messages.entries messages.entries
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
messages = %{messages | entries: Enum.zip(messages.entries, rendered)} messages = %{messages | entries: Enum.zip(messages.entries, rendered)}

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.DnpEntryController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.DnpEntries.DnpEntry alias Philomena.DnpEntries.DnpEntry
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.DnpEntries alias Philomena.DnpEntries
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.ModNotes.ModNote alias Philomena.ModNotes.ModNote
@ -44,7 +44,7 @@ defmodule PhilomenaWeb.DnpEntryController do
bodies = bodies =
dnp_entries dnp_entries
|> Enum.map(&%{body: &1.conditions || "-"}) |> Enum.map(&%{body: &1.conditions || "-"})
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)} dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
@ -61,7 +61,7 @@ defmodule PhilomenaWeb.DnpEntryController do
dnp_entry = conn.assigns.dnp_entry dnp_entry = conn.assigns.dnp_entry
[conditions, reason, instructions] = [conditions, reason, instructions] =
TextRenderer.render_collection( MarkdownRenderer.render_collection(
[ [
%{body: dnp_entry.conditions || "-"}, %{body: dnp_entry.conditions || "-"},
%{body: dnp_entry.reason || "-"}, %{body: dnp_entry.reason || "-"},
@ -164,7 +164,7 @@ defmodule PhilomenaWeb.DnpEntryController do
mod_notes = mod_notes =
mod_notes mod_notes
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes) |> Enum.zip(mod_notes)
assign(conn, :mod_notes, mod_notes) assign(conn, :mod_notes, mod_notes)

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.Image.CommentController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.CommentLoader alias PhilomenaWeb.CommentLoader
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.{Images.Image, Comments.Comment} alias Philomena.{Images.Image, Comments.Comment}
alias Philomena.UserStatistics alias Philomena.UserStatistics
alias Philomena.Comments alias Philomena.Comments
@ -48,7 +48,7 @@ defmodule PhilomenaWeb.Image.CommentController do
def index(conn, _params) do def index(conn, _params) do
comments = CommentLoader.load_comments(conn, conn.assigns.image) comments = CommentLoader.load_comments(conn, conn.assigns.image)
rendered = TextRenderer.render_collection(comments.entries, conn) rendered = MarkdownRenderer.render_collection(comments.entries, conn)
comments = %{comments | entries: Enum.zip(comments.entries, rendered)} comments = %{comments | entries: Enum.zip(comments.entries, rendered)}
@ -56,7 +56,7 @@ defmodule PhilomenaWeb.Image.CommentController do
end end
def show(conn, _params) do def show(conn, _params) do
rendered = TextRenderer.render_one(conn.assigns.comment, conn) rendered = MarkdownRenderer.render_one(conn.assigns.comment, conn)
render(conn, "show.html", render(conn, "show.html",
layout: false, layout: false,

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.Image.DescriptionController do defmodule PhilomenaWeb.Image.DescriptionController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Images alias Philomena.Images
@ -35,7 +35,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do
Images.reindex_image(image) Images.reindex_image(image)
body = body =
TextRenderer.render_one(%{body: image.description}, conn) MarkdownRenderer.render_one(%{body: image.description}, conn)
conn conn
|> put_view(PhilomenaWeb.ImageView) |> put_view(PhilomenaWeb.ImageView)

View file

@ -4,7 +4,7 @@ defmodule PhilomenaWeb.ImageController do
alias PhilomenaWeb.ImageLoader alias PhilomenaWeb.ImageLoader
alias PhilomenaWeb.CommentLoader alias PhilomenaWeb.CommentLoader
alias PhilomenaWeb.NotificationCountPlug alias PhilomenaWeb.NotificationCountPlug
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.{ alias Philomena.{
Images, Images,
@ -63,13 +63,13 @@ defmodule PhilomenaWeb.ImageController do
comments = CommentLoader.load_comments(conn, image) comments = CommentLoader.load_comments(conn, image)
rendered = TextRenderer.render_collection(comments.entries, conn) rendered = MarkdownRenderer.render_collection(comments.entries, conn)
comments = %{comments | entries: Enum.zip(comments.entries, rendered)} comments = %{comments | entries: Enum.zip(comments.entries, rendered)}
description = description =
%{body: image.description} %{body: image.description}
|> TextRenderer.render_one(conn) |> MarkdownRenderer.render_one(conn)
interactions = Interactions.user_interactions([image], conn.assigns.current_user) interactions = Interactions.user_interactions([image], conn.assigns.current_user)

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.Post.PreviewController do defmodule PhilomenaWeb.Post.PreviewController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Posts.Post alias Philomena.Posts.Post
alias Philomena.Repo alias Philomena.Repo
@ -11,7 +11,7 @@ defmodule PhilomenaWeb.Post.PreviewController do
anonymous = params["anonymous"] == true anonymous = params["anonymous"] == true
post = %Post{user: user, body: body, anonymous: anonymous} post = %Post{user: user, body: body, anonymous: anonymous}
rendered = TextRenderer.render_one(post, conn) rendered = MarkdownRenderer.render_one(post, conn)
render(conn, "create.html", layout: false, post: post, body: rendered) render(conn, "create.html", layout: false, post: post, body: rendered)
end end

View file

@ -1,7 +1,7 @@
defmodule PhilomenaWeb.PostController do defmodule PhilomenaWeb.PostController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.{Posts.Query, Posts.Post} alias Philomena.{Posts.Query, Posts.Post}
import Ecto.Query import Ecto.Query
@ -36,7 +36,7 @@ defmodule PhilomenaWeb.PostController do
preload(Post, [:deleted_by, topic: :forum, user: [awards: :badge]]) preload(Post, [:deleted_by, topic: :forum, user: [awards: :badge]])
) )
rendered = TextRenderer.render_collection(posts.entries, conn) rendered = MarkdownRenderer.render_collection(posts.entries, conn)
posts = %{posts | entries: Enum.zip(rendered, posts.entries)} posts = %{posts | entries: Enum.zip(rendered, posts.entries)}

View file

@ -3,7 +3,7 @@ defmodule PhilomenaWeb.Profile.CommissionController do
alias Philomena.Commissions.Commission alias Philomena.Commissions.Commission
alias Philomena.Commissions alias Philomena.Commissions
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Users.User alias Philomena.Users.User
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete] plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete]
@ -37,15 +37,15 @@ defmodule PhilomenaWeb.Profile.CommissionController do
item_descriptions = item_descriptions =
items items
|> Enum.map(&%{body: &1.description}) |> Enum.map(&%{body: &1.description})
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
item_add_ons = item_add_ons =
items items
|> Enum.map(&%{body: &1.add_ons}) |> Enum.map(&%{body: &1.add_ons})
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
[information, contact, will_create, will_not_create] = [information, contact, will_create, will_not_create] =
TextRenderer.render_collection( MarkdownRenderer.render_collection(
[ [
%{body: commission.information || ""}, %{body: commission.information || ""},
%{body: commission.contact || ""}, %{body: commission.contact || ""},

View file

@ -3,7 +3,7 @@ defmodule PhilomenaWeb.Profile.DetailController do
alias Philomena.UserNameChanges.UserNameChange alias Philomena.UserNameChanges.UserNameChange
alias Philomena.ModNotes.ModNote alias Philomena.ModNotes.ModNote
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Polymorphic alias Philomena.Polymorphic
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Repo alias Philomena.Repo
@ -30,7 +30,7 @@ defmodule PhilomenaWeb.Profile.DetailController do
mod_notes = mod_notes =
mod_notes mod_notes
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes) |> Enum.zip(mod_notes)
name_changes = name_changes =

View file

@ -3,7 +3,7 @@ defmodule PhilomenaWeb.ProfileController do
alias PhilomenaWeb.ImageLoader alias PhilomenaWeb.ImageLoader
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.UserStatistics.UserStatistic alias Philomena.UserStatistics.UserStatistic
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Bans alias Philomena.Bans
@ -131,14 +131,14 @@ defmodule PhilomenaWeb.ProfileController do
recent_comments = recent_comments =
recent_comments recent_comments
|> Enum.filter(&Canada.Can.can?(current_user, :show, &1.image)) |> Enum.filter(&Canada.Can.can?(current_user, :show, &1.image))
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(recent_comments) |> Enum.zip(recent_comments)
about_me = about_me =
TextRenderer.render_one(%{body: user.description || ""}, conn) MarkdownRenderer.render_one(%{body: user.description || ""}, conn)
scratchpad = scratchpad =
TextRenderer.render_one(%{body: user.scratchpad || ""}, conn) MarkdownRenderer.render_one(%{body: user.scratchpad || ""}, conn)
commission_information = commission_info(user.commission, conn) commission_information = commission_info(user.commission, conn)
@ -218,7 +218,7 @@ defmodule PhilomenaWeb.ProfileController do
defp commission_info(%{information: info}, conn) defp commission_info(%{information: info}, conn)
when info not in [nil, ""], when info not in [nil, ""],
do: TextRenderer.render_one(%{body: info}, conn) do: MarkdownRenderer.render_one(%{body: info}, conn)
defp commission_info(_commission, _conn), do: "" defp commission_info(_commission, _conn), do: ""
@ -285,7 +285,7 @@ defmodule PhilomenaWeb.ProfileController do
mod_notes = mod_notes =
mod_notes mod_notes
|> TextRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes) |> Enum.zip(mod_notes)
assign(conn, :mod_notes, mod_notes) assign(conn, :mod_notes, mod_notes)

View file

@ -5,7 +5,7 @@ defmodule PhilomenaWeb.TagController do
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.{Tags, Tags.Tag} alias Philomena.{Tags, Tags.Tag}
alias Philomena.{Images, Images.Image} alias Philomena.{Images, Images.Image}
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Interactions alias Philomena.Interactions
import Ecto.Query import Ecto.Query
@ -62,10 +62,10 @@ defmodule PhilomenaWeb.TagController do
interactions = Interactions.user_interactions(images, user) interactions = Interactions.user_interactions(images, user)
body = body =
TextRenderer.render_one(%{body: tag.description || ""}, conn) MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
dnp_bodies = dnp_bodies =
TextRenderer.render_collection( MarkdownRenderer.render_collection(
Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}), Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}),
conn conn
) )

View file

@ -5,7 +5,7 @@ defmodule PhilomenaWeb.TopicController do
alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post, Polls.Poll, PollOptions.PollOption} alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post, Polls.Poll, PollOptions.PollOption}
alias Philomena.{Forums, Topics, Polls, Posts} alias Philomena.{Forums, Topics, Polls, Posts}
alias Philomena.PollVotes alias Philomena.PollVotes
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query import Ecto.Query
@ -60,7 +60,7 @@ defmodule PhilomenaWeb.TopicController do
|> preload([:deleted_by, :topic, topic: :forum, user: [awards: :badge]]) |> preload([:deleted_by, :topic, topic: :forum, user: [awards: :badge]])
|> Repo.all() |> Repo.all()
rendered = TextRenderer.render_collection(posts, conn) rendered = MarkdownRenderer.render_collection(posts, conn)
posts = Enum.zip(posts, rendered) posts = Enum.zip(posts, rendered)

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.ImageLoader do
alias PhilomenaWeb.ImageSorter alias PhilomenaWeb.ImageSorter
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.Images.{Image, Query} alias Philomena.Images.{Image, Query}
alias PhilomenaWeb.TextRenderer alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query import Ecto.Query
@ -132,7 +132,7 @@ defmodule PhilomenaWeb.ImageLoader do
defp render_bodies([tag], conn) do defp render_bodies([tag], conn) do
dnp_bodies = dnp_bodies =
TextRenderer.render_collection( MarkdownRenderer.render_collection(
Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}), Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}),
conn conn
) )
@ -140,7 +140,7 @@ defmodule PhilomenaWeb.ImageLoader do
dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries) dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries)
description = description =
TextRenderer.render_one(%{body: tag.description || ""}, conn) MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
[{tag, description, dnp_entries}] [{tag, description, dnp_entries}]
end end

View file

@ -8,11 +8,21 @@ defmodule PhilomenaWeb.MarkdownRenderer do
@image_view Module.concat(["PhilomenaWeb.ImageView"]) @image_view Module.concat(["PhilomenaWeb.ImageView"])
def render(text, conn) do def render_one(item, conn) do
images = find_images(text) hd(render_collection([item], conn))
representations = render_representations(images, conn) end
def render_collection(collection, conn) do
representations =
collection
|> Enum.flat_map(fn %{body: text} ->
find_images(text)
end)
|> render_representations(conn)
Enum.map(collection, fn %{body: text} ->
Markdown.to_html(text, representations) Markdown.to_html(text, representations)
end)
end end
def render_unsafe(text, conn) do def render_unsafe(text, conn) do

View file

@ -1,13 +0,0 @@
defmodule PhilomenaWeb.TextRenderer do
alias PhilomenaWeb.MarkdownRenderer
def render_one(item, conn) do
hd(render_collection([item], conn))
end
def render_collection(items, conn) do
Enum.map(items, fn item ->
MarkdownRenderer.render(item.body, conn)
end)
end
end