@@ -127,8 +127,8 @@ static table_row *row_from_string(cmark_syntax_extension *self,
127127
128128 table_row * row = NULL ;
129129 bufsize_t cell_matched = 1 , pipe_matched = 1 , offset ;
130- int cell_end_offset ;
131130 int expect_more_cells = 1 ;
131+ int row_end_offset = 0 ;
132132
133133 row = (table_row * )parser -> mem -> calloc (1 , sizeof (table_row ));
134134 row -> n_columns = 0 ;
@@ -144,45 +144,53 @@ static table_row *row_from_string(cmark_syntax_extension *self,
144144 pipe_matched = scan_table_cell_end (string , len , offset + cell_matched );
145145
146146 if (cell_matched || pipe_matched ) {
147- cell_end_offset = offset + cell_matched - 1 ;
148-
149- if (string [cell_end_offset ] == '\n' || string [cell_end_offset ] == '\r' ) {
150- row -> paragraph_offset = cell_end_offset ;
151-
152- cmark_llist_free_full (parser -> mem , row -> cells , (cmark_free_func )free_table_cell );
153- row -> cells = NULL ;
154- row -> n_columns = 0 ;
155- } else {
156- // We are guaranteed to have a cell, since (1) either we found some
157- // content and cell_matched, or (2) we found an empty cell followed by a
158- // pipe.
159- cmark_strbuf * cell_buf = unescape_pipes (parser -> mem , string + offset ,
160- cell_matched );
161- cmark_strbuf_trim (cell_buf );
162-
163- node_cell * cell = (node_cell * )parser -> mem -> calloc (1 , sizeof (* cell ));
164- cell -> buf = cell_buf ;
165- cell -> start_offset = offset ;
166- cell -> end_offset = offset + cell_matched - 1 ;
167-
168- while (cell -> start_offset > 0 && string [cell -> start_offset - 1 ] != '|' ) {
169- -- cell -> start_offset ;
170- ++ cell -> internal_offset ;
171- }
172-
173- row -> n_columns += 1 ;
174- row -> cells = cmark_llist_append (parser -> mem , row -> cells , cell );
147+ // We are guaranteed to have a cell, since (1) either we found some
148+ // content and cell_matched, or (2) we found an empty cell followed by a
149+ // pipe.
150+ cmark_strbuf * cell_buf = unescape_pipes (parser -> mem , string + offset ,
151+ cell_matched );
152+ cmark_strbuf_trim (cell_buf );
153+
154+ node_cell * cell = (node_cell * )parser -> mem -> calloc (1 , sizeof (* cell ));
155+ cell -> buf = cell_buf ;
156+ cell -> start_offset = offset ;
157+ cell -> end_offset = offset + cell_matched - 1 ;
158+
159+ while (cell -> start_offset > 0 && string [cell -> start_offset - 1 ] != '|' ) {
160+ -- cell -> start_offset ;
161+ ++ cell -> internal_offset ;
175162 }
163+
164+ row -> n_columns += 1 ;
165+ row -> cells = cmark_llist_append (parser -> mem , row -> cells , cell );
176166 }
177167
178168 offset += cell_matched + pipe_matched ;
179169
180170 if (pipe_matched ) {
181171 expect_more_cells = 1 ;
182172 } else {
183- // We've scanned the last cell. Skip over the final newline and stop.
184- offset += scan_table_row_end (string , len , offset );
185- expect_more_cells = 0 ;
173+ // We've scanned the last cell. Check if we have reached the end of the row
174+ row_end_offset = scan_table_row_end (string , len , offset );
175+ offset += row_end_offset ;
176+
177+ // If the end of the row is not the end of the input,
178+ // the row is not a real row but potentially part of the paragraph
179+ // preceding the table.
180+ if (row_end_offset && offset != len ) {
181+ row -> paragraph_offset = offset ;
182+
183+ cmark_llist_free_full (parser -> mem , row -> cells , (cmark_free_func )free_table_cell );
184+ row -> cells = NULL ;
185+ row -> n_columns = 0 ;
186+
187+ // Scan past the (optional) leading pipe.
188+ offset += scan_table_cell_end (string , len , offset );
189+
190+ expect_more_cells = 1 ;
191+ } else {
192+ expect_more_cells = 0 ;
193+ }
186194 }
187195 }
188196
0 commit comments