cmark

My personal build of CMark ✏️

Commit
a4f61e832fa9f99e04d05ab4b4768538ba076c84
Parent
2c3612447e35c879ad99e637dd473ec58fde9fd0
Author
John MacFarlane <jgm@berkeley.edu>
Date

Treat line ending with EOF as ending with newline.

Closes #71.

Added a test to api_test.

Diffstat

2 files changed, 13 insertions, 0 deletions

Status File Name N° Changes Insertions Deletions
Modified api_test/main.c 9 9 0
Modified src/blocks.c 4 4 0
diff --git a/api_test/main.c b/api_test/main.c
@@ -677,6 +677,7 @@ line_endings(test_batch_runner *runner)
 	STR_EQ(runner, html, "<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n<li>d</li>\n</ul>\n",
 	       "list with different line endings");
 	free(html);
+
 	static const char crlf_lines[] = "line\r\nline\r\n";
 	html = cmark_markdown_to_html(crlf_lines,
 				      sizeof(crlf_lines) - 1,
@@ -684,6 +685,14 @@ line_endings(test_batch_runner *runner)
 	STR_EQ(runner, html, "<p>line<br />\nline</p>\n",
 	       "crlf endings with CMARK_OPT_HARDBREAKS");
 	free(html);
+
+	static const char no_line_ending[] = "```\nline\n```";
+	html = cmark_markdown_to_html(no_line_ending,
+				      sizeof(no_line_ending) - 1,
+				      CMARK_OPT_DEFAULT);
+	STR_EQ(runner, html, "<pre><code>line\n</code></pre>\n",
+	       "fenced code block with no final newline");
+	free(html);
 }
 
 static void
diff --git a/src/blocks.c b/src/blocks.c
@@ -598,6 +598,10 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
   } else {
     cmark_strbuf_put(parser->curline, buffer, bytes);
   }
+  // ensure line ends with a newline:
+  if (!S_is_line_end_char(parser->curline->ptr[bytes - 1])) {
+	  cmark_strbuf_putc(parser->curline, '\n');
+  }
   parser->offset = 0;
   parser->column = 0;
   parser->blank = false;