cmark

My personal build of CMark ✏️

Commit
07abc70323fe35f711a650b39472e0ac5af96185
Parent
86263adc7ee25ca727c770586fe1a67e41ad8f10
Author
John MacFarlane <jgm@berkeley.edu>
Date

commonmark renderer - improved tight list detection.

Diffstat

1 file changed, 21 insertions, 5 deletions

Status File Name N° Changes Insertions Deletions
Modified src/commonmark.c 26 21 5
diff --git a/src/commonmark.c b/src/commonmark.c
@@ -233,6 +233,19 @@ shortest_unused_backtick_sequence(cmark_chunk *code)
 	return i;
 }
 
+// if node is a block node, returns node.
+// otherwise returns first block-level node that is an ancestor of node.
+static cmark_node*
+get_containing_block(cmark_node *node)
+{
+	while (node &&
+	       (node->type < CMARK_NODE_FIRST_BLOCK ||
+		node->type > CMARK_NODE_LAST_BLOCK)) {
+		node = node->parent;
+	}
+	return node;
+}
+
 static int
 S_render_node(cmark_node *node, cmark_event_type ev_type,
               struct render_state *state)
@@ -255,11 +268,14 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
 	// a following list.
 	if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL &&
 	      entering)) {
-		state->in_tight_list_item =
-			(node->type == CMARK_NODE_ITEM &&
-			 cmark_node_get_list_tight(node->parent)) ||
-			(node->parent && node->parent->type == CMARK_NODE_ITEM &&
-			 cmark_node_get_list_tight(node->parent->parent));
+			tmp = get_containing_block(node);
+			state->in_tight_list_item =
+				(tmp->type == CMARK_NODE_ITEM &&
+				 cmark_node_get_list_tight(tmp->parent)) ||
+				(tmp &&
+				 tmp->parent &&
+				 tmp->parent->type == CMARK_NODE_ITEM &&
+				 cmark_node_get_list_tight(tmp->parent->parent));
 	}
 
 	switch (node->type) {