Skip to content

Commit 12bf1ce

Browse files
committed
Syntax: Handle math blocks within paragraph context
Markdown doesn't know about MathJAX expressions. They are rendered by browsers after HTML has been generated. To reflect that behavior, this commit handles math blocks as normal inline content of paragraphs. To support background tinting as for fenced code blocks, leading and trailing whitespace of standalone math blocks - not preceded nor followed by text - is scoped `markup.math`. LaTeX contexts are embedded rather than included to ensure to bailout from incomplete math expressions. This prevents highlighting the whole document as latex code.
1 parent 9a9fcb2 commit 12bf1ce

4 files changed

Lines changed: 300 additions & 35 deletions

File tree

messages/3.1.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ feedback you can use [GitHub issues][issues].
1616

1717
* Use lower case check mark completed gfm tasks `- [x] task` (#696)
1818
* Improve accuracy of (incomplete) inline MathJAX ($1+\srqt{b$) expressions
19+
* Improve incomplete/standalone/inline math blocks highlighting
1920

2021
[issues]: https://github.com/SublimeText-Markdown/MarkdownEditing/issues

syntaxes/Markdown.sublime-syntax

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ variables:
243243
tag_unquoted_attribute_start: (?=[^{{ascii_space}}=/>}])
244244
tag_unquoted_attribute_break: (?=[{{ascii_space}}}]|/?>)
245245

246-
math_block: '[ ]{,3}\$\$'
247-
248246
reference_definition: (?:\[{{reference_name}}\]\:)
249247
footnote_name: (?:\^(?:\\\]|[^]])+)
250248
reference_name: (?:(?:\\\]|[^]])+)
@@ -257,7 +255,6 @@ variables:
257255
| {{fenced_code_block_start}} # a fenced codeblock begins the line
258256
| {{thematic_break}} # line is a thematic beak
259257
| {{first_list_item}} # a list item begins the line
260-
| {{math_block}} # a math block begins the line
261258
| {{html_block}} # a html block begins the line
262259
)
263260
)
@@ -271,7 +268,6 @@ variables:
271268
| {{fenced_code_block_start}} # a fenced codeblock begins the line
272269
| {{thematic_break}} # line is a thematic beak
273270
| {{list_item}} # a list item begins the line
274-
| {{math_block}} # a math block begins the line
275271
| {{html_block}} # a html block begins the line
276272
)
277273
)
@@ -353,7 +349,6 @@ contexts:
353349
- include: list-blocks
354350
- include: tables
355351
- include: fenced-code-blocks
356-
- include: math-blocks
357352
- include: html-blocks
358353
- include: reference-definitions
359354
- include: atx-headings
@@ -516,7 +511,6 @@ contexts:
516511
| {{fenced_code_block_start}} # a fenced codeblock begins the line
517512
| {{thematic_break}} # line is a thematic beak
518513
| {{list_item}} # a list item begins the line
519-
| {{math_block}} # a math block begins the line
520514
| {{html_block}} # a html block begins the line
521515
)
522516
)
@@ -628,7 +622,6 @@ contexts:
628622
| {{fenced_code_block_start}} # a fenced codeblock begins the line
629623
| {{thematic_break}} # line is a thematic beak
630624
| {{list_item}} # a list item begins the line
631-
| {{math_block}} # a math block begins the line
632625
| {{html_block}} # a html block begins the line
633626
)
634627
)
@@ -656,7 +649,6 @@ contexts:
656649
| {{fenced_code_block_start}} # a fenced codeblock begins the line
657650
| {{thematic_break}} # line is a thematic beak
658651
| {{list_item}} # a list item begins the line
659-
| {{math_block}} # a math block begins the line
660652
| {{html_block}} # a html block begins the line
661653
)
662654
)
@@ -698,7 +690,6 @@ contexts:
698690

699691
list-block-content:
700692
- include: fenced-code-blocks
701-
- include: math-blocks
702693
- include: html-blocks
703694
- include: reference-definitions
704695
- include: list-block-common
@@ -802,6 +793,7 @@ contexts:
802793
- meta_scope: meta.paragraph.list.markdown
803794
- include: list-paragraph-fail
804795
- include: list-paragraph-end
796+
- include: list-math-blocks
805797
- include: inlines
806798

807799
list-paragraph-end:
@@ -937,6 +929,7 @@ contexts:
937929
- meta_scope: meta.paragraph.markdown
938930
- include: paragraph-fail
939931
- include: paragraph-end
932+
- include: math-blocks
940933
- include: inlines
941934

942935
paragraph-end:
@@ -2615,7 +2608,6 @@ contexts:
26152608
| {{fenced_code_block_start}} # a fenced codeblock begins the line
26162609
| {{thematic_break}} # line is a thematic beak
26172610
| {{list_item}} # a list item begins the line
2618-
| {{math_block}} # a math block begins the line
26192611
| {{html_block}} # a html block begins the line
26202612
)
26212613
)
@@ -3638,26 +3630,144 @@ contexts:
36383630
- include: literals
36393631
- include: links
36403632

3641-
###[ EXTENSIONS: LATEX ]######################################################
3633+
###[ EXTENSIONS: MATHJAX ]####################################################
36423634

