Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Rocket.Chat
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RocketChat
Rocket.Chat
Commits
36a5b082
Commit
36a5b082
authored
8 years ago
by
Gabriel Engel
Browse files
Options
Downloads
Plain Diff
Merge pull request #3114 from pitamar/develop
Don't render empty katex
parents
79ec4278
2f321078
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/rocketchat-katex/katex.coffee
+19
-14
19 additions, 14 deletions
packages/rocketchat-katex/katex.coffee
with
19 additions
and
14 deletions
packages/rocketchat-katex/katex.coffee
+
19
−
14
View file @
36a5b082
...
...
@@ -11,10 +11,9 @@ class Katex
{
opener
:
'$'
,
closer
:
'$'
,
displayMode
:
false
},
]
# Searches for the first opening delimiter in the string
find_opening_delimiter
:
(
str
)
->
# Search the string for each opening delimiter
matches
=
({
options
:
o
,
pos
:
str
.
indexOf
(
o
.
opener
)}
for
o
in
@
delimiters_map
)
# Searches for the first opening delimiter in the string from a given position
find_opening_delimiter
:
(
str
,
start
)
->
# Search the string for each opening delimiter
matches
=
({
options
:
o
,
pos
:
str
.
indexOf
(
o
.
opener
,
start
)}
for
o
in
@
delimiters_map
)
positions
=
(
m
.
pos
for
m
in
matches
when
m
.
pos
>=
0
)
# No opening delimiters were found
...
...
@@ -33,6 +32,9 @@ class Katex
length
:
()
->
return
@
end
-
@
start
extract
:
(
str
)
->
return
str
.
substr
@
start
,
@
length
()
# Returns the outer and inner boundaries of the latex block starting
# at the given opening delimiter
get_latex_boundaries
:
(
str
,
opening_delimiter_match
)
->
...
...
@@ -60,14 +62,16 @@ class Katex
# Searches for the first latex block in the given string
find_latex
:
(
str
)
->
unless
str
.
length
and
(
opening_delimiter_match
=
@
find_opening_delimiter
str
)
?
return
null
match
=
@
get_latex_boundaries
str
,
opening_delimiter_match
start
=
0
while
(
opening_delimiter_match
=
@
find_opening_delimiter
str
,
start
++
)
?
match
?
.
options
=
opening_delimiter_match
.
options
match
=
@
get_latex_boundaries
str
,
opening_delimiter_match
return
match
if
match
?
.
inner
.
extract
(
str
).
trim
().
length
match
.
options
=
opening_delimiter_match
.
options
return
match
return
null
# Breaks a message to what comes before, after and to the content of a
# matched latex block
...
...
@@ -75,7 +79,7 @@ class Katex
before
=
str
.
substr
0
,
match
.
outer
.
start
after
=
str
.
substr
match
.
outer
.
end
latex
=
str
.
substr
match
.
inner
.
start
,
match
.
inner
.
length
()
latex
=
match
.
inner
.
extract
str
latex
=
s
.
unescapeHTML
latex
return
{
before
:
before
,
latex
:
latex
,
after
:
after
}
...
...
@@ -90,17 +94,18 @@ class Katex
rendered
=
"<div class=
\"
katex-error katex-
#{
display_mode
}
-error
\"
>"
rendered
+=
"
#{
e
.
message
}
"
rendered
+=
"</div>"
return
rendered
# Takes a string and renders all latex blocks inside it
render
:
(
str
)
->
result
=
''
while
true
loop
# Find the first latex block in the string
match
=
@
find_latex
str
unless
match
?
result
+=
str
break
...
...
@@ -111,7 +116,7 @@ class Katex
# the rendered latex content
rendered
=
@
render_latex
parts
.
latex
,
match
.
options
.
displayMode
result
+=
parts
.
before
+
rendered
# Set what comes after the latex block to be examined next
str
=
parts
.
after
...
...
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