From 2162b2c1e3772cbbc55e9b2fd3a1fd3cf187a758 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Sat, 13 Jul 2024 09:32:34 +0200 Subject: [PATCH] Add emoji support & render markdown in embeds --- webpage/embed.ts | 13 +++++++------ webpage/markdown.ts | 36 ++++++++++++++++++++++++++++++++++++ webpage/settings.ts | 2 +- webpage/style.css | 4 +--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/webpage/embed.ts b/webpage/embed.ts index b9a4886..c4f05c1 100644 --- a/webpage/embed.ts +++ b/webpage/embed.ts @@ -1,5 +1,7 @@ import {Fullscreen} from "./fullscreen.js"; import {Message} from "./message.js"; +import {markdown} from "./markdown.js"; + class Embed{ type:string; owner:Message; @@ -48,7 +50,7 @@ class Embed{ authorline.append(img); } const a=document.createElement("a"); - a.innerText=this.json.author.name + a.textContent=this.json.author.name if(this.json.author.url){ a.href=this.json.author.url } @@ -57,7 +59,7 @@ class Embed{ embed.append(authorline); } const title=document.createElement("a"); - title.textContent=this.json.title; + title.innerHTML=markdown(this.json.title).innerHTML; if(this.json.url){ title.href=this.json.url; } @@ -66,7 +68,7 @@ class Embed{ if(this.json.description){ const p=document.createElement("p"); - p.textContent=this.json.description; + p.innerHTML=markdown(this.json.description).innerHTML; embed.append(p); } @@ -77,9 +79,8 @@ class Embed{ const b=document.createElement("b"); b.textContent=thing.name; div.append(b); - let p; - p=document.createElement("p") - p.textContent=thing.value; + const p=document.createElement("p") + p.innerHTML=markdown(thing.value).innerHTML; p.classList.add("embedp"); div.append(p); diff --git a/webpage/markdown.ts b/webpage/markdown.ts index bc0eac7..572fe24 100644 --- a/webpage/markdown.ts +++ b/webpage/markdown.ts @@ -360,6 +360,42 @@ function markdown(text : string|string[],{keep=false,stdsize=false} = {}){ } } + if (txt[i] == "<" && txt[i + 1] == ":") { + let found=false; + const build=["<", ":"]; + let j=i + 2; + for (; txt[j] !== void 0; j++) { + build.push(txt[j]); + + if (txt[j] == ">") { + found=true; + break; + } + } + + if (found) { + const parts=build.join("").match(/^<:\w+:(\d{10,30})>$/); + if (parts && parts[1]) { + appendcurrent(); + i=j; + + const isEmojiOnly = txt.join("").trim()===build.join("").trim(); + + const emojiElem=document.createElement("img"); + emojiElem.classList.add("md-emoji"); + emojiElem.width=isEmojiOnly ? 48 : 22; + emojiElem.height=isEmojiOnly ? 48 : 22; + emojiElem.crossOrigin="anonymous"; + emojiElem.src=this.info.cdn.toString() + "/emojis/" + parts[1] + ".png?size=32"; + emojiElem.alt=""; + emojiElem.loading="lazy"; + span.appendChild(emojiElem); + + continue; + } + } + } + current.textContent+=txt[i]; } appendcurrent(); diff --git a/webpage/settings.ts b/webpage/settings.ts index 67b3b50..1b2f095 100644 --- a/webpage/settings.ts +++ b/webpage/settings.ts @@ -83,7 +83,7 @@ class PermissionToggle{ div.append(this.generateCheckbox()); const p=document.createElement("p"); - p.innerText=this.rolejson.description; + p.textContent=this.rolejson.description; div.appendChild(p); return div; } diff --git a/webpage/style.css b/webpage/style.css index b92af19..eb154e8 100644 --- a/webpage/style.css +++ b/webpage/style.css @@ -215,8 +215,6 @@ img { vertical-align: middle; max-width: 3in; max-height: 3in; - width: auto; - height: auto; } #page { @@ -1352,4 +1350,4 @@ span { width: 100%; flex-direction: row; max-height:100in; -} \ No newline at end of file +}