Skip to content

Commit ecf6f60

Browse files
committed
Handle KaTeX parse errors
1 parent de5d16f commit ecf6f60

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

build.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,22 +284,19 @@ object LaikaCustomizations {
284284
val spanDirectives = Seq(
285285
SpanDirectives.create("math") {
286286
import SpanDirectives.dsl.*
287-
rawBody.map { body =>
288-
RawContent(
289-
NonEmptySet.of("html", "rss"),
290-
KaTeX(body, false)
287+
rawBody.evalMap { body =>
288+
KaTeX(body, false).map(katexStr =>
289+
RawContent(NonEmptySet.of("html", "rss"), katexStr)
291290
)
292291
}
293292
}
294293
)
295294
val blockDirectives = Seq(
296295
BlockDirectives.create("math") {
297296
import BlockDirectives.dsl.*
298-
rawBody.map { body =>
299-
RawContent(
300-
NonEmptySet.of("html", "rss"),
301-
KaTeX(body, true),
302-
Styles("bulma-has-text-centered")
297+
rawBody.evalMap { body =>
298+
KaTeX(body, true).map(katexStr =>
299+
RawContent(NonEmptySet.of("html", "rss"), katexStr, Styles("bulma-has-text-centered"))
303300
)
304301
}
305302
},
@@ -438,14 +435,18 @@ object KaTeX {
438435
ctx.getBindings("js").getMember("katex")
439436
}
440437

441-
def apply(latex: String, displayMode: Boolean = false): String =
438+
def apply(latex: String, displayMode: Boolean = false): Either[String, String] =
442439
synchronized {
443440
val options = Map(
444441
"throwOnError" -> true,
445442
"strict" -> true,
446443
"displayMode" -> displayMode
447-
)
448-
katex.invokeMember("renderToString", latex, options.asJava).asString
444+
).asJava
445+
try {
446+
Right(katex.invokeMember("renderToString", latex, options).asString)
447+
} catch {
448+
case ex: Exception => Left(ex.getMessage)
449+
}
449450
}
450451

451452
}

0 commit comments

Comments
 (0)