Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.1KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Formatter do
  5. alias Pleroma.HTML
  6. alias Pleroma.User
  7. @safe_mention_regex ~r/^(\s*(?<mentions>(@.+?\s+){1,})+)(?<rest>.*)/s
  8. @link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
  9. @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
  10. defp linkify_opts do
  11. Pleroma.Config.get(Pleroma.Formatter) ++
  12. [
  13. hashtag: true,
  14. hashtag_handler: &Pleroma.Formatter.hashtag_handler/4,
  15. mention: true,
  16. mention_handler: &Pleroma.Formatter.mention_handler/4
  17. ]
  18. end
  19. def escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
  20. case User.get_cached_by_nickname(nickname) do
  21. %User{} ->
  22. # escape markdown characters with `\\`
  23. # (we don't want something like @user__name to be parsed by markdown)
  24. String.replace(mention, @markdown_characters_regex, "\\\\\\1")
  25. _ ->
  26. buffer
  27. end
  28. end
  29. def mention_handler("@" <> nickname, buffer, opts, acc) do
  30. case User.get_cached_by_nickname(nickname) do
  31. %User{id: id} = user ->
  32. user_url = user.uri || user.ap_id
  33. nickname_text = get_nickname_text(nickname, opts)
  34. link =
  35. Phoenix.HTML.Tag.content_tag(
  36. :span,
  37. Phoenix.HTML.Tag.content_tag(
  38. :a,
  39. ["@", Phoenix.HTML.Tag.content_tag(:span, nickname_text)],
  40. "data-user": id,
  41. class: "u-url mention",
  42. href: user_url,
  43. rel: "ugc"
  44. ),
  45. class: "h-card"
  46. )
  47. |> Phoenix.HTML.safe_to_string()
  48. {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
  49. _ ->
  50. {buffer, acc}
  51. end
  52. end
  53. def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
  54. tag = String.downcase(tag)
  55. url = "#{Pleroma.Web.Endpoint.url()}/tag/#{tag}"
  56. link =
  57. Phoenix.HTML.Tag.content_tag(:a, tag_text,
  58. class: "hashtag",
  59. "data-tag": tag,
  60. href: url,
  61. rel: "tag ugc"
  62. )
  63. |> Phoenix.HTML.safe_to_string()
  64. {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
  65. end
  66. @doc """
  67. Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
  68. If the 'safe_mention' option is given, only consecutive mentions at the start the post are actually mentioned.
  69. """
  70. @spec linkify(String.t(), keyword()) ::
  71. {String.t(), [{String.t(), User.t()}], [{String.t(), String.t()}]}
  72. def linkify(text, options \\ []) do
  73. options = linkify_opts() ++ options
  74. if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
  75. %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
  76. acc = %{mentions: MapSet.new(), tags: MapSet.new()}
  77. {text_mentions, %{mentions: mentions}} = Linkify.link_map(mentions, acc, options)
  78. {text_rest, %{tags: tags}} = Linkify.link_map(rest, acc, options)
  79. {text_mentions <> text_rest, MapSet.to_list(mentions), MapSet.to_list(tags)}
  80. else
  81. acc = %{mentions: MapSet.new(), tags: MapSet.new()}
  82. {text, %{mentions: mentions, tags: tags}} = Linkify.link_map(text, acc, options)
  83. {text, MapSet.to_list(mentions), MapSet.to_list(tags)}
  84. end
  85. end
  86. @doc """
  87. Escapes a special characters in mention names.
  88. """
  89. def mentions_escape(text, options \\ []) do
  90. options =
  91. Keyword.merge(options,
  92. mention: true,
  93. url: false,
  94. mention_handler: &Pleroma.Formatter.escape_mention_handler/4
  95. )
  96. if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
  97. %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
  98. Linkify.link(mentions, options) <> Linkify.link(rest, options)
  99. else
  100. Linkify.link(text, options)
  101. end
  102. end
  103. def markdown_to_html(text) do
  104. Earmark.as_html!(text, %Earmark.Options{compact_output: true})
  105. end
  106. def html_escape({text, mentions, hashtags}, type) do
  107. {html_escape(text, type), mentions, hashtags}
  108. end
  109. def html_escape(text, "text/html") do
  110. HTML.filter_tags(text)
  111. end
  112. def html_escape(text, "text/plain") do
  113. Regex.split(@link_regex, text, include_captures: true)
  114. |> Enum.map_every(2, fn chunk ->
  115. {:safe, part} = Phoenix.HTML.html_escape(chunk)
  116. part
  117. end)
  118. |> Enum.join("")
  119. end
  120. def truncate(text, max_length \\ 200, omission \\ "...") do
  121. # Remove trailing whitespace
  122. text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
  123. if String.length(text) < max_length do
  124. text
  125. else
  126. length_with_omission = max_length - String.length(omission)
  127. String.slice(text, 0, length_with_omission) <> omission
  128. end
  129. end
  130. defp get_nickname_text(nickname, %{mentions_format: :full}), do: User.full_nickname(nickname)
  131. defp get_nickname_text(nickname, _), do: User.local_nickname(nickname)
  132. end