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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36 changes: 36 additions & 0 deletions readability/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.serialization)
}

kotlin {
jvmToolchain(libs.versions.java.get().toInt())
explicitApi()
applyDefaultHierarchyTemplate()
androidLibrary {
compileSdk = libs.versions.compileSdk.get().toInt()
namespace = "dev.dimension.flare.readability"
minSdk = libs.versions.minSdk.get().toInt()
}
jvm()
Comment on lines +7 to +16
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Kotlin Multiplatform DSL here appears to be missing an Android target declaration (androidTarget()), and compileSdk/namespace/minSdk are typically configured in the Android Gradle Plugin android {} block (not under kotlin {}). As written, this is likely to fail Gradle configuration or produce no Android target; move Android SDK/namespace config to android {} and add androidTarget() (or the correct Android target DSL your Kotlin version supports).

Copilot uses AI. Check for mistakes.
macosArm64()
linuxX64()
mingwX64()
iosArm64()
iosSimulatorArm64()

sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.ksoup)
implementation(libs.kotlinx.serialization.json)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.dimension.flare.readability

/**
* Result of parsing an article with [Readability].
*/
public data class Article(
public val title: String,
public val byline: String?,
public val dir: String?,
public val lang: String?,
public val content: String,
public val textContent: String,
public val length: Int,
public val excerpt: String?,
public val siteName: String?,
public val publishedTime: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.dimension.flare.readability

/**
* Internal metadata holder used during parsing.
*/
internal data class ArticleMetadata(
var title: String? = null,
var byline: String? = null,
var excerpt: String? = null,
var siteName: String? = null,
var publishedTime: String? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package dev.dimension.flare.readability

import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.nodes.DataNode
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.nodes.Element
import com.fleeksoft.ksoup.nodes.Node
import com.fleeksoft.ksoup.nodes.TextNode

/**
* Configuration options for [isProbablyReaderable].
*/
public data class ReaderableOptions(
public val minScore: Double = 20.0,
public val minContentLength: Int = 140,
public val visibilityChecker: (Element) -> Boolean = ::isNodeVisible,
)

/**
* Checks whether a node is visible.
*
* In a KMP environment without a browser, we check the `style` attribute
* for `display:none` and the `hidden` / `aria-hidden` attributes.
*/
public fun isNodeVisible(node: Element): Boolean {
val style = node.attr("style")
if (style.isNotEmpty() && Regex("display\\s*:\\s*none", RegexOption.IGNORE_CASE).containsMatchIn(style)) {
return false
}
Comment on lines +26 to +29
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a new Regex instance on every isNodeVisible call. Since this is likely executed many times during scoring, consider hoisting the regex to a private val (or into RegExps) so it’s compiled once.

Copilot uses AI. Check for mistakes.
if (node.hasAttr("hidden")) return false
if (node.hasAttr("aria-hidden") && node.attr("aria-hidden") == "true") {
val className = node.className()
return className.contains("fallback-image")
}
return true
}

/**
* Mimics JS `textContent` — concatenates all text node values without
* adding any separators (unlike ksoup's `Element.text()` which
* normalizes whitespace and adds spaces at element boundaries).
*/
private fun textContent(node: Node): String {
val sb = StringBuilder()
fun collect(n: Node) {
if (n is TextNode) {
sb.append(n.getWholeText())
} else if (n is DataNode) {
sb.append(n.getWholeData())
} else {
for (child in n.childNodes()) {
collect(child)
}
}
}
collect(node)
return sb.toString()
}

/**
* Decides whether or not the document is reader-able without parsing the whole thing.
*
* @param doc the parsed [Document] to check
* @param options configuration for the check
* @return whether Readability.parse() will likely succeed at returning an article
*/
public fun isProbablyReaderable(doc: Document, options: ReaderableOptions = ReaderableOptions()): Boolean {
val nodes = mutableSetOf<Element>()

// Get <p>, <pre>, <article> nodes
nodes.addAll(doc.select("p, pre, article"))

// Get <div> nodes which have <br> node(s) and add their parents
val brNodes = doc.select("div > br")
for (br in brNodes) {
val parent = br.parent()
if (parent != null) {
nodes.add(parent)
}
}

var score = 0.0

for (node in nodes) {
if (!options.visibilityChecker(node)) continue

val matchString = node.className() + " " + node.id()
if (RegExps.unlikelyCandidates.containsMatchIn(matchString) &&
!RegExps.okMaybeItsACandidate.containsMatchIn(matchString)
) {
continue
}

if (node.`is`("li p")) continue

val textContentLength = textContent(node).trim().length
if (textContentLength < options.minContentLength) continue

score += kotlin.math.sqrt((textContentLength - options.minContentLength).toDouble())

if (score > options.minScore) {
return true
}
}
return false
}

/**
* Convenience overload that parses HTML first.
*
* @param html raw HTML string
* @param options configuration for the check
* @return whether Readability.parse() will likely succeed
*/
public fun isProbablyReaderable(html: String, options: ReaderableOptions = ReaderableOptions()): Boolean {
val doc = Ksoup.parse(html)
return isProbablyReaderable(doc, options)
}
Loading
Loading