diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2023-01-31 18:22:12 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2023-01-31 18:22:12 +0100 |
commit | 0c3d46795b589d9dfb2e6caa7f94db77fd5371bd (patch) | |
tree | 4d3bdc9cee84be37f51465e49ec38a9055452dd0 /src/Utils.cpp | |
parent | Uncoditionally request keyframes (diff) | |
download | nheko-0c3d46795b589d9dfb2e6caa7f94db77fd5371bd.tar.xz |
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
Diffstat (limited to 'src/Utils.cpp')
-rw-r--r-- | src/Utils.cpp | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp index 218edb62..649e9124 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -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>"))) { |