Make single newlines cause a <br> by default

This should match what people expect from a chat application much
better. The biggest reason not to do this, is because some people might
paste markdown documents. For those people there is now a /cmark
command, which disables most of our extensions to cmark, including the
newline behaviour. There is a long discussion on the Fediverse and on
Github linked below.

Mastodon https://fosstodon.org/@deepbluev7/109771668066978726
fixes #757
This commit is contained in:
Nicolas Werner 2023-01-31 18:22:12 +01:00
parent 9f529075f0
commit 0c3d46795b
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
8 changed files with 48 additions and 15 deletions

View file

@ -901,19 +901,24 @@ process_strikethrough(cmark_node *node)
cmark_iter_free(iter);
}
QString
utils::markdownToHtml(const QString &text, bool rainbowify_)
utils::markdownToHtml(const QString &text, bool rainbowify_, bool noExtensions)
{
const auto str = text.toUtf8();
cmark_node *const node = cmark_parse_document(str.constData(), str.size(), CMARK_OPT_UNSAFE);
process_strikethrough(node);
process_spoilers(node);
if (!noExtensions) {
process_strikethrough(node);
process_spoilers(node);
if (rainbowify_) {
rainbowify(node);
if (rainbowify_) {
rainbowify(node);
}
}
const char *tmp_buf = cmark_render_html(node, CMARK_OPT_UNSAFE);
const char *tmp_buf = cmark_render_html(
node,
// by default make single linebreaks <br> tags
noExtensions ? CMARK_OPT_UNSAFE : (CMARK_OPT_UNSAFE | CMARK_OPT_HARDBREAKS));
// Copy the null terminated output buffer.
std::string html(tmp_buf);
@ -921,7 +926,11 @@ utils::markdownToHtml(const QString &text, bool rainbowify_)
free((char *)tmp_buf);
cmark_node_free(node);
auto result = linkifyMessage(escapeBlacklistedHtml(QString::fromStdString(html))).trimmed();
auto result = escapeBlacklistedHtml(QString::fromStdString(html)).trimmed();
if (!noExtensions) {
result = linkifyMessage(std::move(result)).trimmed();
}
if (result.count(QStringLiteral("<p>")) == 1 && result.startsWith(QLatin1String("<p>")) &&
result.endsWith(QLatin1String("</p>"))) {