From 364588f1cb50257d7d7facc73d281c94ad583304 Mon Sep 17 00:00:00 2001 From: Josiah Campbell <9521010+jocmp@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:00:02 -0600 Subject: [PATCH] Reintroduce cleanLinks --- .../com/jocmp/capy/articles/ArticleRenderer.kt | 1 + .../java/com/jocmp/capy/articles/CleanLinks.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 capy/src/main/java/com/jocmp/capy/articles/CleanLinks.kt diff --git a/capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt b/capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt index 627fbdb1..2c207447 100644 --- a/capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt +++ b/capy/src/main/java/com/jocmp/capy/articles/ArticleRenderer.kt @@ -49,6 +49,7 @@ class ArticleRenderer( document.getElementById("article-body-content")?.append(article.content) cleanStyles(document) + cleanLinks(document) return document.html() } diff --git a/capy/src/main/java/com/jocmp/capy/articles/CleanLinks.kt b/capy/src/main/java/com/jocmp/capy/articles/CleanLinks.kt new file mode 100644 index 00000000..55dd58a8 --- /dev/null +++ b/capy/src/main/java/com/jocmp/capy/articles/CleanLinks.kt @@ -0,0 +1,17 @@ +package com.jocmp.capy.articles + +import org.jsoup.nodes.Element + +internal fun cleanLinks(element: Element) { + element.getElementsByTag("img").forEachIndexed{ index, child -> + child.attr("src", child.absUrl("src")) + + if (index > 0) { + child.attr("loading", "lazy") + } + } + + element.select("img[data-src]").forEach { child -> + child.attr("src", child.absUrl("data-src")) + } +}