Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Mastodon posts not rendering in reader view #292

Merged
merged 3 commits into from
Feb 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class ReaderPresenter(
publishedAt = post.date.relativeDurationString(),
isBookmarked = post.bookmarked,
feed = feed,
postMode = RssContent
postMode = RssContent,
postImage = post.imageUrl
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ internal data class ReaderState(
val isBookmarked: Boolean?,
val feed: Feed?,
val postMode: PostMode,
val postImage: String?
) {

val hasContent: Boolean
get() =
content != null && feed != null && !publishedAt.isNullOrBlank() && !title.isNullOrBlank()
get() = content != null && feed != null && !publishedAt.isNullOrBlank()

companion object {

Expand All @@ -45,6 +45,7 @@ internal data class ReaderState(
isBookmarked = null,
feed = null,
postMode = PostMode.Idle,
postImage = null
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ internal fun readerHTML(
publishedAt: String,
content: String,
colors: ReaderHTMLColors,
featuredImage: String?,
): String {
val hasImgTags = content.contains("""<img[^>]*>""".toRegex())

// language=HTML
return """
<html lang="en">
Expand All @@ -43,11 +46,19 @@ internal fun readerHTML(
</style>
<body>
<h1>$title</h1>
<hr class="top-divider">
<div class ="feedName"><a href='$feedHomePageLink'>$feedName</a></div>
<div class="caption">$publishedAt</div>
<hr class="top-divider">
${feedSection(
feedName = feedName,
feedHomePageLink = feedHomePageLink,
publishedAt = publishedAt,
hasTitle = title.isNotBlank()
)}
$content
${if (!hasImgTags && !featuredImage.isNullOrBlank()) {
featuredImage(featuredImage)
} else {
// no-op
""
}}
<script>
${ReaderJs.content}
</script>
Expand All @@ -57,6 +68,35 @@ internal fun readerHTML(
.trimIndent()
}

private fun featuredImage(image: String): String {
return """
<img src='$image' alt="featured_image"/>
"""
.trimIndent()
}

private fun feedSection(
feedName: String,
feedHomePageLink: String,
publishedAt: String,
hasTitle: Boolean,
): String {
return buildString {
if (hasTitle) {
appendLine("<hr class=\"top-divider\">")
}

appendLine(
"""
<div class ="feedName"><a href='$feedHomePageLink'>$feedName</a></div>
<div class="caption">$publishedAt</div>
<hr class="top-divider">
"""
.trimIndent()
)
}
}

private object ReaderJs {

// language=JS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ internal fun ReaderScreen(presenter: ReaderPresenter, modifier: Modifier = Modif
linkColor = linkColor,
dividerColor = dividerColor,
codeBackgroundColor = codeBackgroundColor
)
),
featuredImage = state.postImage
)
}
val webViewState = rememberWebViewStateWithHTMLData(htmlTemplate)
Expand Down