Skip to content

Commit 5d9fd9a

Browse files
authored
Merge pull request #635 from typelevel/webjar-helper
2 parents 5251c66 + 70c4f9d commit 5d9fd9a

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

build.scala

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,9 @@ object LaikaCustomizations {
405405
}
406406

407407
val Icons = {
408-
def loadFaIcon(prefix: String, name: String) = {
409-
val resourcePath =
410-
"/META-INF/resources/webjars/fortawesome__fontawesome-free/7.2.0"
411-
val inputStream =
412-
getClass.getResourceAsStream(s"$resourcePath/svgs/$prefix/$name.svg")
413-
String(inputStream.readAllBytes())
414-
}
408+
val fa = WebJar("fortawesome__fontawesome-free")
409+
def loadFaIcon(prefix: String, name: String) =
410+
fa.load(s"svgs/$prefix/$name.svg")
415411

416412
Map(
417413
// brands
@@ -439,11 +435,9 @@ object KaTeX {
439435
import org.graalvm.polyglot.*
440436
import scala.jdk.CollectionConverters.*
441437

442-
private def loadKaTeX(): String = {
443-
val resourcePath = "/META-INF/resources/webjars/katex/0.16.44/dist/katex.js"
444-
val inputStream = getClass.getResourceAsStream(resourcePath)
445-
String(inputStream.readAllBytes())
446-
}
438+
private val katexJar = WebJar("katex")
439+
440+
private def loadKaTeX(): String = katexJar.load("dist/katex.js")
447441

448442
private lazy val katex = {
449443
val ctx = Context
@@ -476,6 +470,33 @@ object KaTeX {
476470

477471
}
478472

473+
// Provides access to resources of a bundled WebJar.
474+
class WebJar(artifactId: String) {
475+
import java.io.FileNotFoundException
476+
private val classLoader = classOf[WebJar].getClassLoader
477+
478+
private val version: String =
479+
val propsPath = s"META-INF/maven/org.webjars.npm/$artifactId/pom.properties"
480+
val stream = classLoader.getResourceAsStream(propsPath)
481+
if stream == null then
482+
throw FileNotFoundException(
483+
s"Could not find pom.properties for webjar '$artifactId' on the classpath."
484+
)
485+
val props = java.util.Properties()
486+
props.load(stream)
487+
props.getProperty("version")
488+
489+
// Load a resource from this WebJar as a String, uses relative paths.
490+
def load(path: String): String =
491+
val resourcePath = s"META-INF/resources/webjars/$artifactId/$version/$path"
492+
val stream = classLoader.getResourceAsStream(resourcePath)
493+
if stream == null then
494+
throw FileNotFoundException(
495+
s"Could not find resource '$path' in webjar '$artifactId' ($version)."
496+
)
497+
String(stream.readAllBytes())
498+
}
499+
479500
object Redirects {
480501
import laika.io.model.InputTree
481502
import laika.ast.Path.Root

0 commit comments

Comments
 (0)