blob: b163da856ab511a7b0a238c77e9f63bf72155374 [file] [log] [blame]
TheCodedProfda197f22023-03-01 18:15:51 -05001import Styles from "../../styles/Components/transcripts.module.css"
2import Image from "next/image";
TheCodedProf2a69bd92023-03-02 16:36:32 -05003import Showdown from "showdown";
4import Axios from "axios";
5import { useEffect, useState } from "react";
TheCodedProfc86e44b2023-02-28 17:30:57 -05006
TheCodedProf2a69bd92023-03-02 16:36:32 -05007const bold = /(\*\*.+\*\*)/g;
8const italic = /(\*.+\*|\_.+\_)/g;
9const emoji = /(<a?:.+:\D+>)/g;
10const user = /(<@!?\d+>)/g;
11const underline = /(\_\_.+\_\_)/g;
12const strikethrough = /(\~\~.+\~\~)/g;
13const code = /(\`.+\`)/g;
14const codeBlock = /(\`\`\`.+\`\`\`)/g;
15const spoiler = /(\`\`\`.+\`\`\`)/g;
16
17const regex = /(\*\*\*.+\*\*\*|\*\*.+\*\*|\*.+\*|\_.+\_|\_\_.+\_\_|\~\~.+\~\~|\`.+\`|\`\`\`.+\`\`\`|\`\`\`.+\`\`\`|<a?:.+:\D+>|<@!?\d+>)/g;
18
19const converter = new Showdown.Converter();
20
21async function parse(text) {
22 const splitText = text.split(regex);
23 console.log(splitText)
24 return await splitText.map( async (item, index) => {
25 if (item.match(bold)) {
26 return <b key={index}>{item.replaceAll(/\*\*/g, '')}</b>
27 }
28 if (item.match(underline)) {
29 return <u key={index}>{item.replaceAll(/\_\_/g, '')}</u>
30 }
31 if (item.match(italic)) {
32 return <i key={index}>{item.replaceAll(/\*/g, '')}</i>
33 }
34 if (item.match(code)) {
35 return <code key={index}>{item.replaceAll(/\`/g, '')}</code>
36 }
37 if (item.match(emoji)) {
38 return <Image key={index} src={`https://cdn.discord.com/emojis/${item.replaceAll(/\D/g, '')}`} width={20} height={20} alt="" />
39 }
40 if (item.match(user)) {
41 const username = (await Axios.get(`http://localhost:10000/users/${item.replaceAll(/\D/g, '')}`)).data;
42 console.log(username)
43 return <>{username}</>
44
45 }
46 return item
47 })
48
49
50}
51
52function Embed(props) {
TheCodedProfc86e44b2023-02-28 17:30:57 -050053
TheCodedProfda197f22023-03-01 18:15:51 -050054 let description = props.embed.description;
55 if (description) {
56 description = description.split("\n");
57 }
58
TheCodedProf2a69bd92023-03-02 16:36:32 -050059 const [newDesc, setNewDesc] = useState([]);
60
61 useEffect(() => {
62 async function stuff() {
63 const parsed = await parse(props.embed.description);
64 setNewDesc(newDesc => [...newDesc, parsed]);
65 }
66 stuff();
67 })
68
69 return (
TheCodedProfda197f22023-03-01 18:15:51 -050070 <div className={Styles.embed} style={{borderColor: props.color ?? "#F27878"}}>
71 {
72 props.embed.author ?
73 <div className={Styles.embedAuthor}>
74 {
75 props.embed.author.iconURL ?
76 <Image src={props.embed.author.iconURL} alt=""/> :
77 null
78 }
79 {
80 props.embed.author.url ?
81 <a href={props.embed.author.url}>
82 {props.embed.author?.name ?? ""}
83 </a> :
84 <p>{props.embed.author?.name ?? ""}</p>
85 }
86 </div> :
87 null
88 }
89 {
90 props.embed.title ?
91 <div className={Styles.embedTitle}>{props.embed.title}</div> :
92 null
93 }
94 {
95 props.embed.description ?
96 <div className={Styles.embedDescription}>
97 {
TheCodedProf2a69bd92023-03-02 16:36:32 -050098 newDesc.map((item) => {
99 return item;
TheCodedProfda197f22023-03-01 18:15:51 -0500100 })
101 }
102 </div>
103 : null
104 }
105 </div>
106 )
TheCodedProfc86e44b2023-02-28 17:30:57 -0500107}
108
TheCodedProf2a69bd92023-03-02 16:36:32 -0500109export default Embed;