Skip to content

Commit 78e1cc0

Browse files
Add ancestor_extension field.
1 parent 763587e commit 78e1cc0

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ struct cmark_node {
8282

8383
cmark_syntax_extension *extension;
8484

85+
/**
86+
* Used during cmark_render() to cache the most recent non-NULL
87+
* extension, if you go up the parent chain like this:
88+
*
89+
* node->parent->...parent->extension
90+
*/
91+
cmark_syntax_extension *ancestor_extension;
92+
8593
union {
8694
int ref_ix;
8795
int def_count;

src/render.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ static void S_out(cmark_renderer *renderer, cmark_node *node,
3131
cmark_chunk remainder = cmark_chunk_literal("");
3232
int k = renderer->buffer->size - 1;
3333

34-
cmark_syntax_extension *ext = NULL;
35-
cmark_node *n = node;
36-
while (n && !ext) {
37-
ext = n->extension;
38-
if (!ext)
39-
n = n->parent;
40-
}
34+
cmark_syntax_extension *ext = node->ancestor_extension;
4135
if (ext && !ext->commonmark_escape_func)
4236
ext = NULL;
4337

@@ -182,6 +176,11 @@ char *cmark_render(cmark_mem *mem, cmark_node *root, int options, int width,
182176

183177
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
184178
cur = cmark_iter_get_node(iter);
179+
if (cur->extension) {
180+
cur->ancestor_extension = cur->extension;
181+
} else if (cur->parent) {
182+
cur->ancestor_extension = cur->parent->ancestor_extension;
183+
}
185184
if (!render_node(&renderer, cur, ev_type, options)) {
186185
// a false value causes us to skip processing
187186
// the node's contents. this is used for

0 commit comments

Comments
 (0)