feat(reads): Phase 1 — article reader with markdown, ToC, reading time
- Add richtext-commonmark deps to commons for shared markdown rendering - Create RenderMarkdown composable with URL scheme allowlist (security) - Create ArticleMediaRenderer interface for platform-specific media - Create ArticleHeader with banner, title, author, reading time metadata - Create TableOfContents with heading extraction and scroll-spy - Create ReadingTimeCalculator (238 WPM prose, 80 WPM code, image decay) - Create ArticleReaderScreen with Medium-style typography (680dp, 1.58x) - Add DesktopScreen.Article navigation with address tag routing - Update ReadsScreen to navigate via address tags instead of event IDs - Responsive ToC sidebar (shown when window > 1100dp) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.compose.article
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import coil3.compose.AsyncImage
|
||||
|
||||
@Composable
|
||||
fun ArticleHeader(
|
||||
title: String,
|
||||
authorName: String?,
|
||||
authorPicture: String?,
|
||||
publishedAt: String?,
|
||||
readingTimeMinutes: Int?,
|
||||
bannerUrl: String?,
|
||||
modifier: Modifier = Modifier,
|
||||
onAuthorClick: (() -> Unit)? = null,
|
||||
) {
|
||||
Column(modifier = modifier.fillMaxWidth()) {
|
||||
// Banner image
|
||||
if (!bannerUrl.isNullOrBlank()) {
|
||||
AsyncImage(
|
||||
model = bannerUrl,
|
||||
contentDescription = "Article banner",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.fillMaxWidth().height(300.dp),
|
||||
)
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
|
||||
// Title
|
||||
Text(
|
||||
text = title,
|
||||
style =
|
||||
MaterialTheme.typography.headlineLarge.copy(
|
||||
fontSize = 34.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
lineHeight = 40.sp,
|
||||
letterSpacing = (-0.5).sp,
|
||||
),
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
// Author + metadata row
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
if (!authorPicture.isNullOrBlank()) {
|
||||
AsyncImage(
|
||||
model = authorPicture,
|
||||
contentDescription = "Author",
|
||||
modifier = Modifier.size(40.dp).clip(CircleShape),
|
||||
contentScale = ContentScale.Crop,
|
||||
)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
}
|
||||
|
||||
Column {
|
||||
if (!authorName.isNullOrBlank()) {
|
||||
Text(
|
||||
text = authorName,
|
||||
style = MaterialTheme.typography.bodyMedium.copy(fontWeight = FontWeight.SemiBold),
|
||||
)
|
||||
}
|
||||
|
||||
val metaParts = mutableListOf<String>()
|
||||
readingTimeMinutes?.let { metaParts.add("$it min read") }
|
||||
publishedAt?.let { metaParts.add(it) }
|
||||
|
||||
if (metaParts.isNotEmpty()) {
|
||||
Text(
|
||||
text = metaParts.joinToString(" · "),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.compose.article
|
||||
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.drawBehind
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
data class TocEntry(
|
||||
val level: Int,
|
||||
val text: String,
|
||||
val index: Int,
|
||||
)
|
||||
|
||||
/**
|
||||
* Extracts table of contents entries from markdown content.
|
||||
* Parses ATX headings (# H1, ## H2, etc.), skipping code blocks.
|
||||
*/
|
||||
fun extractTableOfContents(markdown: String): List<TocEntry> {
|
||||
val entries = mutableListOf<TocEntry>()
|
||||
var inCodeBlock = false
|
||||
var headingIndex = 0
|
||||
|
||||
markdown.lines().forEach { line ->
|
||||
val trimmed = line.trim()
|
||||
if (trimmed.startsWith("```")) {
|
||||
inCodeBlock = !inCodeBlock
|
||||
return@forEach
|
||||
}
|
||||
if (inCodeBlock) return@forEach
|
||||
|
||||
val match = Regex("^(#{1,6})\\s+(.+)").find(trimmed)
|
||||
if (match != null) {
|
||||
val level = match.groupValues[1].length
|
||||
val text =
|
||||
match.groupValues[2]
|
||||
.trim()
|
||||
.replace(Regex("#+$"), "")
|
||||
.trim()
|
||||
if (text.isNotEmpty() && level <= 3) {
|
||||
entries.add(TocEntry(level = level, text = text, index = headingIndex))
|
||||
}
|
||||
headingIndex++
|
||||
}
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TableOfContents(
|
||||
entries: List<TocEntry>,
|
||||
activeEntryIndex: Int?,
|
||||
onEntryClick: (TocEntry) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val scrollState = rememberScrollState()
|
||||
|
||||
Column(
|
||||
modifier =
|
||||
modifier
|
||||
.width(240.dp)
|
||||
.verticalScroll(scrollState)
|
||||
.padding(vertical = 16.dp),
|
||||
) {
|
||||
entries.forEach { entry ->
|
||||
val isActive = entry.index == activeEntryIndex
|
||||
val accentColor = MaterialTheme.colorScheme.primary
|
||||
|
||||
Text(
|
||||
text = entry.text,
|
||||
style =
|
||||
MaterialTheme.typography.bodySmall.copy(
|
||||
fontSize = 13.sp,
|
||||
fontWeight = if (isActive) FontWeight.Bold else FontWeight.Normal,
|
||||
color =
|
||||
if (isActive) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.onSurfaceVariant
|
||||
},
|
||||
),
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier =
|
||||
Modifier
|
||||
.clickable { onEntryClick(entry) }
|
||||
.padding(
|
||||
start = ((entry.level - 1) * 16).dp,
|
||||
top = 4.dp,
|
||||
bottom = 4.dp,
|
||||
end = 8.dp,
|
||||
).then(
|
||||
if (isActive) {
|
||||
Modifier.drawBehind {
|
||||
drawLine(
|
||||
color = accentColor,
|
||||
start = Offset(0f, 0f),
|
||||
end = Offset(0f, size.height),
|
||||
strokeWidth = 3.dp.toPx(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Modifier
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.compose.markdown
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
/**
|
||||
* Interface for platform-specific media rendering in markdown articles.
|
||||
* Desktop and Android provide different implementations for image loading and navigation.
|
||||
*/
|
||||
interface ArticleMediaRenderer {
|
||||
@Composable
|
||||
fun renderImage(
|
||||
url: String,
|
||||
alt: String?,
|
||||
)
|
||||
|
||||
fun onLinkClick(url: String)
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.compose.markdown
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.platform.UriHandler
|
||||
import com.halilibo.richtext.commonmark.CommonMarkdownParseOptions
|
||||
import com.halilibo.richtext.commonmark.CommonmarkAstNodeParser
|
||||
import com.halilibo.richtext.markdown.BasicMarkdown
|
||||
import com.halilibo.richtext.ui.RichTextStyle
|
||||
import com.halilibo.richtext.ui.material3.RichText
|
||||
|
||||
private val ALLOWED_SCHEMES = setOf("https", "http", "nostr", "lightning")
|
||||
|
||||
@Composable
|
||||
fun RenderMarkdown(
|
||||
content: String,
|
||||
mediaRenderer: ArticleMediaRenderer,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val astNode =
|
||||
remember(content) {
|
||||
CommonmarkAstNodeParser(CommonMarkdownParseOptions.MarkdownWithLinks).parse(content)
|
||||
}
|
||||
|
||||
val uriHandler =
|
||||
remember(mediaRenderer) {
|
||||
object : UriHandler {
|
||||
override fun openUri(uri: String) {
|
||||
val scheme = uri.substringBefore(":").lowercase()
|
||||
if (scheme in ALLOWED_SCHEMES) {
|
||||
mediaRenderer.onLinkClick(uri)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CompositionLocalProvider(LocalUriHandler provides uriHandler) {
|
||||
RichText(
|
||||
modifier = modifier,
|
||||
style = RichTextStyle(),
|
||||
) {
|
||||
BasicMarkdown(astNode)
|
||||
}
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.model.nip23LongContent
|
||||
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.max
|
||||
|
||||
/**
|
||||
* Calculates estimated reading time for markdown content.
|
||||
* Uses 238 WPM for prose (Brysbaert 2019), 80 WPM for code blocks,
|
||||
* and Medium's image decay formula (12 sec first, -1 each, min 3).
|
||||
*/
|
||||
object ReadingTimeCalculator {
|
||||
private const val PROSE_WPM = 238.0
|
||||
private const val CODE_WPM = 80.0
|
||||
|
||||
fun calculate(markdownContent: String): Int {
|
||||
var proseWords = 0
|
||||
var codeWords = 0
|
||||
var imageCount = 0
|
||||
var inCodeBlock = false
|
||||
|
||||
markdownContent.lines().forEach { line ->
|
||||
val trimmed = line.trim()
|
||||
|
||||
if (trimmed.startsWith("```")) {
|
||||
inCodeBlock = !inCodeBlock
|
||||
return@forEach
|
||||
}
|
||||
|
||||
if (inCodeBlock) {
|
||||
codeWords += trimmed.split("\\s+".toRegex()).count { it.isNotBlank() }
|
||||
return@forEach
|
||||
}
|
||||
|
||||
// Count images
|
||||
val imageMatches = Regex("!\\[.*?]\\(.*?\\)").findAll(trimmed)
|
||||
imageCount += imageMatches.count()
|
||||
|
||||
// Strip markdown syntax for word counting
|
||||
val stripped =
|
||||
trimmed
|
||||
.replace(Regex("!\\[.*?]\\(.*?\\)"), "") // images
|
||||
.replace(Regex("\\[([^]]*)]\\([^)]*\\)"), "$1") // links -> text only
|
||||
.replace(Regex("[*_~`#>]"), "") // formatting
|
||||
.replace(Regex("^-\\s+|^\\d+\\.\\s+"), "") // list markers
|
||||
.replace(Regex("^---+$|^\\*\\*\\*+$"), "") // horizontal rules
|
||||
|
||||
proseWords += stripped.split("\\s+".toRegex()).count { it.isNotBlank() }
|
||||
}
|
||||
|
||||
// Medium's image time decay: 12 sec first, -1 each, min 3
|
||||
val imageSeconds = (0 until imageCount).sumOf { max(12 - it, 3) }
|
||||
|
||||
val totalMinutes = (proseWords / PROSE_WPM) + (codeWords / CODE_WPM) + (imageSeconds / 60.0)
|
||||
return max(1, ceil(totalMinutes).toInt())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user