Skip to content

Commit ee05faf

Browse files
zayeem06jgm
authored andcommitted
fix hard line break round-trip in commonmark renderer
1 parent 7e1e10c commit ee05faf

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎api_test/main.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,37 @@ static void render_commonmark(test_batch_runner *runner) {
773773
free(commonmark);
774774

775775
cmark_node_free(empty_item_doc);
776+
777+
// Hard line breaks are written as a backslash break in every position.
778+
// Two trailing spaces would be stripped as insignificant whitespace at the
779+
// start of a line, or after a space, losing the break on a round trip.
780+
static const char lead_break[] = "\\\nfoo\n";
781+
cmark_node *lead_break_doc =
782+
cmark_parse_document(lead_break, sizeof(lead_break) - 1, CMARK_OPT_DEFAULT);
783+
commonmark = cmark_render_commonmark(lead_break_doc, CMARK_OPT_DEFAULT, 0);
784+
STR_EQ(runner, commonmark, "\\\nfoo\n",
785+
"render hard break at start of line");
786+
free(commonmark);
787+
cmark_node_free(lead_break_doc);
788+
789+
static const char space_break[] = "foo \\\nbar\n";
790+
cmark_node *space_break_doc =
791+
cmark_parse_document(space_break, sizeof(space_break) - 1, CMARK_OPT_DEFAULT);
792+
commonmark = cmark_render_commonmark(space_break_doc, CMARK_OPT_DEFAULT, 0);
793+
STR_EQ(runner, commonmark, "foo \\\nbar\n",
794+
"render hard break after a space");
795+
free(commonmark);
796+
cmark_node_free(space_break_doc);
797+
798+
static const char plain_break[] = "foo\\\nbar\n";
799+
cmark_node *plain_break_doc =
800+
cmark_parse_document(plain_break, sizeof(plain_break) - 1, CMARK_OPT_DEFAULT);
801+
commonmark = cmark_render_commonmark(plain_break_doc, CMARK_OPT_DEFAULT, 0);
802+
STR_EQ(runner, commonmark, "foo\\\nbar\n",
803+
"render ordinary hard break");
804+
free(commonmark);
805+
cmark_node_free(plain_break_doc);
806+
776807
cmark_node_free(text);
777808
cmark_node_free(doc);
778809
}

‎src/commonmark.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
331331

332332
case CMARK_NODE_LINEBREAK:
333333
if (!(CMARK_OPT_HARDBREAKS & options)) {
334-
LIT(" ");
334+
// A backslash line break is unambiguous in every position. Two trailing
335+
// spaces are only recognized as a hard break when they follow text on
336+
// the line; at the start of a line, or after a space, they are stripped
337+
// as insignificant whitespace and the break is lost on a round trip.
338+
LIT("\\");
335339
}
336340
CR();
337341
break;

0 commit comments

Comments
 (0)