From 044787e284b6d5d3c0fb0f96a10416664178c9d6 Mon Sep 17 00:00:00 2001 From: hiimgoodpack Date: Thu, 31 Dec 2020 16:27:05 -0500 Subject: [PATCH] Partial markdown support --- README.md | 10 ++++++- index.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 885f008..38e7520 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,12 @@ node index.js ## Chat commands | Name | Description | | :------: | :---------------------------------------------- | -| /leave | Leaves the channel without telling the server | \ No newline at end of file +| /leave | Leaves the channel without telling the server | +| /newline | Any occurance of /newline becomes a new line | + + +# Tests +## Markdown +``` +__Markdown demo:__/newline**Bold and *italics in bold text* and back to bold** and some regular text/newline/newline> I found this software to be extremely helpful!/newline> **Just look at this quote on what this software does:/newline> > This software does something!/newline> > Just look at what this person said:/newline> > > I found this software to be extremely helpful!/newline> > > **Just look at this quote on what the software does:/newline> > > > *Maximum call stack size exceeded*/newline> > > I don't know what else could have been better!**/newline> > - Some random person/newline> I don't know what else could have been better!**/newline- Some random person/newline/newlineTry the software today!/newline +``` diff --git a/index.js b/index.js index eb4f788..4a92c64 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,90 @@ const hexToRGB = (hex) => { return [(decimal & 0xFF0000) >> 16, (decimal & 0xFF00) >> 8, decimal & 0xFF]; }; +const format = { + bold: "\x1b[1m", + noBold: "\x1b[22m", + + italics: "\x1b[3m", + noItalics: "\x1b[23m", + + quote: "\x1b[7m\x1b[2m", + noQuote: "\x1b[27m\x1b[22m", + + underline: "\x1b[4m", + noUnderline: "\x1b[24m" +}; +const toState = (state) => { + let result = ""; + result += format.noQuote; + result += state.bold ? format.bold : format.noBold; + result += state.italics ? format.italics : format.noItalics; + result += state.underline ? format.underline : format.noUnderline; + return result; +}; +const toMarkdown = (text) => { + const defaultState = { + bold: false, + italics: false, + underline: false + }; + + let states = [Object.assign({}, defaultState)]; + let result = ""; + let quoteDepth = 0; + for (let i = 0; i < text.length; i++) { + let state = states[quoteDepth]; + switch (text[i]) { + case "*": { + // If bold + if (text[i+1] === "*") { + state.bold = !state.bold; + result += state.bold ? format.bold : format.noBold; + i++; + } else { + state.italics = !state.italics; + result += state.italics ? format.italics : format.noItalics; + } + break; + } + case ">": { + if (text[i+1] === " ") { + quoteDepth++; + result += toState(defaultState); + + if (!states[quoteDepth]) + states[quoteDepth] = Object.assign({}, defaultState); + result += (quoteDepth%2==1) ? format.quote : format.noQuote; + result += " | "; + result += toState(states[quoteDepth]); + result += (quoteDepth%2==1) ? format.quote : format.noQuote; + i++; + } + break; + } + case "_": { + if (text[i+1] === "_") { + state.underline = !state.underline; + result += state.underline ? format.underline : format.noUnderline; + i++; + break; + } + } + case "\n": { + if (quoteDepth != 0) { + quoteDepth = 0; + result += toState(states[0]); + } + } + default: { + result += text[i]; + break; + } + } + } + return result; +}; + let user = {}; console.log(`Unofficial brainlet command-line interface @@ -223,7 +307,7 @@ To leave this category, use '/quit'`); const PS1 = `${getColorCode(hexToRGB(user.color))}${user.name}${resetColorCode} [message currently not sent]: `; socket.onMessage((message) => { if (message.category._id === categoryID) { - process.stdout.write(`\r${clearLine}${getColorCode(hexToRGB(message.author.color))}${message.author.username}${resetColorCode}: ${message.content}\n${PS1}`); + process.stdout.write(`\r${clearLine}${getColorCode(hexToRGB(message.author.color))}${message.author.username}${resetColorCode}: ${toMarkdown(message.content)}\n${PS1}`); } }); let sendMessages = true; @@ -241,7 +325,7 @@ To leave this category, use '/quit'`); break; } - socket.send(categoryID, message); + socket.send(categoryID, message.replaceAll("/newline", "\n")); } } }