3643-
math-blocks:
3644-
- match: '[ \t]*(\$\$)'
3635+
list-math-blocks:
3636+
- match: (?=(?:^[ \t]*)?\$\$)
3637+
branch_point: list-math-blocks
3638+
branch:
3639+
- list-math-block-begin
3640+
- list-math-inline-block-begin
3641+
3642+
list-math-block-begin:
3643+
# Leading and trailing whitespace of standalone math blocks are scoped
3644+
# `markup.math` in case background color is assigned.
3645+
- meta_include_prototype: false
3646+
- match: ^[ \t]*(\$\$)
36453647
captures:
3646-
1: punctuation.definition.math.begin.markdown
3647-
push: math-block-content
3648+
1: text.tex.latex.embedded.markdown
3649+
meta.environment.math.block.dollar.latex
3650+
punctuation.definition.math.begin.latex
3651+
set:
3652+
- list-math-block-end
3653+
- list-math-block-content
3654+
- match: ''
3655+
fail: list-math-blocks
36483656

3649-
math-block-content:
3657+
list-math-block-end:
3658+
- meta_include_prototype: false
3659+
- meta_scope: markup.math.block.markdown
3660+
- meta_content_scope:
3661+
text.tex.latex.embedded.markdown
3662+
meta.environment.math.block.dollar.latex
3663+
- match: (\$\$)\s*$\n?
3664+
captures:
3665+
1: text.tex.latex.embedded.markdown
3666+
meta.environment.math.block.dollar.latex
3667+
punctuation.definition.math.end.latex
3668+
pop: 1
3669+
- match: ''
3670+
fail: list-math-blocks
3671+
3672+
list-math-inline-block-begin:
3673+
# Math blocks preceeded or followed by normal text are treated as inline raw
3674+
# code blocks, scoped `markup.math` excluding leading or trailing whitespace.
3675+
- meta_include_prototype: false
3676+
- match: \$\$
3677+
scope: punctuation.definition.math.begin.latex
3678+
set:
3679+
- list-math-inline-block-end
3680+
- list-math-block-content
3681+
3682+
list-math-inline-block-end:
3683+
- meta_include_prototype: false
36503684
- meta_scope:
36513685
markup.math.block.markdown
36523686
text.tex.latex.embedded.markdown
36533687
meta.environment.math.block.dollar.latex
3654-
- match: (\$\$)(?:\s*\n)?
3688+
- match: \$\$
3689+
scope: punctuation.definition.math.end.latex
3690+
pop: 1
3691+
- match: ''
3692+
fail: list-math-blocks
3693+
3694+
list-math-block-content:
3695+
- meta_include_prototype: false
3696+
- match: ''
3697+
embed: math-content
3698+
escape: (?=\$\$)|{{list_paragraph_end}}|{{list_setext_heading_escape}}
3699+
pop: 1 # embed math once
3700+
3701+
math-blocks:
3702+
- match: (?=(?:^[ ]{,3})?\$\$)
3703+
branch_point: math-blocks
3704+
branch:
3705+
- math-block-begin
3706+
- math-inline-block-begin
3707+
3708+
math-block-begin:
3709+
# Leading and trailing whitespace of standalone math blocks are scoped
3710+
# `markup.math` in case background color is assigned.
3711+
- meta_include_prototype: false
3712+
- match: ^[ ]{,3}(\$\$)
3713+
captures:
3714+
1: text.tex.latex.embedded.markdown
3715+
meta.environment.math.block.dollar.latex
3716+
punctuation.definition.math.begin.latex
3717+
set:
3718+
- math-block-end
3719+
- math-block-content
3720+
- match: ''
3721+
fail: math-blocks
3722+
3723+
math-block-end:
3724+
- meta_include_prototype: false
3725+
- meta_scope: markup.math.block.markdown
3726+
- meta_content_scope:
3727+
text.tex.latex.embedded.markdown
3728+
meta.environment.math.block.dollar.latex
3729+
- match: (\$\$)\s*$\n?
36553730
captures:
3656-
1: punctuation.definition.math.end.markdown
3731+
1: text.tex.latex.embedded.markdown
3732+
meta.environment.math.block.dollar.latex
3733+
punctuation.definition.math.end.latex
36573734
pop: 1
3658-
- include: math-content
3735+
- match: ''
3736+
fail: math-blocks
3737+
3738+
math-inline-block-begin:
3739+
# Math blocks preceeded or followed by normal text are treated as inline raw
3740+
# code blocks, scoped `markup.math` excluding leading or trailing whitespace.
3741+
- meta_include_prototype: false
3742+
- match: \$\$
3743+
scope: punctuation.definition.math.begin.latex
3744+
set:
3745+
- math-inline-blocks-end
3746+
- math-block-content
3747+
3748+
math-inline-blocks-end:
3749+
- meta_include_prototype: false
3750+
- meta_scope:
3751+
markup.math.block.markdown
3752+
text.tex.latex.embedded.markdown
3753+
meta.environment.math.block.dollar.latex
3754+
- match: \$\$
3755+
scope: punctuation.definition.math.end.latex
3756+
pop: 1
3757+
- match: ''
3758+
fail: math-blocks
3759+
3760+
math-block-content:
3761+
- meta_include_prototype: false
3762+
- match: ''
3763+
embed: math-content
3764+
escape: (?=\$\$)|{{paragraph_end}}|{{setext_heading_escape}}
3765+
pop: 1 # embed math once
36593766

36603767
math-inline:
3768+
# Consume any remaining $$ to prevent deadlock
3769+
# (workaround for sublimehq/sublime_text/issues/5415)
3770+
- match: \$\$
36613771
- match: |-
36623772
(?x)
36633773
# dollar sign not followed by whitespace

0 commit comments

Comments
 (0)