Skip to content

Commit 1717040

Browse files
committed
Added cmark_node.parent_footnote_def, removed usage of 'user_data', made sure to free allocated string in commonmark.c
1 parent 32ffc77 commit 1717040

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/blocks.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,15 @@ static void process_footnotes(cmark_parser *parser) {
484484
if (!footnote->ix)
485485
footnote->ix = ++ix;
486486

487+
// store a reference to this footnote reference's footnote definition
488+
// this is used by renderers when generating label ids
489+
cur->parent_footnote_def = footnote->node;
490+
487491
// keep track of a) count of how many times this footnote def has been
488492
// referenced, and b) which reference index this footnote ref is at.
489493
// this is used by renderers when generating links and backreferences.
490494
cur->footnote.ref_ix = ++footnote->node->footnote.def_count;
491495

492-
// store the footnote reference text label in the footnote ref's node's
493-
// `user_data`, so that renderers can use the label when generating
494-
// links and backreferences.
495-
cur->user_data = parser->mem->calloc(1, (sizeof(char) * cur->as.literal.len) + 1);
496-
memmove(cur->user_data, cur->as.literal.data, cur->as.literal.len);
497-
498496
char n[32];
499497
snprintf(n, sizeof(n), "%d", footnote->ix);
500498
cmark_chunk_free(parser->mem, &cur->as.literal);

src/commonmark.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,13 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
477477
case CMARK_NODE_FOOTNOTE_REFERENCE:
478478
if (entering) {
479479
LIT("[^");
480-
OUT(node->user_data, false, LITERAL);
480+
481+
char *footnote_label = renderer->mem->calloc(1, (sizeof(char) * node->parent_footnote_def->as.literal.len) + 1);
482+
memmove(footnote_label, node->parent_footnote_def->as.literal.data, node->parent_footnote_def->as.literal.len);
483+
484+
OUT(footnote_label, false, LITERAL);
485+
renderer->mem->free(footnote_label);
486+
481487
LIT("]");
482488
}
483489
break;
@@ -487,10 +493,12 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
487493
renderer->footnote_ix += 1;
488494
LIT("[^");
489495

490-
char *str = renderer->mem->calloc(1, (sizeof(char) * node->as.literal.len) + 1);
491-
memmove(str, node->as.literal.data, node->as.literal.len);
496+
char *footnote_label = renderer->mem->calloc(1, (sizeof(char) * node->as.literal.len) + 1);
497+
memmove(footnote_label, node->as.literal.data, node->as.literal.len);
498+
499+
OUT(footnote_label, false, LITERAL);
500+
renderer->mem->free(footnote_label);
492501

493-
OUT(str, false, LITERAL);
494502
LIT("]:\n");
495503

496504
cmark_strbuf_puts(renderer->prefix, " ");

src/html.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ static int S_render_node(cmark_html_renderer *renderer, cmark_node *node,
424424
case CMARK_NODE_FOOTNOTE_REFERENCE:
425425
if (entering) {
426426
cmark_strbuf_puts(html, "<sup class=\"footnote-ref\"><a href=\"#fn:");
427-
cmark_strbuf_puts(html, node->user_data);
427+
cmark_strbuf_put(html, node->parent_footnote_def->as.literal.data, node->parent_footnote_def->as.literal.len);
428428
cmark_strbuf_puts(html, "\" id=\"fnref:");
429-
cmark_strbuf_puts(html, node->user_data);
429+
cmark_strbuf_put(html, node->parent_footnote_def->as.literal.data, node->parent_footnote_def->as.literal.len);
430430

431431
if (node->footnote.ref_ix > 1) {
432432
char n[32];

src/node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct cmark_node {
8181
int def_count;
8282
} footnote;
8383

84+
cmark_node *parent_footnote_def;
85+
8486
union {
8587
cmark_chunk literal;
8688
cmark_list list;

0 commit comments

Comments
 (0)