Hook sending messages back up
This commit is contained in:
@@ -3,10 +3,65 @@
|
||||
|
||||
#include "blockcache.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include "chattextitemhelper.h"
|
||||
|
||||
using namespace Block;
|
||||
|
||||
inline QString formatQuote(const QString &input)
|
||||
{
|
||||
QString stringOut;
|
||||
auto splitString = input.split(u"\n\n"_s, Qt::SkipEmptyParts);
|
||||
for (auto &string : splitString) {
|
||||
if (string.startsWith(u'*')) {
|
||||
string.removeFirst();
|
||||
}
|
||||
if (string.startsWith(u'\"')) {
|
||||
string.removeFirst();
|
||||
}
|
||||
if (string.endsWith(u'*')) {
|
||||
string.removeLast();
|
||||
}
|
||||
if (string.endsWith(u'\"')) {
|
||||
string.removeLast();
|
||||
}
|
||||
if (!stringOut.isEmpty()) {
|
||||
stringOut += u"\n"_s;
|
||||
}
|
||||
stringOut += u"> "_s + string;
|
||||
}
|
||||
return stringOut;
|
||||
}
|
||||
|
||||
inline QString formatCode(const QString &input)
|
||||
{
|
||||
return u"```\n%1\n```"_s.arg(input).replace(u"\n\n"_s, u"\n"_s);
|
||||
}
|
||||
|
||||
inline QString trim(QString string)
|
||||
{
|
||||
while (string.startsWith(u"\n"_s)) {
|
||||
string.removeFirst();
|
||||
}
|
||||
while (string.endsWith(u"\n"_s)) {
|
||||
string.removeLast();
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
QString CacheItem::toString() const
|
||||
{
|
||||
auto newText = trim(content.toMarkdown(QTextDocument::MarkdownDialectGitHub));
|
||||
newText.replace(QRegularExpression(u"(?<!\n)\n(?!\n)"_s), u" "_s);
|
||||
if (type == MessageComponentType::Quote) {
|
||||
newText = formatQuote(newText);
|
||||
} else if (type == MessageComponentType::Code) {
|
||||
newText = formatCode(newText);
|
||||
}
|
||||
return newText;
|
||||
}
|
||||
|
||||
void Cache::fill(QList<MessageComponent> components)
|
||||
{
|
||||
std::ranges::for_each(components, [this](const MessageComponent &component) {
|
||||
@@ -23,3 +78,15 @@ void Cache::fill(QList<MessageComponent> components)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
QString Cache::toString() const
|
||||
{
|
||||
QString text;
|
||||
std::ranges::for_each(constBegin(), constEnd(), [&text](const CacheItem &item) {
|
||||
if (!text.isEmpty()) {
|
||||
text += u"\n\n"_s;
|
||||
}
|
||||
text += item.toString();
|
||||
});
|
||||
return text;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user