Skip to content

Commit

Permalink
Conditionally add break after multiline code block
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kevinoid committed Nov 9, 2018
1 parent 4f053f0 commit eb8ad18
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libdiscord.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -1634,6 +1646,11 @@ discord_convert_markdown(const gchar *html)
out = g_string_append(out, "<br/><span style='font-family: monospace; white-space: pre'>");
} else {
out = g_string_append(out, "</span>");

/* Ensure no text on same line after code. */
if (discord_has_nonspace_before_newline(html, i + 3)) {
out = g_string_append(out, "<br/>");
}
}

i += 2;
Expand Down

0 comments on commit eb8ad18

Please sign in to comment.