Add Image Detection to RMF (Rich Message Formatting) #79
@@ -36,19 +36,27 @@ let dateOptions = {
|
||||
* @returns {String} - Formatted Message (IN HTML)
|
||||
*/
|
||||
function RMF(message) {
|
||||
var IMG_END = [".jpg", ".jpeg", ".png", ".gif", ".webp"]
|
||||
var URLREGEX = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
|
||||
var URLmatch = message.match(URLREGEX);
|
||||
var newMessage = []
|
||||
if (URLmatch) {
|
||||
for (var i = 0; i < URLmatch.length; i++) {
|
||||
var link = (<span>
|
||||
{message.split(URLmatch[i])[0]}
|
||||
<Link href={"https://"+URLmatch[i]} target="_blank" className="hover:underline">{URLmatch[i]}</Link>
|
||||
{message.split(URLmatch[i])[1]}
|
||||
</span>)
|
||||
message = link
|
||||
if (IMG_END.includes(URLmatch[i].slice(-4)) || IMG_END.includes(URLmatch[i].slice(-5))) {
|
||||
// Its a photo
|
||||
newMessage.push((<img src={"https://"+URLmatch[i]} className="max-w-[100%] max-h-[100%]"/>))
|
||||
} else {
|
||||
console.log(message)
|
||||
newMessage.push((<span className="mr-2">
|
||||
{URLmatch.length == 1 && message.split(URLmatch[i])[0]}
|
||||
<Link href={"https://"+URLmatch[i]} target="_blank" className="hover:underline">{URLmatch[i]}</Link>
|
||||
{(i == URLmatch.length || URLmatch.length == 1) && message.split(URLmatch[i])[1]}
|
||||
</span>))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return message
|
||||
return newMessage
|
||||
}
|
||||
/**
|
||||
* Grabs Window Size
|
||||
|
||||
Reference in New Issue
Block a user