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

It is useful especially for OneNoe.
This commit is contained in:
Le Tan 2018-10-15 19:39:01 +08:00
parent 1b95b636e7
commit ec15475c87
2 changed files with 25 additions and 3 deletions

View File

@ -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); var markdown = ts.turndown(html);
content.htmlToTextCB(identifier, id, timeStamp, markdown); content.htmlToTextCB(identifier, id, timeStamp, markdown);
}; };

View File

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