Replace links with HTML <a> tags when messages have a formatted_body and in all other cases
This makes (https) links show up as actual links when they're in a message with a `formatted_body`. For example, the pursuivant messages in any KDE channel (like #kirigami), or links which are in a reply to another message/image. Also corrected a regex mistake in another place using the same link replacement regex. Fix #331
This commit is contained in:
@@ -15,7 +15,24 @@ TextEdit {
|
|||||||
readonly property var hasSpoiler: /data-mx-spoiler/g
|
readonly property var hasSpoiler: /data-mx-spoiler/g
|
||||||
|
|
||||||
property bool isEmote: false
|
property bool isEmote: false
|
||||||
property string textMessage: model.display
|
|
||||||
|
/* Turn all links which aren't already in <a> tags into <a> hyperlinks */
|
||||||
|
readonly property var linkRegex: /(href=["'])?(\b(https?):\/\/[^\s\<\>\"\'\\]+)/g
|
||||||
|
property string textMessage: model.display.includes("http")
|
||||||
|
? model.display.replace(linkRegex, function() {
|
||||||
|
if (arguments[1]) {
|
||||||
|
return arguments[0];
|
||||||
|
} else {
|
||||||
|
var l = arguments[2];
|
||||||
|
if ([".", ","].includes(l[l.length-1])) {
|
||||||
|
var link = l.substring(0, l.length-1);
|
||||||
|
var leftover = l[l.length-1];
|
||||||
|
return "<a href=\"" + link + "\">" + link + "</a>" + leftover;
|
||||||
|
}
|
||||||
|
return "<a href=\"" + l + "\">" + l + "</a>";
|
||||||
|
}
|
||||||
|
})
|
||||||
|
: model.display
|
||||||
property bool spoilerRevealed: !hasSpoiler.test(textMessage)
|
property bool spoilerRevealed: !hasSpoiler.test(textMessage)
|
||||||
|
|
||||||
ListView.onReused: Qt.binding(() => !hasSpoiler.test(textMessage))
|
ListView.onReused: Qt.binding(() => !hasSpoiler.test(textMessage))
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ Kirigami.OverlayDrawer {
|
|||||||
TextEdit {
|
TextEdit {
|
||||||
Layout.fillWidth: true
|
Layout.fillWidth: true
|
||||||
text: room && room.topic ? room.topic.replace(replaceLinks, "<a href=\"$1\">$1</a>") : i18n("No Topic")
|
text: room && room.topic ? room.topic.replace(replaceLinks, "<a href=\"$1\">$1</a>") : i18n("No Topic")
|
||||||
readonly property var replaceLinks: /\(https:\/\/[^ ]*\)/
|
readonly property var replaceLinks: /(https:\/\/[^ ]*)/
|
||||||
textFormat: TextEdit.MarkdownText
|
textFormat: TextEdit.MarkdownText
|
||||||
wrapMode: Text.WordWrap
|
wrapMode: Text.WordWrap
|
||||||
selectByMouse: true
|
selectByMouse: true
|
||||||
|
|||||||
Reference in New Issue
Block a user