mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
delete local images when deleting markdown file
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
a6e7a7e42d
commit
0306256863
@ -3,6 +3,7 @@
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegExp>
|
||||
|
||||
VUtils::VUtils()
|
||||
{
|
||||
@ -121,3 +122,25 @@ QString VUtils::basePathFromPath(const QString &path)
|
||||
{
|
||||
return QFileInfo(path).path();
|
||||
}
|
||||
|
||||
// Collect image links like 
|
||||
QVector<QString> VUtils::imagesFromMarkdownFile(const QString &filePath)
|
||||
{
|
||||
Q_ASSERT(isMarkdown(filePath));
|
||||
QVector<QString> images;
|
||||
if (filePath.isEmpty()) {
|
||||
return images;
|
||||
}
|
||||
QString basePath = basePathFromPath(filePath);
|
||||
QString text = readFileFromDisk(filePath);
|
||||
QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)");
|
||||
int pos = 0;
|
||||
|
||||
while (pos < text.size() && (pos = regExp.indexIn(text, pos)) != -1) {
|
||||
Q_ASSERT(regExp.captureCount() == 1);
|
||||
qDebug() << regExp.capturedTexts()[0] << regExp.capturedTexts()[1];
|
||||
images.append(QDir(basePath).filePath(regExp.capturedTexts()[1]));
|
||||
pos += regExp.matchedLength();
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ public:
|
||||
static inline QString directoryNameFromPath(const QString& path);
|
||||
static QString fileNameFromPath(const QString &path);
|
||||
static QString basePathFromPath(const QString &path);
|
||||
static QVector<QString> imagesFromMarkdownFile(const QString &filePath);
|
||||
private:
|
||||
static inline void addQssVarToMap(QVector<QPair<QString, QString> > &map,
|
||||
const QString &key, const QString &value);
|
||||
|
@ -355,6 +355,9 @@ void VFileList::deleteFileAndUpdateList(const QString &p_notebook,
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete local images in ./images
|
||||
deleteLocalImages(filePath);
|
||||
|
||||
// Delete the file
|
||||
QFile file(filePath);
|
||||
if (!file.remove()) {
|
||||
@ -539,3 +542,20 @@ void VFileList::convertFileType(const QString ¬ebook, const QString &fileRela
|
||||
}
|
||||
VUtils::writeFileToDisk(filePath, fileText);
|
||||
}
|
||||
|
||||
void VFileList::deleteLocalImages(const QString &filePath)
|
||||
{
|
||||
if (!VUtils::isMarkdown(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QVector<QString> images = VUtils::imagesFromMarkdownFile(filePath);
|
||||
int deleted = 0;
|
||||
for (int i = 0; i < images.size(); ++i) {
|
||||
QFile file(images[i]);
|
||||
if (file.remove()) {
|
||||
++deleted;
|
||||
}
|
||||
}
|
||||
qDebug() << "delete" << deleted << "images for" << filePath;
|
||||
}
|
||||
|
@ -63,6 +63,7 @@ private:
|
||||
void convertFileType(const QString ¬ebook, const QString &fileRelativePath,
|
||||
DocType oldType, DocType newType);
|
||||
QListWidgetItem *findItem(const QString &p_notebook, const QString &p_relativePath);
|
||||
void deleteLocalImages(const QString &filePath);
|
||||
|
||||
VNote *vnote;
|
||||
QString notebook;
|
||||
|
Loading…
x
Reference in New Issue
Block a user