From 0496f20ae22f920363f469563e0aa148abc31327 Mon Sep 17 00:00:00 2001 From: Mathias Hasselmann Date: Fri, 5 Jan 2024 00:31:53 +0100 Subject: [PATCH] Markdown: Open external links from text editor Fixes: QTCREATORBUG-30119 Change-Id: I77ab4b8e030db32b08ed3a8a39562b1943a65dcc Reviewed-by: David Schulz --- src/plugins/texteditor/markdowneditor.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/markdowneditor.cpp b/src/plugins/texteditor/markdowneditor.cpp index ca8088d3264..b574a76074f 100644 --- a/src/plugins/texteditor/markdowneditor.cpp +++ b/src/plugins/texteditor/markdowneditor.cpp @@ -570,10 +570,12 @@ void MarkdownEditorWidget::findLinkAt(const QTextCursor &cursor, { static const QStringView CAPTURE_GROUP_LINK = u"link"; static const QStringView CAPTURE_GROUP_ANCHOR = u"anchor"; + static const QStringView CAPTURE_GROUP_RAWURL = u"rawurl"; static const QRegularExpression markdownLink{ R"(\[[^[\]]*\]\((?.+?)\))" R"(|(?\[\^[^\]]+\])(?:[^:]|$))" + R"(|(?(?:https?|ftp)\://[^">)\s]+))" }; QTC_ASSERT(markdownLink.isValid(), return); @@ -597,11 +599,13 @@ void MarkdownEditorWidget::findLinkAt(const QTextCursor &cursor, return textDocument()->filePath().parentDir().resolvePath(url.path()); else if (url.isLocalFile()) return FilePath::fromString(url.toLocalFile()); + else if (!url.scheme().isEmpty()) + return FilePath::fromString(url.toString()); else return FilePath{}; }(); - if (!linkedPath.isFile()) + if (!linkedPath.isFile() && url.scheme().isEmpty()) continue; Link result = linkedPath; @@ -628,6 +632,15 @@ void MarkdownEditorWidget::findLinkAt(const QTextCursor &cursor, result.linkTextEnd = match.capturedEnd(CAPTURE_GROUP_ANCHOR) + blockOffset; processLinkCallback(result); break; + } else if (const QStringView rawurl = match.capturedView(CAPTURE_GROUP_RAWURL); + !rawurl.isEmpty()) { + // Process raw links starting with "http://", "https://", or "ftp://". + + Link result{FilePath::fromString(rawurl.toString())}; + result.linkTextStart = match.capturedStart() + blockOffset; + result.linkTextEnd = match.capturedEnd() + blockOffset; + processLinkCallback(result); + break; } else { QTC_ASSERT_STRING("This line should not be reached unless 'markdownLink' is wrong"); return;