Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to preserve linebreaks with backslash #679

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ext/redcarpet/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,17 @@ char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t of
}


/* char_linebreak • '\n' preceded by two spaces (assuming linebreak != 0) */
/* char_linebreak • '\n' preceded by two spaces or backslash (assuming linebreak != 0) */
static size_t
char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
{
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ')
if (!(offset >= 1 && data[-1] == '\\') && !(offset >= 2 && data[-1] == ' ' && data[-2] == ' '))
return 0;

/* removing escaping backslash if there was any */
if (ob->size && ob->data[ob->size - 1] == '\\')
ob->size--;

/* removing the last space from ob and rendering */
while (ob->size && ob->data[ob->size - 1] == ' ')
ob->size--;
Expand Down
21 changes: 21 additions & 0 deletions test/markdown_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,25 @@ def test_single_dashes_on_table_headers

assert_match /<table>/, output
end

def test_double_space_inserts_linebreak
result = %(<p>here comes a break:<br>\nThere it was.</p>)
output = render("here comes a break: \nThere it was.")

assert_equal result, output
end

def test_backslash_escapes_linebreak
result = %(<p>here comes a break:<br>\nThere it was.</p>)
output = render("here comes a break:\\\nThere it was.")

assert_equal result, output
end

def test_double_backslash_does_not_escape_linebreak
result = %(<p>here comes a backslash, not a break:\\There it was.</p>)
output = render("here comes a backslash, not a break:\\There it was.")

assert_equal result, output
end
end