forked from qt-creator/qt-creator
Markdown: Open external links from text editor
Fixes: QTCREATORBUG-30119 Change-Id: I77ab4b8e030db32b08ed3a8a39562b1943a65dcc Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -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"(\[[^[\]]*\]\((?<link>.+?)\))"
|
||||
R"(|(?<anchor>\[\^[^\]]+\])(?:[^:]|$))"
|
||||
R"(|(?<rawurl>(?: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;
|
||||
|
||||
Reference in New Issue
Block a user