Skip to content

Commit 9a9fcb2

Browse files
committed
Syntax: Improve accuracy of inline MathJAX expressions
This commit ... 1. prevents highlighting mathjax like expressions which are intersected by html tags 2. embeds `math-content` to handle incomplete latex expressions better. We don't want to leak latex syntax up to the end of file if embedded latex expressions don't pop all contexts off stack before final `$`.
1 parent ab62f4b commit 9a9fcb2

3 files changed

Lines changed: 124 additions & 24 deletions

File tree

messages/3.1.4.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ feedback you can use [GitHub issues][issues].
1414

1515
## Changes
1616

17-
* use lower case check mark completed gfm tasks `- [x] task` (#696)
17+
* Use lower case check mark completed gfm tasks `- [x] task` (#696)
18+
* Improve accuracy of (incomplete) inline MathJAX ($1+\srqt{b$) expressions
1819

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

syntaxes/Markdown.sublime-syntax

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ variables:
296296
| \B \* {{no_space_but_punct}}
297297
)
298298
299+
no_escape_behind: (?<![^\\]\\)(?<![\\]{3})
299300
# not followed by Unicode whitespace and not followed by a Unicode punctuation character
300301
no_space_nor_punct: (?![\s*\p{P}])
301302
# not followed by Unicode whitespace and followed by a Unicode punctuation character
@@ -3659,23 +3660,27 @@ contexts:
36593660
math-inline:
36603661
- match: |-
36613662
(?x)
3662-
(\$)(?=\S)
3663-
(?=
3664-
(?: \\\\ | \\\$ | [^\$] )*?
3665-
\S\$(?:[^a-zA-Z0-9]|$)
3666-
)
3667-
scope: punctuation.definition.math.begin.markdown
3668-
push: math-inline-content
3669-
3670-
math-inline-content:
3671-
- meta_scope:
3663+
# dollar sign not followed by whitespace
3664+
(\$) (?=\S)
3665+
# arbritary number of chars, maybe escaped dollar sign, but no html tags
3666+
# terminated by non-whitespace, followed by dollar and non-word characther
3667+
(?= (?: \\\\ | \\\$ | (?!</?[A-Za-z]+[ \t]*/?>)[^\$] )*? \S\$\W )
3668+
scope:
36723669
markup.math.inline.markdown
36733670
text.tex.latex.embedded.markdown
36743671
meta.environment.math.block.dollar.latex
3675-
- match: \$
3676-
scope: punctuation.definition.math.end.markdown
3677-
pop: 1
3678-
- include: math-content
3672+
punctuation.definition.math.begin.latex
3673+
embed: math-content
3674+
embed_scope:
3675+
markup.math.inline.markdown
3676+
text.tex.latex.embedded.markdown
3677+
meta.environment.math.block.dollar.latex
3678+
escape: '{{no_escape_behind}}\$'
3679+
escape_captures:
3680+
0: markup.math.inline.markdown
3681+
text.tex.latex.embedded.markdown
3682+
meta.environment.math.block.dollar.latex
3683+
punctuation.definition.math.end.latex
36793684
36803685
math-content:
36813686
- include: scope:text.tex.latex#macros

tests/syntax_test_markdown.md

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7903,15 +7903,7 @@ This is a [[wiki link]].
79037903
| ^^ punctuation.definition.link.end.markdown
79047904
79057905
7906-
# TEST: LATEX EQUATIONS #######################################################
7907-
7908-
This is math $1+1$ expression.
7909-
| ^^^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7910-
| ^ punctuation.definition.math.begin.markdown
7911-
| ^ constant.numeric.math.tex
7912-
| ^ keyword.operator.math.tex
7913-
| ^ constant.numeric.math.tex
7914-
| ^ punctuation.definition.math.end.markdown
7906+
# TEST: MATHJAX BLOCKS MARKUP #################################################
79157907
79167908
$$
79177909
| <- markup.math.block.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex punctuation.definition.math.begin.markdown
@@ -7961,3 +7953,105 @@ $$
79617953
|^^^^^^^^ markup.list.numbered.markdown markup.math.block.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
79627954
|^ punctuation.definition.math.begin.markdown
79637955
| ^^ punctuation.definition.math.end.markdown
7956+
7957+
# TEST: MATHJAX INLINE MARKUP #################################################
7958+
7959+
# Math $1+1$ atx heading
7960+
| <- markup.heading.1.markdown punctuation.definition.heading.begin.markdown
7961+
|^^^^^^^^^^^^^^^^^^^^^^^^ markup.heading.1.markdown
7962+
| ^^^^^ markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7963+
7964+
Math $1+1$ setext heading
7965+
| <- markup.heading.1.markdown
7966+
|^^^^^^^^^^^^^^^^^^^^^^^^^ markup.heading.1.markdown
7967+
| ^^^^^ markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7968+
===
7969+
| <- markup.heading.1.markdown punctuation.definition.heading.setext.markdown
7970+
7971+
Math $1+1$ setext heading
7972+
| <- markup.heading.2.markdown
7973+
|^^^^^^^^^^^^^^^^^^^^^^^^^ markup.heading.2.markdown
7974+
| ^^^^^ markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7975+
---
7976+
| <- markup.heading.2.markdown punctuation.definition.heading.setext.markdown
7977+
7978+
This is math $1+1$ expression, but $ 1+1 $ ,$ 1+1$, $1+1 $ and 1+1$ or $1+1 are not.
7979+
| ^^^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7980+
| ^ punctuation.definition.math.begin.latex
7981+
| ^ constant.numeric.math.tex
7982+
| ^ keyword.operator.math.tex
7983+
| ^ constant.numeric.math.tex
7984+
| ^ punctuation.definition.math.end.latex
7985+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.paragraph.markdown - markup.math
7986+
7987+
Math with -$1$() $2$-$3$a or $4$_
7988+
| ^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7989+
| ^^^ meta.paragraph.markdown - markup.math
7990+
| ^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7991+
| ^^^^^^^^^^^^^^ meta.paragraph.markdown - markup.math
7992+
7993+
Use `\$` to display a dollar sign: $\sqrt{\$4}$
7994+
| ^^^^ meta.paragraph.markdown markup.raw.inline.markdown
7995+
| ^^^^^^^^^^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
7996+
| ^ punctuation.definition.math.begin.latex
7997+
| ^^^^^ support.function.math.tex
7998+
| ^ punctuation.definition.group
7999+
| ^^ constant.character.escape
8000+
| ^ constant.numeric
8001+
| ^ punctuation.definition
8002+
| ^ punctuation.definition.math.end.latex
8003+
8004+
No math <span>$</span>1+1$ or $1+1<span>$</span>
8005+
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.paragraph.markdown - markup.math
8006+
8007+
No math $<br>1+1$ or $1+1<br>$
8008+
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.paragraph.markdown - markup.math
8009+
8010+
Math $a<b$ but $a<b>a$ $a <b 10$ text
8011+
| ^^^^^ markup.math.inline.markdown
8012+
| ^^^^^^^^^^^^^ - markup.math
8013+
| ^^^^^^^^^ markup.math.inline.markdown
8014+
| ^^^^^^ - markup.math - meta.tag
8015+
8016+
This is no $1+
8017+
b$ math.
8018+
| <- meta.paragraph.markdown - markup.math
8019+
|^^^^^^^^ meta.paragraph.markdown - markup.math
8020+
8021+
Handle incomplete $\sqrt{b$ expressions well.
8022+
| ^^^^^^^^^ meta.paragraph.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
8023+
| ^ punctuation.definition.math.begin.latex
8024+
| ^^^^^ support.function.math.tex
8025+
| ^^ meta.group.brace.latex
8026+
| ^ punctuation.definition.math.end.latex - meta.group
8027+
| ^ meta.paragraph.markdown - markup.math
8028+
8029+
1. ordered list $\sqrt{b}$ equation $1+
8030+
| ^^^^^^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
8031+
| ^^^^^^^^^^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown - markup.math
8032+
8033+
Math $1+1$ in paragraph.
8034+
| ^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
8035+
8036+
No math <span>$</span>1+1$ or $1+1<span>$</span>
8037+
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown - markup.math
8038+
8039+
No math $<br>1+1$ or $1+1<br>$
8040+
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown - markup.math
8041+
8042+
Math $a<b$ but $a<b>a$ $a <b 10$ text
8043+
| ^^^^^ markup.math.inline.markdown
8044+
| ^^^^^^^^^^^^^ - markup.math
8045+
| ^^^^^^^^^ markup.math.inline.markdown
8046+
| ^^^^^^ - markup.math - meta.tag
8047+
8048+
+ unordered $\sqrt{b}$ equation
8049+
| ^^^^^^^^^^ markup.list.numbered.markdown meta.paragraph.list.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
8050+
8051+
Handle incomplete $\sqrt{b$ expressions well.
8052+
| ^^^^^^^^^ meta.paragraph.list.markdown markup.math.inline.markdown text.tex.latex.embedded.markdown meta.environment.math.block.dollar.latex
8053+
| ^ punctuation.definition.math.begin.latex
8054+
| ^^^^^ support.function.math.tex
8055+
| ^^ meta.group.brace.latex
8056+
| ^ punctuation.definition.math.end.latex - meta.group
8057+
| ^ meta.paragraph.list.markdown - markup.math

0 commit comments

Comments
 (0)