Skip to content
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
13 changes: 8 additions & 5 deletions api_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,13 +1120,15 @@ static void source_pos(test_batch_runner *runner) {
"> Sure.\n"
">\n"
"> 2. Yes, okay.\n"
"> ![ok](hi \"yes\")\n";
"> ![ok](hi \"yes\")\n"
">\n"
"> #\n";

cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT | CMARK_OPT_SOURCEPOS);
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<document sourcepos=\"1:1-10:20\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
"<document sourcepos=\"1:1-12:3\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
" <heading sourcepos=\"1:1-1:14\" level=\"1\">\n"
" <text sourcepos=\"1:3-1:6\" xml:space=\"preserve\">Hi *</text>\n"
" <emph sourcepos=\"1:7-1:13\">\n"
Expand All @@ -1148,16 +1150,16 @@ static void source_pos(test_batch_runner *runner) {
" </link>\n"
" <text sourcepos=\"4:42-4:42\" xml:space=\"preserve\">.</text>\n"
" </paragraph>\n"
" <block_quote sourcepos=\"6:1-10:20\">\n"
" <list sourcepos=\"6:3-10:20\" type=\"ordered\" start=\"1\" delimiter=\"period\" tight=\"false\">\n"
" <block_quote sourcepos=\"6:1-12:3\">\n"
" <list sourcepos=\"6:3-11:1\" type=\"ordered\" start=\"1\" delimiter=\"period\" tight=\"false\">\n"
" <item sourcepos=\"6:3-8:1\">\n"
" <paragraph sourcepos=\"6:6-7:10\">\n"
" <text sourcepos=\"6:6-6:10\" xml:space=\"preserve\">Okay.</text>\n"
" <softbreak />\n"
" <text sourcepos=\"7:6-7:10\" xml:space=\"preserve\">Sure.</text>\n"
" </paragraph>\n"
" </item>\n"
" <item sourcepos=\"9:3-10:20\">\n"
" <item sourcepos=\"9:3-11:1\">\n"
" <paragraph sourcepos=\"9:6-10:20\">\n"
" <text sourcepos=\"9:6-9:15\" xml:space=\"preserve\">Yes, okay.</text>\n"
" <softbreak />\n"
Expand All @@ -1167,6 +1169,7 @@ static void source_pos(test_batch_runner *runner) {
" </paragraph>\n"
" </item>\n"
" </list>\n"
" <heading sourcepos=\"12:3-12:3\" level=\"1\" />\n"
" </block_quote>\n"
"</document>\n",
"sourcepos are as expected");
Expand Down
22 changes: 17 additions & 5 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
}

static void chop_trailing_hashtags(cmark_chunk *ch) {
bufsize_t n, orig_n;
bufsize_t n, orig_n, i;

cmark_chunk_rtrim(ch);
orig_n = n = ch->len - 1;
Expand All @@ -668,10 +668,22 @@ static void chop_trailing_hashtags(cmark_chunk *ch) {
n--;

// Check for a space before the final #s:
if (n != orig_n && n >= 0 && S_is_space_or_tab(peek_at(ch, n))) {
ch->len = n;
cmark_chunk_rtrim(ch);
if (n == orig_n || n < 0 || !S_is_space_or_tab(peek_at(ch, n))) {
return;
}

// Check to make sure we have #s at beginning (see issue #656):
i = n;
while (i >= 0 && peek_at(ch, i) != '#') {
i--;
}

if (i < 0) { // We didn't find #, hence we had empty header (#656)
return;
}

ch->len = n;
cmark_chunk_rtrim(ch);
}

// Check for thematic break. On failure, return 0 and update
Expand Down Expand Up @@ -1262,7 +1274,7 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
} else if (accepts_lines(S_type(container))) {
if (S_type(container) == CMARK_NODE_HEADING &&
container->as.heading.setext == false) {
chop_trailing_hashtags(input);
chop_trailing_hashtags(input);
}
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
false);
Expand Down
Loading