Browse Source

delete local images when deleting markdown file

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
0306256863
4 changed files with 45 additions and 0 deletions
  1. 23 0
      src/utils/vutils.cpp
  2. 1 0
      src/utils/vutils.h
  3. 20 0
      src/vfilelist.cpp
  4. 1 0
      src/vfilelist.h

+ 23 - 0
src/utils/vutils.cpp

@@ -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 ![](images/xx.jpg)
+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;
+}

+ 1 - 0
src/utils/vutils.h

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

+ 20 - 0
src/vfilelist.cpp

@@ -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 &notebook, 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;
+}

+ 1 - 0
src/vfilelist.h

@@ -63,6 +63,7 @@ private:
     void convertFileType(const QString &notebook, 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;