Skip to content

Migrate desktop reader parsing from Readability to Defuddle on HtmlUnit 5#1216

Merged
prof18 merged 1 commit into
mainfrom
desktop-defuddle-parser
Jun 11, 2026
Merged

Migrate desktop reader parsing from Readability to Defuddle on HtmlUnit 5#1216
prof18 merged 1 commit into
mainfrom
desktop-defuddle-parser

Conversation

@prof18

@prof18 prof18 commented Jun 11, 2026

Copy link
Copy Markdown
Owner

What

Replaces the desktop reader-mode content extractor. The old pipeline ran a customized Mozilla Readability fork plus Turndown inside HtmlUnit 4. This swaps it for Defuddle (the same library already used on Android and iOS), running on HtmlUnit 5, using Defuddle's native markdown converter.

Why

  • The Readability fork was a one-off, customized to work around the old HtmlUnit JS engine, and diverged from what mobile uses.
  • HtmlUnit 5 (May 2026) shipped major core-js JS-engine updates that make running the modern Defuddle bundle feasible.
  • Defuddle's own markdown converter produces cleaner output than the previous Readability + Turndown chain (fewer junk artifacts), and aligns desktop with the Android/iOS reader experience.

How

  • HtmlUnit 4.21.05.1.0 (gradle/libs.versions.toml).
  • defuddle-full-es5.js — the Defuddle full build (it includes the markdown converter; the core build used on mobile does not) transpiled to ES5 with Babel, because HtmlUnit's Rhino-based engine can't parse the original ES2020 bundle (e.g. class syntax). Regenerated from the same Defuddle version as the mobile defuddle.js bundle.
  • defuddle-content-parser.js — wraps Defuddle with markdown: true and the desktop-specific handling: banner-image removal, image dedup, relative→absolute URL resolution, and <iframe> stripping (HtmlUnit's DOMParser NPEs on frames in a detached document).
  • HtmlUnit compatibility shims (the important part) in defuddle-content-parser.js:
    • getComputedStyle / getBoundingClientRect return safe stubs — HtmlUnit throws uncatchable Java exceptions on these for elements in a DOMParser-created document (no enclosing WebWindow), and CSS is disabled in the WebClient anyway.
    • querySelector/querySelectorAll rewrite the :scope pseudo-class (unsupported by HtmlUnit's CSS engine) and degrade any other unsupported selector to an empty result instead of throwing. Without this, a single :scope selector aborted all of Defuddle's clutter removal and it fell back to returning the raw, junk-filled document — this was the root cause of the messy desktop output during development.
  • DesktopFeedItemParserWorker.kt slimmed to mobile parity: only placeholder-image cleanup remains in Kotlin; the rest moved into the JS parser. Adds a debug-level log of the parsed title + content snippet.
  • Removed readability-es5.js, reader-content-parser.js, turndown-es5.js.

Tests

  • DefuddleHtmlUnitTest — regression test running the full JS pipeline inside HtmlUnit, asserting markdown output (link conversion, no leftover HTML/script bodies).
  • DefuddleDebugHarness — a manual diagnostic (no-ops without a local fixture) that prints Defuddle's debug log + resulting markdown for a given article; used to find the :scope failure and handy for future Defuddle version bumps.
  • Verified live in the running desktop app against several real articles (Il Post, 9to5Google, a personal blog) — clean markdown, site chrome removed, matching mobile output.
  • ./gradlew detekt allTests passes.

Reviewer notes

  • The ES5 Defuddle bundle must be regenerated (same Babel config, same Defuddle version as mobile) whenever the Android/iOS defuddle.js is updated.
  • Known follow-up (not in this PR): very large WeChat articles (multi-MB, see issue 为什么电脑版无法解析微信公众号文章? #1214) are slow in HtmlUnit; the desktop worker also lacks a JS-execution timeout (Android has one). Worth a separate pass.

@prof18 prof18 enabled auto-merge (rebase) June 11, 2026 21:19
@prof18 prof18 merged commit 864873a into main Jun 11, 2026
4 checks passed
@prof18 prof18 deleted the desktop-defuddle-parser branch June 11, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant