Sfoglia il codice sorgente

Turndown: remove title and alt text if they contains invalid characters

It is useful especially for OneNoe.
Le Tan 7 anni fa
parent
commit
ec15475c87
2 ha cambiato i file con 25 aggiunte e 3 eliminazioni
  1. 20 0
      src/resources/markdown_template.js
  2. 5 3
      src/utils/vutils.cpp

+ 20 - 0
src/resources/markdown_template.js

@@ -1638,6 +1638,26 @@ var htmlToText = function(identifier, id, timeStamp, html) {
         }
     });
 
+    ts.addRule('img_fix', {
+        filter: 'img',
+        replacement: function (content, node) {
+            var alt = node.alt || '';
+            if (/[\r\n\[\]]/g.test(alt)) {
+                alt= '';
+            }
+
+            var src = node.getAttribute('src') || '';
+
+            var title = node.title || '';
+            if (/[\r\n\)"]/g.test(title)) {
+                title = '';
+            }
+
+            var titlePart = title ? ' "' + title + '"' : '';
+            return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
+        }
+    });
+
     var markdown = ts.turndown(html);
     content.htmlToTextCB(identifier, id, timeStamp, markdown);
 };

+ 5 - 3
src/utils/vutils.cpp

@@ -46,7 +46,7 @@ QVector<QPair<QString, QString>> VUtils::s_availableLanguages;
 const QString VUtils::c_imageLinkRegExp = QString("\\!\\[([^\\[\\]]*)\\]"
                                                   "\\(\\s*"
                                                   "([^\\)\"'\\s]+)"
-                                                  "(\\s*(\"[^\"\\)\\n]*\")|('[^'\\)\\n]*'))?"
+                                                  "(\\s*(\"[^\"\\)\\n\\r]*\")|('[^'\\)\\n\\r]*'))?"
                                                   "(\\s*=(\\d*)x(\\d*))?"
                                                   "\\s*\\)");
 
@@ -54,8 +54,8 @@ const QString VUtils::c_imageTitleRegExp = QString("[^\\[\\]]*");
 
 const QString VUtils::c_linkRegExp = QString("\\[([^\\]]*)\\]"
                                              "\\(\\s*(\\S+)"
-                                             "(?:\\s+((\"[^\"\\n]*\")"
-                                                     "|('[^'\\n]*')))?\\s*"
+                                             "(?:\\s+((\"[^\"\\n\\r]*\")"
+                                                     "|('[^'\\n\\r]*')))?\\s*"
                                              "\\)");
 
 const QString VUtils::c_fileNameRegExp = QString("(?:[^\\\\/:\\*\\?\"<>\\|\\s]| )*");
@@ -158,6 +158,8 @@ QString VUtils::generateImageFileName(const QString &path,
                                       const QString &title,
                                       const QString &format)
 {
+    Q_UNUSED(title);
+
     const QChar sep('_');
 
     QString baseName(g_config->getImageNamePrefix());