markdown + reactons + emoji picker (needs bug fixes everywhere)
This commit is contained in:
44
utils/parseMessageBody.ts
Normal file
44
utils/parseMessageBody.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { IChannel } from "~/types";
|
||||
|
||||
export default function parseBody(body: string, activeChannel: IChannel) {
|
||||
body = escape(body);
|
||||
const rules = [
|
||||
//bold, italics and paragragh rules
|
||||
[/\*\*\s?([^\n]+)\*\*/g, "<b>$1</b>"],
|
||||
[/\*\s?([^\n]+)\*/g, "<i>$1</i>"],
|
||||
[/\_\_\s?([^\n]+)\_\_/g, "<u>$1</u>"],
|
||||
[/\~\~\s?([^\n]+)\~\~/g, "<s>$1</s>"],
|
||||
|
||||
// code lines and blocks
|
||||
[/```(.+?)```/g, "<pre class='codeblock'><code>$1</code></pre>"],
|
||||
[/(?<!`)`(.+?)`(?!`)/g, "<code class='inline-code'>$1</code>"],
|
||||
];
|
||||
|
||||
rules.forEach(([rule, template]) => {
|
||||
body = body.replace(rule, template);
|
||||
})
|
||||
|
||||
const mentions = body.match(/<@([a-z]|[0-9]){25}>/g);
|
||||
|
||||
if (mentions) {
|
||||
const participants = (activeChannel.DM) ? activeChannel.dmParticipants : activeChannel.server.participants;
|
||||
if (!participants) throw new Error(`participants in channel "${activeChannel.id}" not found"`)
|
||||
mentions.forEach((e: string) => {
|
||||
if (!e) return
|
||||
const id = e.split('<@')[1]?.split('>')[0];
|
||||
if (!id) return;
|
||||
const user = participants.find((e) => e.id === id)
|
||||
if (!user) return;
|
||||
body = body.split(e).join(`@${user.username}`)
|
||||
});
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
function escape(s: string) {
|
||||
return s.replace(
|
||||
/[^0-9A-Za-z ]/g,
|
||||
c => "&#" + c.charCodeAt(0) + ";"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user