Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Created March 17, 2019 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localhostdotdev/1a3c7fcec58521249a8e13f78b76c081 to your computer and use it in GitHub Desktop.
Save localhostdotdev/1a3c7fcec58521249a8e13f78b76c081 to your computer and use it in GitHub Desktop.
Formats your strings for discord messages (with fixes around their buggy markdown) #discord #discordrb #ruby
class Format
ZERO_WIDTH = "​"
def self.i(str)
"_#{str}_"
end
def self.b(str)
"**#{str}**"
end
def self.bi(str)
"***#{str}***"
end
def self.u(str)
"__#{str}__"
end
def self.ui(str)
"__*#{str}*__"
end
def self.ub(str)
"__**#{str}**__"
end
def self.ubi(str)
"__***#{str}***__"
end
def self.strike(str)
"~~#{str}~~"
end
def self.inline(str)
"`#{str.gsub("`", "'")}`"
end
def self.code(str, language: nil)
str = str.gsub("`") { ZERO_WIDTH * 2 + "`" }
"```#{language}\n#{str}\n```"
end
def self.margin(str, margin: 2)
"\n" + str.lines.map { |l| " " * margin + l }.join + "\n" * 2
end
def self.escape(str)
str = str.gsub('\\') { "\\\\" }.gsub("_") { '\\_' }
str = str.gsub("*") { "\\*" }.gsub("`") { ZERO_WIDTH * 2 + "`" }
str.gsub("~") { "\\~" }
end
def self.number(n)
n = n.to_i if n.to_i == n
ActiveSupport::NumberHelper.number_to_delimited(n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment