editor: support prepend_dot_in_relative_path config

This commit is contained in:
Le Tan 2018-12-03 19:51:04 +08:00
parent 369ccb34e8
commit 64960cc3c5
7 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -1885,3 +1885,18 @@ QString VUtils::escapeHtml(QString p_text)
p_text.replace(">", "&gt;").replace("<", "&lt;").replace("&", "&amp;");
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("./");
}
}

View File

@ -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):

View File

@ -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()

View File

@ -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

View File

@ -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) {

View File

@ -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"),
"",