Skip to content

Commit 5c75d23

Browse files
committed
Ignore nested STRONGs during rendering
STRONG, in most rendering engines, becomes bold. Bold cannot be applied to text two times in most languages. This caps the number of times we attempt to bold text when rendering. Running `python3 -c 'pad = "_" * 100000; print(pad + "." + pad, end="")' | time ./build/src/cmark-gfm --to $LANG` Before: ``` ./build/src/cmark-gfm --to plaintext > /dev/null 12.29s user 0.00s system 99% cpu 12.321 total ./build/src/cmark-gfm --to commonmark > /dev/null 25.97s user 0.01s system 99% cpu 26.026 total ./build/src/cmark-gfm --to html > /dev/null 0.01s user 0.00s system 43% cpu 0.033 total ./build/src/cmark-gfm --to man > /dev/null 12.91s user 0.00s system 99% cpu 12.938 total ./build/src/cmark-gfm --to latex > /dev/null 13.13s user 0.01s system 99% cpu 13.159 total ``` After: ``` ./build/src/cmark-gfm --to plaintext > /dev/null 0.01s user 0.01s system 39% cpu 0.030 total ./build/src/cmark-gfm --to commonmark > /dev/null 0.01s user 0.00s system 41% cpu 0.031 total ./build/src/cmark-gfm --to html > /dev/null 0.01s user 0.00s system 38% cpu 0.030 total ./build/src/cmark-gfm --to man > /dev/null 0.01s user 0.01s system 40% cpu 0.030 total ./build/src/cmark-gfm --to latex > /dev/null 0.01s user 0.00s system 39% cpu 0.033 total ```
1 parent c32ef78 commit 5c75d23

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

src/commonmark.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,17 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
189189
// Don't adjust tight list status til we've started the list.
190190
// Otherwise we loose the blank line between a paragraph and
191191
// a following list.
192-
if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) {
193-
tmp = get_containing_block(node);
194-
renderer->in_tight_list_item =
195-
tmp && // tmp might be NULL if there is no containing block
196-
((tmp->type == CMARK_NODE_ITEM &&
197-
cmark_node_get_list_tight(tmp->parent)) ||
198-
(tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
199-
cmark_node_get_list_tight(tmp->parent->parent)));
192+
if (entering) {
193+
if (node->parent && node->parent->type == CMARK_NODE_ITEM) {
194+
renderer->in_tight_list_item = node->parent->parent->as.list.tight;
195+
}
196+
} else {
197+
if (node->type == CMARK_NODE_LIST) {
198+
renderer->in_tight_list_item =
199+
node->parent &&
200+
node->parent->type == CMARK_NODE_ITEM &&
201+
node->parent->parent->as.list.tight;
202+
}
200203
}
201204

202205
if (node->extension && node->extension->commonmark_render_func) {
@@ -405,10 +408,12 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
405408
break;
406409

407410
case CMARK_NODE_STRONG:
408-
if (entering) {
409-
LIT("**");
410-
} else {
411-
LIT("**");
411+
if (node->parent == NULL || node->parent->type != CMARK_NODE_STRONG) {
412+
if (entering) {
413+
LIT("**");
414+
} else {
415+
LIT("**");
416+
}
412417
}
413418
break;
414419

src/html.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,12 @@ static int S_render_node(cmark_html_renderer *renderer, cmark_node *node,
364364
break;
365365

366366
case CMARK_NODE_STRONG:
367-
if (entering) {
368-
cmark_strbuf_puts(html, "<strong>");
369-
} else {
370-
cmark_strbuf_puts(html, "</strong>");
367+
if (node->parent == NULL || node->parent->type != CMARK_NODE_STRONG) {
368+
if (entering) {
369+
cmark_strbuf_puts(html, "<strong>");
370+
} else {
371+
cmark_strbuf_puts(html, "</strong>");
372+
}
371373
}
372374
break;
373375

src/latex.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,12 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
385385
break;
386386

387387
case CMARK_NODE_STRONG:
388-
if (entering) {
389-
LIT("\\textbf{");
390-
} else {
391-
LIT("}");
388+
if (node->parent == NULL || node->parent->type != CMARK_NODE_STRONG) {
389+
if (entering) {
390+
LIT("\\textbf{");
391+
} else {
392+
LIT("}");
393+
}
392394
}
393395
break;
394396

src/man.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
225225
break;
226226

227227
case CMARK_NODE_STRONG:
228-
if (entering) {
229-
LIT("\\f[B]");
230-
} else {
231-
LIT("\\f[]");
228+
if (node->parent == NULL || node->parent->type != CMARK_NODE_STRONG) {
229+
if (entering) {
230+
LIT("\\f[B]");
231+
} else {
232+
LIT("\\f[]");
233+
}
232234
}
233235
break;
234236

src/plaintext.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
4646
// Don't adjust tight list status til we've started the list.
4747
// Otherwise we loose the blank line between a paragraph and
4848
// a following list.
49-
if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) {
50-
tmp = get_containing_block(node);
51-
renderer->in_tight_list_item =
52-
tmp && // tmp might be NULL if there is no containing block
53-
((tmp->type == CMARK_NODE_ITEM &&
54-
cmark_node_get_list_tight(tmp->parent)) ||
55-
(tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
56-
cmark_node_get_list_tight(tmp->parent->parent)));
49+
if (entering) {
50+
if (node->parent && node->parent->type == CMARK_NODE_ITEM) {
51+
renderer->in_tight_list_item = node->parent->parent->as.list.tight;
52+
}
53+
} else {
54+
if (node->type == CMARK_NODE_LIST) {
55+
renderer->in_tight_list_item =
56+
node->parent &&
57+
node->parent->type == CMARK_NODE_ITEM &&
58+
node->parent->parent->as.list.tight;
59+
}
5760
}
5861

5962
if (node->extension && node->extension->plaintext_render_func) {

0 commit comments

Comments
 (0)