From 010f4245f66e05ae3524b8831c6a8e2e80fbba0e Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 22 Jan 2017 19:40:43 +0800 Subject: [PATCH] bugfix: clear image preview blocks having unmatched image path The syntax highlighter could not recognize image links with spaces in the URL. When clearing orphan image preview block, we need to clear thoes blocks which having unmatched image path. Fix issue: after inserting spaces in the URL of an image link, the preview block still exists. Signed-off-by: Le Tan --- src/vmdedit.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index 5850f193..7c65f94a 100644 --- a/src/vmdedit.cpp +++ b/src/vmdedit.cpp @@ -268,14 +268,28 @@ bool VMdEdit::isOrphanImagePreviewBlock(QTextBlock p_block) { if (isImagePreviewBlock(p_block)) { // It is an orphan image preview block if previous block is not - // a block need to preview (containing exactly one image). + // a block need to preview (containing exactly one image) or the image + // paths are not equal to each other. QTextBlock prevBlock = p_block.previous(); if (prevBlock.isValid()) { - if (fetchImageToPreview(prevBlock.text()).isEmpty()) { + QString imageLink = fetchImageToPreview(prevBlock.text()); + if (imageLink.isEmpty()) { return true; - } else { - return false; } + QString imagePath = QDir(m_file->retriveBasePath()).filePath(imageLink); + + // Get image preview block's image path. + QTextCursor cursor(p_block); + int shift = p_block.text().indexOf(QChar::ObjectReplacementCharacter); + if (shift > 0) { + cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, + shift + 1); + } + QTextImageFormat format = cursor.charFormat().toImageFormat(); + Q_ASSERT(format.isValid()); + QString curPath = format.property(ImagePath).toString(); + + return curPath != imagePath; } else { return true; }