Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ object LaikaCustomizations {
},
TemplateDirectives.create("svg") {
import TemplateDirectives.dsl.*
attribute(0).as[String].map { icon =>
TemplateElement(
RawContent(NonEmptySet.of("html", "rss"), Icons(icon))
)
attribute(0).as[String].evalMap { icon =>
Icons.get(icon).toRight(s"Unknown SVG icon '$icon'").map { svg =>
TemplateElement(RawContent(NonEmptySet.of("html", "rss"), svg))
}
}
}
)
Expand All @@ -284,25 +284,31 @@ object LaikaCustomizations {
val spanDirectives = Seq(
SpanDirectives.create("math") {
import SpanDirectives.dsl.*
rawBody.map { body =>
SpanSequence(
RawContent(NonEmptySet.of("html"), KaTeX(body, false)),
RawContent(NonEmptySet.of("rss"), KaTeX(body, false, "mathml"))
rawBody.evalMap { body =>
(KaTeX.render(body, false), KaTeX.render(body, false, "mathml")).mapN(
(katexStr, mathmlStr) =>
SpanSequence(
RawContent(NonEmptySet.of("html"), katexStr),
RawContent(NonEmptySet.of("rss"), mathmlStr)
)
)
}
}
)
val blockDirectives = Seq(
BlockDirectives.create("math") {
import BlockDirectives.dsl.*
rawBody.map { body =>
BlockSequence(
RawContent(
NonEmptySet.of("html"),
KaTeX(body, true),
Styles("bulma-has-text-centered")
),
RawContent(NonEmptySet.of("rss"), KaTeX(body, true, "mathml"))
rawBody.evalMap { body =>
(KaTeX.render(body, true), KaTeX.render(body, true, "mathml")).mapN(
(katexStr, mathmlStr) =>
BlockSequence(
RawContent(
NonEmptySet.of("html", "rss"),
katexStr,
Styles("bulma-has-text-centered")
),
RawContent(NonEmptySet.of("rss"), mathmlStr)
)
)
}
},
Expand Down Expand Up @@ -448,20 +454,24 @@ object KaTeX {
ctx.getBindings("js").getMember("katex")
}

def apply(
def render(
latex: String,
displayMode: Boolean = false,
output: String = "htmlAndMathml"
): String =
): Either[String, String] =
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this perhaps no longer be apply and instead be something like parse or render?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd say so -- once it's becoming effectful (which this kind of is), apply doesn't feel quite idiomatically right any more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, renamed to render

synchronized {
// https://katex.org/docs/options
val options = Map(
"throwOnError" -> true,
"strict" -> true,
"displayMode" -> displayMode,
"output" -> output
)
katex.invokeMember("renderToString", latex, options.asJava).asString
).asJava
try {
Right(katex.invokeMember("renderToString", latex, options).asString)
} catch {
case ex: Exception => Left(ex.getMessage)
}
}

}
Expand Down
Loading