From e50f4fbbca21b85648f6c4045cbd50f672d315cd Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Fri, 16 Oct 2026 21:04:38 +0000 Subject: Render an article's maths instead of showing its TeX source Reported against lcamtuf's Substack ("we should render tex", blog.coredump.cx/p/yours-truly): reader mode showed raw "\(p = \text{...}\)" where equations should be. What the page actually ships is the TeX SOURCE, as text, inside a "latex-rendered" div -- Substack renders it in the browser with its own script, which never runs here. Readability keeps that text verbatim (it strips the class, not the text), so nothing was being discarded during extraction and no amount of extraction-side repair would have helped: there was simply never any rendered markup to keep. So render it during extraction, where JavaScript is already running for Readability's sake. KaTeX (vendored in assets, never fetched -- this runs against untrusted third-party pages) converts each expression to MathML before Readability sees the document, and the reader WebView renders that natively. The reader keeps JavaScript OFF, as it must, and needs no stylesheet, webfont or script of KaTeX's; the only reader-side change is containing a wide equation so it scrolls within its own box instead of dragging the whole article sideways. Two details worth knowing: - KaTeX refuses {align} outside display mode, and Substack wraps display maths in INLINE delimiters (a block-styled span does the centring), so a mode that renders nothing is retried in the other one. An expression neither mode can render is left as source -- no worse than before. - The reported article came with "extractedContent cached: true", so the raw source was already on disk and every open read it back. Recognised and dropped on read (same mechanism as the cached consent notice), which re-extracts the article the next time it is opened. APK grows by ~90KB: the vendored katex.min.js (267KB) compresses to 91KB in the APK. No new permissions, no schema change, no new JS-enabled surface. Verified on the emulator against the live article: stale raw-TeX cache discarded, article re-extracted, all four equations rendered, and a wide align block scrolls inside itself. --- .../terst/feedreader/data/CachedExtractionTest.kt | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'app/src/test/java/org/terst/feedreader/data') diff --git a/app/src/test/java/org/terst/feedreader/data/CachedExtractionTest.kt b/app/src/test/java/org/terst/feedreader/data/CachedExtractionTest.kt index 72dc73f..fc9c32e 100644 --- a/app/src/test/java/org/terst/feedreader/data/CachedExtractionTest.kt +++ b/app/src/test/java/org/terst/feedreader/data/CachedExtractionTest.kt @@ -33,6 +33,7 @@ class CachedExtractionTest { val result = loadCachedExtraction(content) { cleared = true } assertEquals(content, result.content) assertFalse(result.discardedConsentNotice) + assertFalse(result.discarded) assertFalse("a good cache must not be cleared", cleared) } @@ -54,6 +55,37 @@ class CachedExtractionTest { assertFalse(cleared) } + // Same shape of problem as the consent notice, different cause: this content was + // extracted before extraction knew how to render maths, so it holds the raw TeX source + // Substack ships. A fixed extractor never gets a look at it -- the cached copy is + // returned first and the article is never re-extracted. + @Test + fun `a cached extraction holding raw tex source is discarded and deleted`() = runTest { + var cleared = false + val rawTex = "

Imagine the following sentence:

" + + "

" + + "\\(p = \\text{\u201Cif }p\\text{ is true, then pigs can fly\u201D}\\)

" + + "

" + "Considered as a whole, the statement cannot be true. ".repeat(20) + "

" + val result = loadCachedExtraction(rawTex) { cleared = true } + assertNull("raw TeX source must not be shown as the article", result.content) + assertTrue(result.discardedUnrenderedTex) + assertTrue("the stale row must be cleared so a re-extract can replace it", cleared) + assertTrue("the caller must be told to extract again", result.discarded) + } + + @Test + fun `an extraction with rendered maths is kept`() = runTest { + var cleared = false + val withMathMl = "

A slice of the set looks like this:

" + + "" + + "p=" + + "

" + "The rest of the article continues from there. ".repeat(20) + "

" + val result = loadCachedExtraction(withMathMl) { cleared = true } + assertEquals(withMathMl, result.content) + assertFalse(result.discardedUnrenderedTex) + assertFalse("a good cache must not be cleared", cleared) + } + // A blank value is as good as nothing cached -- it is not a consent notice, so // reporting it as a discarded one would make the caller re-extract on a false // premise. -- cgit v1.2.3