Skip to content

Instantly share code, notes, and snippets.

@jerbear2008
Last active January 7, 2022 22:47
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 jerbear2008/9cab84e876d19dc92760134c7cb79a2d to your computer and use it in GitHub Desktop.
Save jerbear2008/9cab84e876d19dc92760134c7cb79a2d to your computer and use it in GitHub Desktop.
Discord Text Formatter
let text = `\
i italic
b bold
r red
I codeblock italic
\\i literal
plain text
ciuS formatting test`
let formats = {
inline: {
"i": '*S*',
"b": '**S**',
"u": '__S__',
"s": '~~S~~',
"c": '`S`',
"q": '> S',
"S": '||S||'
},
codeblock: {
"r": '```diff\n- S\n```',
"o": '```cs\n# S\n```',
"y": '```fix\n S\n```',
"g": '```diff\n+ S\n```',
"B": '```md\n# S\n```',
"w": '```\n S\n```',
"k": '```md\n> S\n```',
"I": '```asciidoc\n\'⠀S \'\n```',
}
}
let lines = text.split('\n')
let output = ''
lines.forEach(line => {
if (line.startsWith('\\')) {
output += '\n' + line.substring(1)
return
}
let formattingCode = line.split(' ')[0]
if (formattingCode.length === 1) {
if (formats.inline[formattingCode]) {
let formattedLine = formats.inline[formattingCode].split('S').join(line.substring(2))
output += '\n' + formattedLine
return
}
if (formats.codeblock[formattingCode]) {
let formattedLine = formats.codeblock[formattingCode].split('S').join(line.substring(2))
output += formattedLine
return
}
}
processMultiCode: {
let formattedLine = line.substring(formattingCode.length + 1)
let formattingCodes = formattingCode.split('')
let valid = true
formattingCodes.forEach(code => {
console.log(1, formattedLine, code, line)
if (!formats.inline[code]) {
valid = false
return
}
formattedLine = formats.inline[code].split('S').join(formattedLine)
console.log(2, formattedLine, code, line)
})
if (!valid) break processMultiCode
output += '\n' + formattedLine
return
}
output += '\n' + line
})
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment