Skip to content

Commit 1326888

Browse files
committed
qownnotes/web-companion#35 htmltomarkdown: fix evernote tests
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent e028400 commit 1326888

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/utils/misc.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)