Merge branch 'develop' into oembed_provider
[akkoma] / lib / pleroma / formatter.ex
index 46d0d926a814b2177c4aeefff7cc79e37b8f74e6..8024d97c2dc716454592cd5fc8e739d1962f4765 100644 (file)
@@ -177,4 +177,22 @@ defmodule Pleroma.Formatter do
       String.replace(result_text, uuid, replacement)
     end)
   end
+
+  def truncate(text, opts \\ []) do
+    max_length = opts[:max_length] || 200
+    omission = opts[:omission] || "..."
+
+    cond do
+      not String.valid?(text) ->
+        text
+
+      String.length(text) < max_length ->
+        text
+
+      true ->
+        length_with_omission = max_length - String.length(omission)
+
+        "#{String.slice(text, 0, length_with_omission)}#{omission}"
+    end
+  end
 end