diff --git a/src/node.h b/src/node.h index 3d9ddcf54..9759e98f0 100644 --- a/src/node.h +++ b/src/node.h @@ -86,6 +86,22 @@ struct cmark_node { CMARK_EXPORT int cmark_node_check(cmark_node *node, FILE *out); +static inline bool CMARK_NODE_TYPE_BLOCK_P(cmark_node_type node_type) { + return node_type < CMARK_NODE_TEXT; +} + +static inline bool CMARK_NODE_BLOCK_P(cmark_node *node) { + return node != NULL && CMARK_NODE_TYPE_BLOCK_P((cmark_node_type) node->type); +} + +static inline bool CMARK_NODE_TYPE_INLINE_P(cmark_node_type node_type) { + return node_type >= CMARK_NODE_TEXT; +} + +static inline bool CMARK_NODE_INLINE_P(cmark_node *node) { + return node != NULL && CMARK_NODE_TYPE_INLINE_P((cmark_node_type) node->type); +} + #ifdef __cplusplus } #endif diff --git a/src/render.c b/src/render.c index 7fc0d9654..0de109ec1 100644 --- a/src/render.c +++ b/src/render.c @@ -178,9 +178,11 @@ char *cmark_render(cmark_node *root, int options, int width, } } - // ensure final newline - if (renderer.buffer->size == 0 || renderer.buffer->ptr[renderer.buffer->size - 1] != '\n') { - cmark_strbuf_putc(renderer.buffer, '\n'); + // If the root node is a block type (i.e. not inline), ensure there's a final newline: + if (CMARK_NODE_TYPE_BLOCK_P(root->type)) { + if (renderer.buffer->size == 0 || renderer.buffer->ptr[renderer.buffer->size - 1] != '\n') { + cmark_strbuf_putc(renderer.buffer, '\n'); + } } result = (char *)cmark_strbuf_detach(renderer.buffer);