Allow striking through text with ~~

Similar to spoilers, some Markdown flavors like GitHub's or Discord's
allow you to strike through text with ~~. Normally, the only way to do
this on most Matrix clients (including Element) is surrounding text with
<del> tags. As expected, no one knows how to do this.

So now NeoChat supports the ~~ syntax. We can reuse the existing spoiler
parser for this after making it generic. I have added new test cases for
this syntax. This does not affect the quick format bar yet.
This commit is contained in:
Joshua Goins
2025-05-08 14:50:24 -04:00
parent 14cbbd394f
commit 4757ac11dc
2 changed files with 61 additions and 45 deletions

View File

@@ -706,45 +706,53 @@ QString TextHandler::customMarkdownToHtml(const QString &stringIn)
{
QString buffer = stringIn;
qsizetype beginCodeBlockTag = buffer.indexOf(u"<code>"_s);
qsizetype endCodeBlockTag = buffer.indexOf(u"</code>"_s, beginCodeBlockTag + 1);
const auto processSyntax = [&buffer](const QString &syntax, const QString &beginTag, const QString &endTag) {
qsizetype beginCodeBlockTag = buffer.indexOf(u"<code>"_s);
qsizetype endCodeBlockTag = buffer.indexOf(u"</code>"_s, beginCodeBlockTag + 1);
// Indesx to search from
qsizetype lastPos = 0;
while (true) {
const qsizetype pos = buffer.indexOf(u"||"_s, lastPos);
if (pos == -1) {
break;
// Index to search from
qsizetype lastPos = 0;
while (true) {
const qsizetype pos = buffer.indexOf(syntax, lastPos);
if (pos == -1) {
break;
}
// If we're inside a code block, ignore and move the search past the code block
const bool validCodeBlock = beginCodeBlockTag != -1 && endCodeBlockTag != -1;
if (validCodeBlock && pos > beginCodeBlockTag && pos < endCodeBlockTag) {
lastPos = endCodeBlockTag + 5;
continue;
}
qsizetype nextPos = buffer.indexOf(syntax, pos + 1);
if (nextPos == -1) {
break;
}
// Replace the beginning syntax
buffer.replace(pos, 2, 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);
// 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;
}
};
// If we're inside of a code block, ignore and move the search past the code block
const bool validCodeBlock = beginCodeBlockTag != -1 && endCodeBlockTag != -1;
if (validCodeBlock && pos > beginCodeBlockTag && pos < endCodeBlockTag) {
lastPos = endCodeBlockTag + 5;
continue;
}
// spoilers
processSyntax(u"||"_s, u"<span data-mx-spoiler>"_s, u"</span>"_s);
qsizetype nextPos = buffer.indexOf(u"||"_s, pos + 1);
if (nextPos == -1) {
break;
}
// Replace the begin ||
buffer.replace(pos, 2, QStringLiteral("<span data-mx-spoiler>"));
// Update positions and re-search since the underlying text buffer changed
nextPos = buffer.indexOf(u"||"_s, pos + 1);
beginCodeBlockTag = buffer.indexOf(u"<code>"_s, pos + 1);
endCodeBlockTag = buffer.indexOf(u"</code>"_s, beginCodeBlockTag + 1);
// Now replace the end ||
buffer.replace(nextPos, 2, QStringLiteral("</span>"));
// 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;
}
// strikethrough
processSyntax(u"~~"_s, u"<del>"_s, u"</del>"_s);
return buffer;
}