@@ -646,6 +646,21 @@ QString Utils::Misc::htmlToMarkdown(QString text) {
646646 // we can get those from Google Chrome via the clipboard
647647 text.remove (QChar (0 ));
648648
649+ // Protect existing Markdown code blocks from being processed
650+ QRegularExpression existingCodeBlockRe (QStringLiteral (" ```[^`]*```" ),
651+ QRegularExpression::DotMatchesEverythingOption);
652+ QStringList existingCodeBlocks;
653+ QRegularExpressionMatchIterator codeBlockIt = existingCodeBlockRe.globalMatch (text);
654+ int placeholderIndex = 0 ;
655+
656+ while (codeBlockIt.hasNext ()) {
657+ QRegularExpressionMatch match = codeBlockIt.next ();
658+ QString codeBlock = match.captured (0 );
659+ existingCodeBlocks.append (codeBlock);
660+ QString placeholder = QStringLiteral (" ___CODE_BLOCK_PLACEHOLDER_%1___" ).arg (placeholderIndex++);
661+ text.replace (codeBlock, placeholder);
662+ }
663+
649664 // remove some blocks
650665 text.remove (QRegularExpression (QStringLiteral (" <head.*?>(.+?)<\\ /head>" ),
651666 QRegularExpression::CaseInsensitiveOption |
@@ -943,6 +958,12 @@ QString Utils::Misc::htmlToMarkdown(QString text) {
943958 // replace multiple line breaks
944959 text.replace (QRegularExpression (QStringLiteral (" \n\n\n +" )), QStringLiteral (" \n\n " ));
945960
961+ // Restore protected code blocks
962+ for (int i = 0 ; i < existingCodeBlocks.size (); ++i) {
963+ QString placeholder = QStringLiteral (" ___CODE_BLOCK_PLACEHOLDER_%1___" ).arg (i);
964+ text.replace (placeholder, existingCodeBlocks[i]);
965+ }
966+
946967 // Trim leading/trailing whitespace
947968 text = text.trimmed ();
948969
0 commit comments