Skip to content

Commit

Permalink
Don't append a newline character when rendering inline nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 19, 2024
1 parent 2734f21 commit 9be6ac0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9be6ac0

Please sign in to comment.