Skip to content

Commit 2084139

Browse files
authored
Merge pull request #623 from typelevel/directive-errors
Better error messages on directive fails
2 parents a2cd653 + 72cbc57 commit 2084139

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

build.scala

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ object LaikaCustomizations {
272272
},
273273
TemplateDirectives.create("svg") {
274274
import TemplateDirectives.dsl.*
275-
attribute(0).as[String].map { icon =>
276-
TemplateElement(
277-
RawContent(NonEmptySet.of("html", "rss"), Icons(icon))
278-
)
275+
attribute(0).as[String].evalMap { icon =>
276+
Icons.get(icon).toRight(s"Unknown SVG icon '$icon'").map { svg =>
277+
TemplateElement(RawContent(NonEmptySet.of("html", "rss"), svg))
278+
}
279279
}
280280
}
281281
)
@@ -284,25 +284,31 @@ object LaikaCustomizations {
284284
val spanDirectives = Seq(
285285
SpanDirectives.create("math") {
286286
import SpanDirectives.dsl.*
287-
rawBody.map { body =>
288-
SpanSequence(
289-
RawContent(NonEmptySet.of("html"), KaTeX(body, false)),
290-
RawContent(NonEmptySet.of("rss"), KaTeX(body, false, "mathml"))
287+
rawBody.evalMap { body =>
288+
(KaTeX.render(body, false), KaTeX.render(body, false, "mathml")).mapN(
289+
(katexStr, mathmlStr) =>
290+
SpanSequence(
291+
RawContent(NonEmptySet.of("html"), katexStr),
292+
RawContent(NonEmptySet.of("rss"), mathmlStr)
293+
)
291294
)
292295
}
293296
}
294297
)
295298
val blockDirectives = Seq(
296299
BlockDirectives.create("math") {
297300
import BlockDirectives.dsl.*
298-
rawBody.map { body =>
299-
BlockSequence(
300-
RawContent(
301-
NonEmptySet.of("html"),
302-
KaTeX(body, true),
303-
Styles("bulma-has-text-centered")
304-
),
305-
RawContent(NonEmptySet.of("rss"), KaTeX(body, true, "mathml"))
301+
rawBody.evalMap { body =>
302+
(KaTeX.render(body, true), KaTeX.render(body, true, "mathml")).mapN(
303+
(katexStr, mathmlStr) =>
304+
BlockSequence(
305+
RawContent(
306+
NonEmptySet.of("html", "rss"),
307+
katexStr,
308+
Styles("bulma-has-text-centered")
309+
),
310+
RawContent(NonEmptySet.of("rss"), mathmlStr)
311+
)
306312
)
307313
}
308314
},
@@ -448,20 +454,24 @@ object KaTeX {
448454
ctx.getBindings("js").getMember("katex")
449455
}
450456

451-
def apply(
457+
def render(
452458
latex: String,
453459
displayMode: Boolean = false,
454460
output: String = "htmlAndMathml"
455-
): String =
461+
): Either[String, String] =
456462
synchronized {
457463
// https://katex.org/docs/options
458464
val options = Map(
459465
"throwOnError" -> true,
460466
"strict" -> true,
461467
"displayMode" -> displayMode,
462468
"output" -> output
463-
)
464-
katex.invokeMember("renderToString", latex, options.asJava).asString
469+
).asJava
470+
try {
471+
Right(katex.invokeMember("renderToString", latex, options).asString)
472+
} catch {
473+
case ex: Exception => Left(ex.getMessage)
474+
}
465475
}
466476

467477
}

0 commit comments

Comments
 (0)