Skip to content

Commit eb8ad18

Browse files
committed
Conditionally add break after multiline code block
In the Discord web client (and presumably others) multiline code blocks are rendered using `<pre><code>text</code></pre>`. Since `<pre>` 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: EionRobb#207 Signed-off-by: Kevin Locke <[email protected]>
1 parent 4f053f0 commit eb8ad18

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

libdiscord.c

+17
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,18 @@ discord_underscore_match(const gchar *html, int i)
15791579
return FALSE;
15801580
}
15811581

1582+
static gboolean
1583+
discord_has_nonspace_before_newline(const gchar *html, int i)
1584+
{
1585+
while (html[i] && html[i] != '\n') {
1586+
if (!g_ascii_isspace(html[i++])) {
1587+
return TRUE;
1588+
}
1589+
}
1590+
1591+
return FALSE;
1592+
}
1593+
15821594
static gchar *
15831595
discord_convert_markdown(const gchar *html)
15841596
{
@@ -1634,6 +1646,11 @@ discord_convert_markdown(const gchar *html)
16341646
out = g_string_append(out, "<br/><span style='font-family: monospace; white-space: pre'>");
16351647
} else {
16361648
out = g_string_append(out, "</span>");
1649+
1650+
/* Ensure no text on same line after code. */
1651+
if (discord_has_nonspace_before_newline(html, i + 3)) {
1652+
out = g_string_append(out, "<br/>");
1653+
}
16371654
}
16381655

16391656
i += 2;

0 commit comments

Comments
 (0)