X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fformatter.ex;h=8024d97c2dc716454592cd5fc8e739d1962f4765;hb=a3003364598b42849e384a216948dad810481f51;hp=46d0d926a814b2177c4aeefff7cc79e37b8f74e6;hpb=cc6689cc203d44e19e9aa14c2ef3a3dad823ef88;p=akkoma diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 46d0d926a..8024d97c2 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -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