Skip to content

Commit 9643429

Browse files
committed
link-dialog: allow relative file linking
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 300b4ae commit 9643429

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/dialogs/linkdialog.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QRegularExpression>
1616
#include <QTimer>
1717

18+
#include "entities/notefolder.h"
1819
#include "services/settingsservice.h"
1920
#include "ui_linkdialog.h"
2021
#include "widgets/navigationwidget.h"
@@ -125,7 +126,8 @@ QString LinkDialog::getSelectedHeading() const {
125126
QString LinkDialog::getURL() const {
126127
QString url = ui->urlEdit->text().trimmed();
127128

128-
if (!url.isEmpty() && !url.contains(QStringLiteral("://"))) {
129+
if (!url.isEmpty() && !url.contains(QStringLiteral("://")) &&
130+
!url.startsWith(QStringLiteral("./"))) {
129131
url = QStringLiteral("http://") + url;
130132
}
131133

@@ -317,6 +319,20 @@ void LinkDialog::addFileUrl() {
317319
// store url for the next time
318320
settings.setValue(QStringLiteral("LinkDialog/lastSelectedFileUrl"), fileUrlString);
319321

322+
// Check if the url is a file url and relative to the current note folder, if yes, convert
323+
// it to relative
324+
if (QUrl(fileUrlString).isLocalFile()) {
325+
QUrl fileUrl(fileUrlString);
326+
QString noteFolderPath = NoteFolder::currentLocalPath();
327+
QString filePath = fileUrl.toLocalFile();
328+
if (filePath.startsWith(noteFolderPath)) {
329+
fileUrlString =
330+
QStringLiteral("./") + QDir(noteFolderPath).relativeFilePath(filePath);
331+
}
332+
}
333+
334+
qDebug() << __func__ << " - 'fileUrlString': " << fileUrlString;
335+
320336
// write the file-url to the url text-edit
321337
ui->urlEdit->setText(fileUrlString);
322338
}

0 commit comments

Comments
 (0)