From 07abc70323fe35f711a650b39472e0ac5af96185 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 29 Mar 2015 20:32:15 -0700 Subject: commonmark renderer - improved tight list detection. --- src/commonmark.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/commonmark.c') diff --git a/src/commonmark.c b/src/commonmark.c index c94cf9a..83a6602 100644 --- 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) { -- cgit v1.2.3