This Gist was automatically created by Carbide, a free online programming environment.
Last active
January 7, 2022 22:47
-
-
Save jerbear2008/9cab84e876d19dc92760134c7cb79a2d to your computer and use it in GitHub Desktop.
Discord Text Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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