Fix custom emojis

Don't turn them into a tags, and don't generate previews of them.

BUG: 460178

BUG: 460179
This commit is contained in:
Bharadwaj Raju
2022-10-11 18:31:06 +00:00
committed by Tobias Fella
parent 9398c5004c
commit 61f1e2481b
2 changed files with 37 additions and 11 deletions

View File

@@ -11,9 +11,22 @@ import org.kde.neochat 1.0
RowLayout { RowLayout {
id: row id: row
readonly property var customEmojiLinksRegex: /data-mx-emoticon="" src="(\bhttps?:\/\/[^\s\<\>\"\']*[^\s\<\>\"\'])/g
readonly property var customEmojiLinks: {
let links = [];
// we need all this because QML JS doesn't support String.matchAll introduced in ECMAScript 2020
let match = customEmojiLinksRegex.exec(model.display);
while (match !== null) {
links.push(match[1])
match = customEmojiLinksRegex.exec(model.display);
}
return links;
}
property var links: model.display.match(/(\bhttps?:\/\/[^\s\<\>\"\']*[^\s\<\>\"\'])/g) property var links: model.display.match(/(\bhttps?:\/\/[^\s\<\>\"\']*[^\s\<\>\"\'])/g)
// don't show previews for room links or user mentions // don't show previews for room links or user mentions or custom emojis
.filter(link => !link.includes("https://matrix.to")) .filter(link => !(
link.includes("https://matrix.to") || (customEmojiLinks && customEmojiLinks.includes(link))
))
// remove ending fullstops and commas // remove ending fullstops and commas
.map(link => (link.length && [".", ","].includes(link[link.length-1])) ? link.substring(0, link.length-1) : link) .map(link => (link.length && [".", ","].includes(link[link.length-1])) ? link.substring(0, link.length-1) : link)
LinkPreviewer { LinkPreviewer {

View File

@@ -17,20 +17,33 @@ TextEdit {
property bool isEmote: false property bool isEmote: false
/* Turn all links which aren't already in <a> tags into <a> hyperlinks */ /* Turn all links which aren't already in <a> tags into <a> hyperlinks */
readonly property var customEmojiLinksRegex: /data-mx-emoticon="" src="(\bhttps?:\/\/[^\s\<\>\"\']*[^\s\<\>\"\'])/g
readonly property var customEmojiLinks: {
let links = [];
// we need all this because QML JS doesn't support String.matchAll introduced in ECMAScript 2020
let match = customEmojiLinksRegex.exec(model.display);
while (match !== null) {
links.push(match[1])
match = customEmojiLinksRegex.exec(model.display);
}
return links;
}
readonly property var linkRegex: /(href=["'])?(\b(https?):\/\/[^\s\<\>\"\'\\]+)/g readonly property var linkRegex: /(href=["'])?(\b(https?):\/\/[^\s\<\>\"\'\\]+)/g
property string textMessage: model.display.includes("http") property string textMessage: model.display.includes("http")
? model.display.replace(linkRegex, function() { ? model.display.replace(linkRegex, function() {
if (customEmojiLinks && customEmojiLinks.includes(arguments[0])) {
return arguments[0];
}
if (arguments[1]) { if (arguments[1]) {
return arguments[0]; return arguments[0];
} else { }
var l = arguments[2]; const l = arguments[2];
if ([".", ","].includes(l[l.length-1])) { if ([".", ","].includes(l[l.length-1])) {
var link = l.substring(0, l.length-1); const link = l.substring(0, l.length-1);
var leftover = l[l.length-1]; const leftover = l[l.length-1];
return "<a href=\"" + link + "\">" + link + "</a>" + leftover; return `<a href="${link}">${link}</a>${leftover}`;
} }
return "<a href=\"" + l + "\">" + l + "</a>"; return `<a href="${l}">${l}</a>`;
}
}) })
: model.display : model.display
property bool spoilerRevealed: !hasSpoiler.test(textMessage) property bool spoilerRevealed: !hasSpoiler.test(textMessage)