From ec15475c878384db175b288c18fe1bd50af72327 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 15 Oct 2018 19:39:01 +0800 Subject: [PATCH] Turndown: remove title and alt text if they contains invalid characters It is useful especially for OneNoe. --- src/resources/markdown_template.js | 20 ++++++++++++++++++++ src/utils/vutils.cpp | 8 +++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/resources/markdown_template.js b/src/resources/markdown_template.js index 6a54dc5a..5cb92548 100644 --- a/src/resources/markdown_template.js +++ b/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); }; diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index e15c38da..36734397 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -46,7 +46,7 @@ QVector> 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());