-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
299 lines (248 loc) · 8.53 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import com.varabyte.kobweb.common.text.ensureSurrounded
import com.varabyte.kobweb.common.text.splitCamelCase
import com.varabyte.kobweb.gradle.application.util.configAsKobwebApplication
import com.varabyte.kobwebx.gradle.markdown.children
import kotlinx.html.*
import org.commonmark.node.Text
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import java.net.HttpURLConnection
import java.net.URI
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.jetbrains.compose)
alias(libs.plugins.kobweb.application)
alias(libs.plugins.kobwebx.markdown)
alias(libs.plugins.kotlinx.serialization)
}
group = "io.github.ayfri"
version = "1.0-SNAPSHOT"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
maven("https://us-central1-maven.pkg.dev/varabyte-repos/public")
}
fun HEAD.meta(property: String, content: String) {
meta {
attributes["property"] = property
this.content = content
}
}
operator fun <K : Any, V : Any> MapProperty<K, V>.set(key: K, value: V) {
put(key, value)
}
operator fun <K : Any, V : Any> MapProperty<K, V>.get(key: K) = getting(key)
val blogInputDir = layout.projectDirectory.dir("src/jsMain/resources/markdown/articles")
val downloadDataTask = tasks.register("downloadData") {
val file = layout.buildDirectory.file("generated/ayfri/src/jsMain/kotlin/io/github/ayfri/data/Data.kt").get().asFile
outputs.dir(file.parentFile)
doLast {
val dataLink = "https://raw.githubusercontent.com/Ayfri/Portfolio/api/result.json"
val url = URI(dataLink).toURL()
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.connect()
val responseCode = connection.responseCode
if (responseCode != 200) {
throw Exception("Error while downloading data, response code: $responseCode")
}
var data = connection.inputStream.readBytes().decodeToString()
data = data.replace(Regex("\\$"), "\\\${\"\\$\"}")
val tripleQuotes = "\"\"\""
val kotlinOutput = """
package io.github.ayfri.data
const val rawData = $tripleQuotes$data$tripleQuotes
""".trimIndent()
file.parentFile.mkdirs()
file.writeText(kotlinOutput)
logger.lifecycle("Generated '$file'")
}
}
data class BlogEntry(
val file: File,
val date: String,
val title: String,
val desc: String,
val navTitle: String,
val keywords: List<String>,
val dateModified: String,
)
fun String.escapeQuotes() = this.replace("\"", "\\\"")
kobweb {
markdown {
routeOverride = { route ->
"/articles/${route.splitCamelCase().joinToString("-") { word -> word.lowercase() }}/index"
}
handlers {
img.set { image ->
val altText = image.children()
.filterIsInstance<Text>()
.map { it.literal.escapeSingleQuotedText() }
.joinToString("")
this.childrenOverride = emptyList()
"""org.jetbrains.compose.web.dom.Img(src="${image.destination}", alt="$altText") {
| attr("loading", "lazy")
| attr("decoding", "async")
|}
""".trimMargin()
}
code.set { code ->
val text = "\"\"\"${code.literal.escapeTripleQuotedText()}\"\"\""
"""io.github.ayfri.components.CodeBlock($text, "${code.info.takeIf { it.isNotBlank() }}")"""
}
}
process = { markdownFile ->
val blogEntries = mutableListOf<BlogEntry>()
markdownFile.forEach { entry ->
val path = File(entry.filePath)
val fileName = path.name
val fm = entry.frontMatter
val requiredFields = listOf("title", "description", "date-created", "date-modified", "nav-title")
val title = fm["title"]?.firstOrNull()
val desc = fm["description"]?.firstOrNull()
val dateCreated = fm["date-created"]?.firstOrNull()
val dateModified = fm["date-modified"]?.firstOrNull()
val navTitle = fm["nav-title"]?.firstOrNull()
if (title == null || desc == null || dateCreated == null || dateModified == null || navTitle == null) {
println("Skipping '$fileName', missing required fields in front matter of $fileName: ${requiredFields.filter { fm[it] == null }}")
return@forEach
}
val keywords = fm["keywords"]?.firstOrNull()?.split(Regex(",\\s*")) ?: emptyList()
// Dates are only formatted in this format "2023-11-13"
val dateCreatedComplete = dateCreated.split("-").let { (year, month, day) ->
"$year-$month-${day}T00:00:00.000000000+01:00"
}
val dateModifiedComplete = dateModified.split("-").let { (year, month, day) ->
"$year-$month-${day}T00:00:00.000000000+01:00"
}
blogEntries.add(
BlogEntry(
file = path,
date = dateCreatedComplete,
title = title,
desc = desc,
navTitle = navTitle,
keywords = keywords,
dateModified = dateModifiedComplete
)
)
}
generateKotlin("$group/articles.kt", buildString {
appendLine(
"""
|// This file is generated. Modify the build script if you need to change it.
|
|package io.github.ayfri
|
|import io.github.ayfri.components.ArticleEntry
|
|val articlesEntries = listOf${if (blogEntries.isEmpty()) "<ArticleEntry>" else ""}(
""".trimMargin()
)
fun List<String>.asCode() = "listOf(${joinToString { "\"$it\"" }})"
blogEntries.sortedByDescending { it.date }.forEach { entry ->
appendLine(
""" ArticleEntry("/articles/${
entry.file.nameWithoutExtension
.splitCamelCase()
.joinToString("-") { word -> word.lowercase() }
.ensureSurrounded("", "/")
}", "${entry.date}", "${entry.title.escapeQuotes()}", "${entry.desc.escapeQuotes()}", "${entry.navTitle.escapeQuotes()}", ${
entry.keywords.asCode()
}, "${entry.dateModified}"),
""".trimMargin()
)
}
appendLine(")")
})
}
}
app {
export {
includeSourceMap = false
}
index {
val url = "https://ayfri.com"
val author = "Pierre Roy"
val twitterHandle = "@Ayfri_"
val description = """
Hi, I'm Pierre Roy, an IT student, and I'm passionate about computer science and especially programming.
Discover my projects and my blog on this website.
""".trimIndent()
val image = "$url/images/avatar.webp"
globals["author"] = author
globals["description"] = description
globals["url"] = url
this.description = description
head.apply {
add {
meta(charset = "utf-8")
meta(name = "viewport", content = "width=device-width, initial-scale=1.0")
meta(name = "Author", content = author)
meta(property = "og:description", content = description)
meta(property = "og:image", content = image)
meta(property = "og:type", content = "website")
meta(property = "og:url", content = url)
meta(property = "twitter:card", content = "summary")
meta(property = "twitter:creator", content = twitterHandle)
meta(property = "twitter:description", content = description)
meta(property = "twitter:image", content = image)
meta(property = "twitter:site", content = twitterHandle)
link(href = "https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;500;700&display=swap", rel = "stylesheet")
link(href = "https://dev-cats.github.io/code-snippets/JetBrainsMono.css", rel = "stylesheet")
link(href = "/prism.min.css", rel = "stylesheet")
script(src = "/prism.min.js", type = "text/javascript") {
attributes += "data-manual" to ""
}
script(src = "https://kit.fontawesome.com/74fed0e2b5.js", type = "text/javascript") {
async = true
}
script(src = "https://www.googletagmanager.com/gtag/js?id=G-TS3BHPVFKK", type = "text/javascript") {
defer = true
}
script(type = "text/javascript") {
unsafe {
raw(
"""
function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag('js',new Date),gtag('config','G-TS3BHPVFKK')
""".trimIndent()
)
}
}
}
}
}
}
}
kotlin {
configAsKobwebApplication("portfolio")
js(IR) {
browser {
commonWebpackConfig {
val isDev = project.findProperty("kobwebEnv") == "DEV"
sourceMaps = isDev
devServer?.open = false
}
}
binaries.executable()
}
sourceSets {
jsMain {
kotlin.srcDir(downloadDataTask)
dependencies {
implementation(compose.html.core)
implementation(compose.runtime)
implementation(libs.kobwebx.markdown)
implementation(libs.kobweb.core)
implementation(libs.kotlinx.wrappers.browser)
implementation(libs.kotlinx.serialization.json)
implementation(npm("marked", project.extra["npm.marked.version"].toString()))
}
}
}
}
tasks.withType<KotlinJsCompile>().configureEach {
kotlinOptions.freeCompilerArgs += listOf(
"-Xklib-enable-signature-clash-checks=false",
)
}