diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index ded0fdfd..cc45d6ec 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -545,3 +545,6 @@ ParseAndPaste=P [markdown] ; Enable WaveDrom enable_wavedrom=false + +; Prepend a dot in relative path of images and attachments +prepend_dot_in_relative_path=false diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index c9d6bc19..1243668f 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -1885,3 +1885,18 @@ QString VUtils::escapeHtml(QString p_text) p_text.replace(">", ">").replace("<", "<").replace("&", "&"); return p_text; } + +QString VUtils::encodeSpacesInPath(const QString &p_path) +{ + QString tmp(p_path); + tmp.replace(' ', "%20"); + return tmp; +} + +void VUtils::prependDotIfRelative(QString &p_path) +{ + if (QFileInfo(p_path).isRelative() + && !p_path.startsWith("../") && !p_path.startsWith("./")) { + p_path.prepend("./"); + } +} diff --git a/src/utils/vutils.h b/src/utils/vutils.h index 6b459dd6..3c457646 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -394,6 +394,10 @@ public: static QString escapeHtml(QString p_text); + static QString encodeSpacesInPath(const QString &p_path); + + static void prependDotIfRelative(QString &p_path); + // Regular expression for image link. // ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100) // Captured texts (need to be trimmed): diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 45b88fa6..e0050dc5 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -377,6 +377,8 @@ void VConfigManager::initMarkdownConfigs() { const QString section("markdown"); m_enableWavedrom = getConfigFromSettings(section, "enable_wavedrom").toBool(); + + m_prependDotInRelativePath = getConfigFromSettings(section, "prepend_dot_in_relative_path").toBool(); } void VConfigManager::initSettings() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 0a853fd9..8a561397 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -625,6 +625,9 @@ public: QString getImageBrowsePath() const; void setImageBrowsePath(const QString &p_path); + bool getPrependDotInRelativePath() const; + void setPrependDotInRelativePath(bool p_enabled); + private: void initEditorConfigs(); @@ -1106,6 +1109,9 @@ private: // Editor font family to override the value set by the style. QString m_editorFontFamily; + // Whether prepend a dot in the relative path of images and attachments. + bool m_prependDotInRelativePath; + // The name of the config file in each directory. static const QString c_dirConfigFile; @@ -2877,4 +2883,19 @@ inline void VConfigManager::setImageBrowsePath(const QString &p_path) { setConfigToSessionSettings("global", "image_browse_path", p_path); } + +inline bool VConfigManager::getPrependDotInRelativePath() const +{ + return m_prependDotInRelativePath; +} + +inline void VConfigManager::setPrependDotInRelativePath(bool p_enabled) +{ + if (m_prependDotInRelativePath == p_enabled) { + return; + } + + m_prependDotInRelativePath = p_enabled; + setConfigToSettings("markdown", "prepend_dot_in_relative_path", m_prependDotInRelativePath); +} #endif // VCONFIGMANAGER_H diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index d8e36eb0..1206dd33 100644 --- a/src/vmdeditoperations.cpp +++ b/src/vmdeditoperations.cpp @@ -114,6 +114,11 @@ void VMdEditOperations::insertImageFromQImage(const QString &p_title, } QString url = QDir::fromNativeSeparators(QString("%1/%2").arg(p_folderInLink).arg(fileName)); + url = VUtils::encodeSpacesInPath(url); + if (g_config->getPrependDotInRelativePath()) { + VUtils::prependDotIfRelative(url); + } + insertText(imageLink(p_title, url, p_width, p_height)); qDebug() << "insert image" << p_title << filePath; @@ -195,6 +200,11 @@ void VMdEditOperations::insertImageFromPath(const QString &p_title, } p_urlInLink = QDir::fromNativeSeparators(QString("%1/%2").arg(p_folderInLink).arg(fileName)); + p_urlInLink = VUtils::encodeSpacesInPath(p_urlInLink); + if (g_config->getPrependDotInRelativePath()) { + VUtils::prependDotIfRelative(p_urlInLink); + } + p_destImagePath = filePath; if (p_insertText) { diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 3b31143f..e7baf341 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -1947,6 +1947,9 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source) QDir dir(m_file->fetchBasePath()); ut = dir.relativeFilePath(url.toLocalFile()); ut = QUrl(ut).toString(QUrl::EncodeSpaces); + if (g_config->getPrependDotInRelativePath()) { + VUtils::prependDotIfRelative(ut); + } } else { ut = url.isLocalFile() ? url.toString(QUrl::EncodeSpaces) : url.toString(); @@ -2005,6 +2008,9 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source) QDir dir(m_file->fetchBasePath()); ut = dir.relativeFilePath(url.toLocalFile()); ut = QUrl(ut).toString(QUrl::EncodeSpaces); + if (g_config->getPrependDotInRelativePath()) { + VUtils::prependDotIfRelative(ut); + } } else { ut = url.isLocalFile() ? url.toString(QUrl::EncodeSpaces) : url.toString(); @@ -2214,6 +2220,9 @@ void VMdEditor::handleLinkToAttachmentAction(QAction *p_act) QDir dir(note->fetchBasePath()); QString ut = dir.relativeFilePath(filePath); ut = QUrl(ut).toString(QUrl::EncodeSpaces); + if (g_config->getPrependDotInRelativePath()) { + VUtils::prependDotIfRelative(ut); + } VInsertLinkDialog ld(QObject::tr("Insert Link"), "",