From 034cad79c7f4d9c07b6a897b3b791223e93e10a0 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Thu, 29 May 2025 11:42:19 +0200 Subject: [PATCH] Fix the code block handling for our custom Markdown syntax There were some cases that was hit that revealed some mistakes in the code block checking code, which is now fixed. Basically, we just needed to make sure the indices were updated at the right times. I also took some time to clean up some of the comments, and magic numbers used here. A new test case was added that was failing before in real-world testing. --- autotests/texthandlertest.cpp | 2 ++ src/libneochat/texthandler.cpp | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index 0faaa6f4c..a4cef2231 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -264,6 +264,8 @@ void TextHandlerTest::sendCustomTags_data() QTest::newRow("inside code block spoiler") << u"```||apple||```"_s << u"||apple||"_s; QTest::newRow("outside code block spoiler") << u"||apple|| ```||banana||``` ||pear||"_s << u"apple ||banana|| pear"_s; + QTest::newRow("complex spoiler") << u"Between `formFactor == Horizontal||Vertical` and `location == top||left||bottom||right`"_s + << u"Between formFactor == Horizontal||Vertical and location == top||left||bottom||right"_s; // strikethrough QTest::newRow("incomplete strikethrough") << u"~~test"_s << u"~~test"_s; diff --git a/src/libneochat/texthandler.cpp b/src/libneochat/texthandler.cpp index 9ac878209..90ec94d35 100644 --- a/src/libneochat/texthandler.cpp +++ b/src/libneochat/texthandler.cpp @@ -718,10 +718,15 @@ QString TextHandler::customMarkdownToHtml(const QString &stringIn) break; } - // If we're inside a code block, ignore and move the search past the code block + // If we're inside a code block, ignore and move the search past this code block const bool validCodeBlock = beginCodeBlockTag != -1 && endCodeBlockTag != -1; if (validCodeBlock && pos > beginCodeBlockTag && pos < endCodeBlockTag) { - lastPos = endCodeBlockTag + 5; + lastPos = endCodeBlockTag + 7; + + // since we moved past this code block, make sure to update the indices for the next one + beginCodeBlockTag = buffer.indexOf(u""_s, lastPos + 1); + endCodeBlockTag = buffer.indexOf(u""_s, beginCodeBlockTag + 1); + continue; } @@ -731,20 +736,24 @@ QString TextHandler::customMarkdownToHtml(const QString &stringIn) } // Replace the beginning syntax - buffer.replace(pos, 2, beginTag); + buffer.replace(pos, syntax.length(), beginTag); // Update positions and re-search since the underlying text buffer changed nextPos = buffer.indexOf(syntax, pos + 1); - beginCodeBlockTag = buffer.indexOf(u""_s, pos + 1); - endCodeBlockTag = buffer.indexOf(u""_s, beginCodeBlockTag + 1); // Now replace the end syntax - buffer.replace(nextPos, 2, endTag); + buffer.replace(nextPos, syntax.length(), endTag); + + // If we have begun checking spoilers past our current code block, make sure we're in the next one (if it exists) + if (nextPos > endCodeBlockTag) { + beginCodeBlockTag = buffer.indexOf(u""_s, nextPos + 1); + endCodeBlockTag = buffer.indexOf(u""_s, beginCodeBlockTag + 1); + } // Move the search pointer past this point. // Not technically needed in most cases since we replaced the original tag, but needed for code blocks // which still have the characters. - lastPos = nextPos + 2; + lastPos = nextPos + syntax.length(); } };