Skip to content

Commit fee8411

Browse files
committed
fix(math): preserve source and parse math fences natively
1 parent 373f4ee commit fee8411

3 files changed

Lines changed: 71 additions & 9 deletions

File tree

‎packages/core/cpp/enrmrkd/enrmrkd.c‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4382,6 +4382,19 @@ md_analyze_latex_bracket(MD_CTX* ctx, int mark_index)
43824382

43834383
if((mark->flags & MD_MARK_POTENTIAL_CLOSER) && LATEX_BRACKET_OPENERS.top >= 0) {
43844384
int opener_index = LATEX_BRACKET_OPENERS.top;
4385+
OFF off;
4386+
int substantive = false;
4387+
4388+
/* Numeric bracket citations are prose, not display equations. */
4389+
for(off = ctx->marks[opener_index].end; off < mark->beg; off++) {
4390+
if(!ISWHITESPACE(off) && !ISNEWLINE(off) && !ISDIGIT(off) &&
4391+
CH(off) != _T(',') && CH(off) != _T('\\'))
4392+
substantive = true;
4393+
}
4394+
if(!substantive) {
4395+
LATEX_BRACKET_OPENERS.top = -1;
4396+
return;
4397+
}
43854398

43864399
md_pop_openers(ctx, opener_index);
43874400
md_disable_marks(ctx, opener_index+1, mark_index);
@@ -5205,7 +5218,8 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, ENRMRKD_SIZE n_lines)
52055218
* above advances off past line->end over the trailing blanks
52065219
* (CommonMark code-span examples 335, 337, 640). */
52075220
if(off < mark->beg && ISNEWLINE(off))
5208-
ENRMRKD_TEXT(text_type, _T(" "), 1);
5221+
/* TeX comments end at a newline; only code spans fold it. */
5222+
ENRMRKD_TEXT(text_type, text_type == ENRMRKD_TEXT_LATEXMATH ? _T("\n") : _T(" "), 1);
52095223
} else if(text_type == ENRMRKD_TEXT_HTML) {
52105224
/* Inside raw HTML, we output the new line verbatim, including
52115225
* any trailing spaces. */
@@ -5410,6 +5424,7 @@ md_process_table_block_contents(MD_CTX* ctx, int col_count, const MD_LINE* lines
54105424
#define MD_BLOCK_CONTAINER (MD_BLOCK_CONTAINER_OPENER | MD_BLOCK_CONTAINER_CLOSER)
54115425
#define MD_BLOCK_LOOSE_LIST 0x04
54125426
#define MD_BLOCK_SETEXT_HEADER 0x08
5427+
#define MD_BLOCK_CLOSED_FENCE 0x10
54135428

54145429
struct MD_BLOCK_tag {
54155430
ENRMRKD_BLOCKTYPE type : 8;
@@ -5571,6 +5586,7 @@ md_process_leaf_block(MD_CTX* ctx, MD_BLOCK* block)
55715586
MD_ATTRIBUTE_BUILD lang_build = { 0 };
55725587
int is_in_tight_list;
55735588
int clean_fence_code_detail = false;
5589+
int math_fence = false;
55745590
int ret = 0;
55755591

55765592
/* For large tables check the table density: If it's too low, lets suppress
@@ -5616,6 +5632,9 @@ md_process_leaf_block(MD_CTX* ctx, MD_BLOCK* block)
56165632
memset(&det.code, 0, sizeof(ENRMRKD_BLOCK_CODE_DETAIL));
56175633
clean_fence_code_detail = true;
56185634
MD_CHECK(md_setup_fenced_code_detail(ctx, block, &det.code, &info_build, &lang_build));
5635+
math_fence = (ctx->parser.flags & ENRMRKD_FLAG_LATEXMATHSPANS) &&
5636+
(block->flags & MD_BLOCK_CLOSED_FENCE) &&
5637+
det.code.lang.size == 4 && memcmp(det.code.lang.text, _T("math"), 4 * sizeof(CHAR)) == 0;
56195638
}
56205639
break;
56215640

@@ -5634,7 +5653,9 @@ md_process_leaf_block(MD_CTX* ctx, MD_BLOCK* block)
56345653
break;
56355654
}
56365655

5637-
if(!is_in_tight_list || block->type != ENRMRKD_BLOCK_P)
5656+
if(math_fence)
5657+
MD_ENTER_SPAN(ENRMRKD_SPAN_LATEXMATH_DISPLAY, NULL);
5658+
else if(!is_in_tight_list || block->type != ENRMRKD_BLOCK_P)
56385659
MD_ENTER_BLOCK(block->type, (void*) &det);
56395660

56405661
/* Process the block contents accordingly to is type. */
@@ -5665,7 +5686,9 @@ md_process_leaf_block(MD_CTX* ctx, MD_BLOCK* block)
56655686
break;
56665687
}
56675688

5668-
if(!is_in_tight_list || block->type != ENRMRKD_BLOCK_P)
5689+
if(math_fence)
5690+
MD_LEAVE_SPAN(ENRMRKD_SPAN_LATEXMATH_DISPLAY, NULL);
5691+
else if(!is_in_tight_list || block->type != ENRMRKD_BLOCK_P)
56695692
MD_LEAVE_BLOCK(block->type, (void*) &det);
56705693

56715694
abort:
@@ -6888,6 +6911,7 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
68886911
* which we transform into MD_LINE_BLANK. */
68896912
if(line->indent < ctx->code_indent_offset) {
68906913
if(md_is_closing_code_fence(ctx, CH(pivot_line->beg), off, &off)) {
6914+
ctx->current_block->flags |= MD_BLOCK_CLOSED_FENCE;
68916915
line->type = MD_LINE_BLANK;
68926916
ctx->last_line_has_list_loosening_effect = false;
68936917
break;

0 commit comments

Comments
 (0)