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.
This commit is contained in:
Joshua Goins
2025-05-29 11:42:19 +02:00
parent 5c8ba7e29e
commit 034cad79c7
2 changed files with 18 additions and 7 deletions

View File

@@ -264,6 +264,8 @@ void TextHandlerTest::sendCustomTags_data()
QTest::newRow("inside code block spoiler") << u"```||apple||```"_s << u"<code>||apple||</code>"_s;
QTest::newRow("outside code block spoiler") << u"||apple|| ```||banana||``` ||pear||"_s
<< u"<span data-mx-spoiler>apple</span> <code>||banana||</code> <span data-mx-spoiler>pear</span>"_s;
QTest::newRow("complex spoiler") << u"Between `formFactor == Horizontal||Vertical` and `location == top||left||bottom||right`"_s
<< u"Between <code>formFactor == Horizontal||Vertical</code> and <code>location == top||left||bottom||right</code>"_s;
// strikethrough
QTest::newRow("incomplete strikethrough") << u"~~test"_s << u"~~test"_s;

View File

@@ -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"<code>"_s, lastPos + 1);
endCodeBlockTag = buffer.indexOf(u"</code>"_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"<code>"_s, pos + 1);
endCodeBlockTag = buffer.indexOf(u"</code>"_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"<code>"_s, nextPos + 1);
endCodeBlockTag = buffer.indexOf(u"</code>"_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();
}
};