Markdown: Fix that file links were shown as blank in preview

Handle links ourselves since QTextBrowser doesn't do what we want.

- anchors without file path just jump to anchor
- local files (potentially relative to the markdown file) open in
  Qt Creator (anchors ignored)
- otherwise QDesktopServices is used

Fixes: QTCREATORBUG-30120
Change-Id: I9a68607a0b32255ec075c698a1265cc6d1387e0c
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2024-01-03 16:21:55 +01:00
parent a0cc2e3c3e
commit 298bd125c0

View File

@@ -20,6 +20,7 @@
#include <utils/stringutils.h>
#include <utils/utilsicons.h>
#include <QDesktopServices>
#include <QHBoxLayout>
#include <QScrollBar>
#include <QTextBrowser>
@@ -70,9 +71,22 @@ public:
// preview
m_previewWidget = new QTextBrowser();
m_previewWidget->setOpenExternalLinks(true);
m_previewWidget->setOpenLinks(false); // we want to open files in QtC, not the browser
m_previewWidget->setFrameShape(QFrame::NoFrame);
new Utils::MarkdownHighlighter(m_previewWidget->document());
connect(m_previewWidget, &QTextBrowser::anchorClicked, this, [this](const QUrl &link) {
if (link.hasFragment() && link.path().isEmpty() && link.scheme().isEmpty()) {
// local anchor
m_previewWidget->scrollToAnchor(link.fragment(QUrl::FullyEncoded));
} else if (link.isLocalFile() || link.scheme().isEmpty()) {
// absolute path or relative (to the document)
// open in Qt Creator
EditorManager::openEditor(
document()->filePath().parentDir().resolvePath(link.path()));
} else {
QDesktopServices::openUrl(link);
}
});
// editor
m_textEditorWidget = new TextEditorWidget;