Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/commonMain/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Miley Chandonnet</title>
<link rel="icon" type="image/svg+xml" href="favicon.svg">

<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:site_name" content="Miley Chandonnet">
<meta property="og:title" content="Miley Chandonnet">
<meta property="og:description" content="Native Android Application Engineer">
<meta property="og:url" content="https://miley.codes">
<meta property="og:image" content="https://miley.codes/blog/dark-factory-needs-windows/demo.gif">

<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Miley Chandonnet">
<meta name="twitter:description" content="Native Android Application Engineer">
<meta name="twitter:image" content="https://miley.codes/blog/dark-factory-needs-windows/demo.gif">
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-9MPHTXCPC7"></script>
<script>
Expand Down
56 changes: 52 additions & 4 deletions src/jsMain/kotlin/codes/miley/frontend/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ import kotlinx.browser.document
import kotlinx.browser.window
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import org.w3c.dom.HTMLMetaElement

private fun setMetaTag(attrName: String, attrValue: String, content: String) {
val selector = "meta[$attrName=\"$attrValue\"]"
val element = document.querySelector(selector) as? HTMLMetaElement
if (element != null) {
element.content = content
}
}

private fun updateMetaTags(title: String, description: String, url: String, image: String) {
setMetaTag("property", "og:title", title)
setMetaTag("property", "og:description", description)
setMetaTag("property", "og:url", url)
setMetaTag("property", "og:image", image)
setMetaTag("name", "twitter:title", title)
setMetaTag("name", "twitter:description", description)
setMetaTag("name", "twitter:image", image)
}

private fun trackPageView(route: Map<String, String>) {
val path = when {
Expand Down Expand Up @@ -91,13 +110,42 @@ fun main() {
trackPageView(route)
val page = route["page"]

document.title = when {
when {
page == "blog" && route.containsKey("post") -> {
val post = ALL_POSTS.find { it.id == route["post"] }
post?.title?.let { "$it | Miley Chandonnet" } ?: "Miley Chandonnet"
if (post != null) {
document.title = "${post.title} | Miley Chandonnet"
updateMetaTags(
title = post.title,
description = post.excerpt,
url = "https://miley.codes/#page=blog&post=${post.id}",
image = "https://miley.codes/blog/${post.id}/demo.gif",
)
setMetaTag("property", "og:type", "article")
} else {
document.title = "Miley Chandonnet"
}
}
page == "blog" -> {
document.title = "Blog | Miley Chandonnet"
updateMetaTags(
title = "Blog | Miley Chandonnet",
description = "Native Android Application Engineer",
url = "https://miley.codes/#page=blog",
image = "https://miley.codes/blog/dark-factory-needs-windows/demo.gif",
)
setMetaTag("property", "og:type", "website")
}
else -> {
document.title = "Miley Chandonnet"
updateMetaTags(
title = "Miley Chandonnet",
description = "Native Android Application Engineer",
url = "https://miley.codes",
image = "https://miley.codes/blog/dark-factory-needs-windows/demo.gif",
)
setMetaTag("property", "og:type", "website")
}
page == "blog" -> "Blog | Miley Chandonnet"
else -> "Miley Chandonnet"
}

header()
Expand Down