From 9be6ac03f15428cc1c3cb925ef554cda38d7ead1 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 19 Aug 2024 16:45:32 +1200 Subject: [PATCH] Don't append a newline character when rendering inline nodes. --- src/node.h | 16 ++++++++++++++++ src/render.c | 8 +++++--- 2 files changed, 21 insertions(+), 3 deletions(-) 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);