Yokan / rebuild
Course 1 8624561

NovelEmbeddedContent.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/novel/NovelEmbeddedContent.kt

implementation25 lines
1package eu.friedcutlet.yokan.ui.screens.novel23import kotlinx.coroutines.currentCoroutineContext4import kotlinx.coroutines.ensureActive56/** Reads each embedded image [html] references once, with backpressure and a per-chapter budget. */7internal suspend fun loadNovelEmbeddedImages(8    html: String,9    embeddedImage: suspend (String) -> ByteArray?,10): Map<String, ByteArray> {11    val images = mutableMapOf<String, ByteArray?>()12    var remaining = 32L * 1024 * 102413    parseNovelRichContent(html).blocks.forEach { block ->14        currentCoroutineContext().ensureActive()15        val reference = (block as? NovelContentBlock.Image)?.reference.orEmpty()16        // External/data images retain the remote-image behavior.17        if (reference.isBlank() || reference in images || reference.startsWith("//") || reference.substringBefore('/').contains(':')) {18            return@forEach19        }20        val loaded = if (remaining > 0) embeddedImage(reference) else null21        currentCoroutineContext().ensureActive()22        images[reference] = loaded?.takeIf { it.size <= remaining }?.also { remaining -= it.size }23    }24    return buildMap { images.forEach { (reference, bytes) -> if (bytes != null) put(reference, bytes) } }25}26

Search chapters, reference and source filenames.