Partial markdown support
This commit is contained in:
parent
2e3b40af2d
commit
044787e284
2 changed files with 95 additions and 3 deletions
10
README.md
10
README.md
|
@ -37,4 +37,12 @@ node index.js <server>
|
|||
## Chat commands
|
||||
| Name | Description |
|
||||
| :------: | :---------------------------------------------- |
|
||||
| /leave | Leaves the channel without telling the server |
|
||||
| /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
|
||||
```
|
||||
|
|
88
index.js
88
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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue