From 4f053f08683b54dd03d1085ed2b0c0c1dabaa6a6 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Fri, 9 Nov 2018 09:52:31 -0700 Subject: [PATCH 1/2] Avoid duplicate markup for multiline code block Previously, i was only incremented once for the multiline code block start marker, which would cause the three it to be interpreted as multiline-start, inline-start, inline-close. For example, the message ```hello``` would be rendered as
hello This commit fixes the issue by incrementing i to the end of the start marker. Signed-off-by: Kevin Locke --- libdiscord.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libdiscord.c b/libdiscord.c index 1b05900..fa5f893 100644 --- a/libdiscord.c +++ b/libdiscord.c @@ -1634,9 +1634,9 @@ discord_convert_markdown(const gchar *html) out = g_string_append(out, "
"); } else { out = g_string_append(out, ""); - i += 2; } + i += 2; s_codeblock = !s_codeblock; } else { HTML_TOGGLE_OUT(s_codebit, "", ""); From eb8ad1891db59db0ce7fbb83fc5b418174365a6c Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Fri, 9 Nov 2018 10:06:39 -0700 Subject: [PATCH 2/2] Conditionally add break after multiline code block In the Discord web client (and presumably others) multiline code blocks are rendered using `
text
`. Since `
` is a
block-level element, the text is always displayed on its own line(s).
Mimic this behavior by adding a line break when text following the block
would appear on the same line.  Ignore collapsible white space, as a
browser would.

Fixes: #207

Signed-off-by: Kevin Locke 
---
 libdiscord.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/libdiscord.c b/libdiscord.c
index fa5f893..d17944d 100644
--- a/libdiscord.c
+++ b/libdiscord.c
@@ -1579,6 +1579,18 @@ discord_underscore_match(const gchar *html, int i)
 	return FALSE;
 }
 
+static gboolean
+discord_has_nonspace_before_newline(const gchar *html, int i)
+{
+	while (html[i] && html[i] != '\n') {
+		if (!g_ascii_isspace(html[i++])) {
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
+
 static gchar *
 discord_convert_markdown(const gchar *html)
 {
@@ -1634,6 +1646,11 @@ discord_convert_markdown(const gchar *html)
 					out = g_string_append(out, "
"); } else { out = g_string_append(out, ""); + + /* Ensure no text on same line after code. */ + if (discord_has_nonspace_before_newline(html, i + 3)) { + out = g_string_append(out, "
"); + } } i += 2;