-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 42 additions & 39 deletions
81
src/main/kotlin/nl/avisi/structurizr/site/generatr/site/views/CDN.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,66 @@ | ||
package nl.avisi.structurizr.site.generatr.site.views | ||
|
||
import com.structurizr.Workspace | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
import java.lang.IllegalStateException | ||
|
||
class CDN { | ||
companion object { | ||
private const val CDN_BASE = "https://cdn.jsdelivr.net/npm/" | ||
private val json = Json { ignoreUnknownKeys = true } | ||
class CDN(val workspace: Workspace) { | ||
private val cdnBaseURL = getCdnBaseUrl() | ||
private val json = Json { ignoreUnknownKeys = true } | ||
|
||
private val packageJson = object {}.javaClass.getResource("/package.json")?.readText() | ||
?: throw IllegalStateException("package.json not found") | ||
private val packageJson = object {}.javaClass.getResource("/package.json")?.readText() | ||
?: throw IllegalStateException("package.json not found") | ||
|
||
private val dependencies = json.parseToJsonElement(packageJson).jsonObject["dependencies"] | ||
?.jsonObject | ||
?.map { Dependency(it.key, it.value.jsonPrimitive.content) } | ||
?: throw IllegalStateException("dependencies element not found in package.json") | ||
private val dependencies = json.parseToJsonElement(packageJson).jsonObject["dependencies"] | ||
?.jsonObject | ||
?.map { Dependency(cdnBaseURL, it.key, it.value.jsonPrimitive.content) } | ||
?: throw IllegalStateException("dependencies element not found in package.json") | ||
|
||
fun bulmaCss() = dependencies.single { it.name == "bulma" }.let { | ||
"${it.baseUrl()}/css/bulma.min.css" | ||
} | ||
fun bulmaCss() = dependencies.single { it.name == "bulma" }.let { | ||
"${it.baseUrl()}/css/bulma.min.css" | ||
} | ||
|
||
fun katexJs() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.js" | ||
} | ||
fun katexJs() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.js" | ||
} | ||
|
||
fun katexCss() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.css" | ||
} | ||
fun katexCss() = dependencies.single { it.name == "katex" }.let { | ||
"${it.baseUrl()}/dist/katex.min.css" | ||
} | ||
|
||
fun lunrJs() = dependencies.single { it.name == "lunr" }.let { | ||
"${it.baseUrl()}/lunr.min.js" | ||
} | ||
fun lunrJs() = dependencies.single { it.name == "lunr" }.let { | ||
"${it.baseUrl()}/lunr.min.js" | ||
} | ||
|
||
fun lunrLanguagesStemmerJs() = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}/min/lunr.stemmer.support.min.js" | ||
} | ||
fun lunrLanguagesStemmerJs() = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}/min/lunr.stemmer.support.min.js" | ||
} | ||
|
||
fun lunrLanguagesJs(language: String) = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}/min/lunr.$language.min.js" | ||
} | ||
fun lunrLanguagesJs(language: String) = dependencies.single { it.name == "lunr-languages" }.let { | ||
"${it.baseUrl()}/min/lunr.$language.min.js" | ||
} | ||
|
||
fun mermaidJs() = dependencies.single { it.name == "mermaid" }.let { | ||
"${it.baseUrl()}/dist/mermaid.esm.min.mjs" | ||
} | ||
fun mermaidJs() = dependencies.single { it.name == "mermaid" }.let { | ||
"${it.baseUrl()}/dist/mermaid.esm.min.mjs" | ||
} | ||
|
||
fun svgpanzoomJs() = dependencies.single { it.name == "svg-pan-zoom" }.let { | ||
"${it.baseUrl()}/dist/svg-pan-zoom.min.js" | ||
} | ||
fun svgpanzoomJs() = dependencies.single { it.name == "svg-pan-zoom" }.let { | ||
"${it.baseUrl()}/dist/svg-pan-zoom.min.js" | ||
} | ||
|
||
fun webfontloaderJs() = dependencies.single { it.name == "webfontloader" }.let { | ||
"${it.baseUrl()}/webfontloader.js" | ||
} | ||
fun webfontloaderJs() = dependencies.single { it.name == "webfontloader" }.let { | ||
"${it.baseUrl()}/webfontloader.js" | ||
} | ||
|
||
fun getCdnBaseUrl() = workspace.views.configuration.properties | ||
.getOrDefault("generatr.site.cdn", "https://cdn.jsdelivr.net/npm") | ||
.trimEnd('/') | ||
|
||
@Serializable | ||
data class Dependency(val name: String, val version: String) { | ||
fun baseUrl() = "$CDN_BASE$name@$version" | ||
data class Dependency(val cdnBaseURL: String, val name: String, val version: String) { | ||
fun baseUrl() = "$cdnBaseURL/$name@$version" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.