1. InsertImageDialog crashes when file does not exist;
2. Use &nbsp; for leading spaces of <p> element in Turndown;
This commit is contained in:
Le Tan 2021-06-24 21:36:47 +08:00
parent 36dd070d05
commit 30e3b5721e
4 changed files with 38 additions and 4 deletions

View File

@ -24,7 +24,7 @@
using namespace vnotex;
#ifndef QT_NO_DEBUG
// #define VX_DEBUG_WEB
#define VX_DEBUG_WEB
#endif
const QString ConfigMgr::c_orgName = "VNote";

View File

@ -21,10 +21,13 @@ class TurndownConverter {
// TODO: verify and copy several rules from VNote 2.0.
this.fixMark();
this.fixParagraph();
}
turndown(p_html) {
return this.ts.turndown(p_html);
let markdown = this.ts.turndown(p_html);
return markdown;
}
// Trim a string into 3 parts: leading spaces, content, trailing spaces.
@ -69,6 +72,25 @@ class TurndownConverter {
});
}
fixParagraph() {
this.ts.addRule('paragraph', {
filter: 'p',
replacement: function(content) {
// Replace leading spaces with &nbsp; to avoid being parsed as code block.
let lRe = /^\s+/;
let ret = lRe.exec(content);
if (ret) {
let leadingSpaces = ret[0];
if (leadingSpaces.length > 3) {
content = '&nbsp;'.repeat(leadingSpaces.length) + content.slice(leadingSpaces.length);
}
}
return '\n\n' + content + '\n\n'
}
});
}
fixImage() {
this.ts.addRule('img_fix', {
filter: 'img',

View File

@ -61,6 +61,8 @@ void ImageInsertDialog::setupUI(const QString &p_title,
auto gridLayout = new QGridLayout();
mainLayout->addLayout(gridLayout);
mainLayout->addStretch();
// Image Path.
m_imagePathEdit = WidgetsFactory::createLineEdit(p_imagePath, mainWidget);
m_imagePathEdit->setReadOnly(!m_browserEnabled);
@ -172,7 +174,13 @@ void ImageInsertDialog::checkImagePathInput()
}
if (url.isLocalFile()) {
setImage(FileUtils::imageFromFile(url.toLocalFile()));
const auto localFile = url.toLocalFile();
if (QFileInfo::exists(localFile)) {
setImage(FileUtils::imageFromFile(localFile));
} else {
setImage(QImage());
}
m_source = Source::LocalFile;
} else {
setImage(QImage());

View File

@ -1049,7 +1049,7 @@ void MarkdownEditor::fetchImagesToLocalAndReplace(QString &p_text)
}
const QString imageTitle = purifyImageTitle(regExp.cap(1).trimmed());
const QString imageUrl = regExp.cap(2).trimmed();
QString imageUrl = regExp.cap(2).trimmed();
const int maxUrlLength = 100;
QString urlToDisplay(imageUrl);
@ -1091,6 +1091,10 @@ void MarkdownEditor::fetchImagesToLocalAndReplace(QString &p_text)
}
} else {
// Network path.
// Prepend the protocol if missing.
if (imageUrl.startsWith(QStringLiteral("//"))) {
imageUrl.prepend(QStringLiteral("https:"));
}
QByteArray data = vte::Downloader::download(QUrl(imageUrl));
if (!data.isEmpty()) {
auto suffix = info.suffix();