Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Fork of markdown-it-katex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Postal Owl
Fork of markdown-it-katex
Commits
fd464f82
Commit
fd464f82
authored
9 years ago
by
Christopher Breeden
Browse files
Options
Downloads
Patches
Plain Diff
Added proper math-mode escaping
parent
97cea231
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
index.js
+25
-20
25 additions, 20 deletions
index.js
test/fixtures/default.txt
+14
-0
14 additions, 0 deletions
test/fixtures/default.txt
with
39 additions
and
20 deletions
index.js
+
25
−
20
View file @
fd464f82
...
...
@@ -40,39 +40,36 @@ function isValidDelim(state, pos) {
}
function
math_inline
(
state
,
silent
)
{
var
start
,
match
,
token
,
res
,
pos
=
state
.
pos
;
var
start
,
match
,
token
,
res
,
pos
,
esc_count
;
if
(
state
.
src
[
state
.
pos
]
!==
"
$
"
)
{
return
false
;
}
res
=
isValidDelim
(
state
,
pos
);
res
=
isValidDelim
(
state
,
state
.
pos
);
if
(
!
res
.
can_open
)
{
if
(
!
silent
)
{
state
.
pending
+=
"
$
"
;
}
state
.
pos
+=
1
;
return
true
;
}
// First check for and bypass all properly escaped delimieters
// This loop will assume that the first leading backtick can not
// be the first character in state.src, which is known since
// we have found an opening delimieter already.
start
=
state
.
pos
+
1
;
match
=
state
.
src
.
indexOf
(
"
$
"
,
start
);
match
=
start
;
while
(
(
match
=
state
.
src
.
indexOf
(
"
$
"
,
match
))
!==
-
1
)
{
// Found potential $, look for escapes, pos will point to
// first non escape when complete
pos
=
match
-
1
;
while
(
state
.
src
[
pos
]
===
"
\\
"
)
{
pos
-=
1
;
}
// No closing delimter found. Consume $ and continue.
if
(
match
===
-
1
)
{
if
(
!
silent
)
{
state
.
pending
+=
"
$
"
;
}
state
.
pos
=
start
;
return
true
;
// Even number of escapes, potential closing delimiter found
if
(
((
match
-
pos
)
%
2
)
==
1
)
{
break
;
}
match
+=
1
;
}
res
=
isValidDelim
(
state
,
match
);
// We only will look at the very next delimeter while searching
// for closing delimeters. As a consequnce, we will never send
// KaTeX a $ inside of math mode, even if escaped. The other alternative
// would otherwise require escaping commonly used things such as
// \int, \sum, etc... Perhaps there is a way to find a better solution.
// Such as counting the number of \\\\\\\$ and if odd escape $ and remove
// and leading \ or otherwise leave as is.
if
(
!
res
.
can_close
)
{
// No closing delimter found. Consume $ and continue.
if
(
match
===
-
1
)
{
if
(
!
silent
)
{
state
.
pending
+=
"
$
"
;
}
state
.
pos
=
start
;
return
true
;
...
...
@@ -85,6 +82,14 @@ function math_inline(state, silent) {
return
true
;
}
// Check for valid closing delimiter
res
=
isValidDelim
(
state
,
match
);
if
(
!
res
.
can_close
)
{
if
(
!
silent
)
{
state
.
pending
+=
"
$
"
;
}
state
.
pos
=
start
;
return
true
;
}
if
(
!
silent
)
{
token
=
state
.
push
(
'
math_inline
'
,
'
math
'
,
0
);
token
.
markup
=
"
$
"
;
...
...
This diff is collapsed.
Click to expand it.
test/fixtures/default.txt
+
14
−
0
View file @
fd464f82
...
...
@@ -207,3 +207,17 @@ It's well know that $$1 + 1 = 3$$ for sufficiently large 1.
.
<p>It's well know that $$1 + 1 = 3$$ for sufficiently large 1.</p>
.
Escaped delimiters in math mode
.
Money adds: $\$X + \$Y = \$Z$.
.
<p>Money adds: <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">$</mi><mi>X</mi><mo>+</mo><mi mathvariant="normal">$</mi><mi>Y</mi><mo>=</mo><mi mathvariant="normal">$</mi><mi>Z</mi></mrow><annotation encoding="application/x-tex">\$X + \$Y = \$Z</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:0.83333em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mbin">+</span><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.22222em;">Y</span><span class="mrel">=</span><span class="mord mathrm">$</span><span class="mord mathit" style="margin-right:0.07153em;">Z</span></span></span></span>.</p>
.
Multiple escaped delimiters in math module
.
Weird-o: $\displaystyle{\begin{pmatrix} \$ & 1\\\$ \end{pmatrix}}$.
.
<p>Weird-o: <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow><mo fence="true">(</mo><mtable><mtr><mtd><mrow><mi mathvariant="normal">$</mi></mrow></mtd><mtd><mrow><mn>1</mn></mrow></mtd></mtr><mtr><mtd><mrow><mi mathvariant="normal">$</mi></mrow></mtd></mtr></mtable><mo fence="true">)</mo></mrow></mrow></mstyle></mrow><annotation encoding="application/x-tex">\displaystyle{\begin{pmatrix} \$ & 1\\\$ \end{pmatrix}}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.45em;"></span><span class="strut bottom" style="height:2.40003em;vertical-align:-0.95003em;"></span><span class="base textstyle uncramped"><span class="reset-textstyle displaystyle textstyle uncramped"><span class="mord displaystyle textstyle uncramped"><span class="minner displaystyle textstyle uncramped"><span class="style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size3">(</span></span><span class="mord"><span class="mtable"><span class="col-align-c"><span class="vlist"><span style="top:-0.6099999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">$</span></span></span><span style="top:0.5900000000000003em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">$</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span></span></span></span><span class="arraycolsep" style="width:0.5em;"></span><span class="arraycolsep" style="width:0.5em;"></span><span class="col-align-c"><span class="vlist"><span style="top:-0.6099999999999999em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span><span class="mord displaystyle textstyle uncramped"><span class="mord mathrm">1</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;"></span></span></span></span></span></span></span><span class="style-wrap reset-textstyle textstyle uncramped" style="top:0em;"><span class="delimsizing size3">)</span></span></span></span></span></span></span></span>.</p>
.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment