From cb884125a64d45a69acdd5a107c114101362a62d Mon Sep 17 00:00:00 2001 From: MorenoTropical <154519856+morenotropical@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:54:05 -0300 Subject: [PATCH] Use backend mathjax and jquery --- AnkiDroid/src/main/assets/card_template.html | 2 +- .../java/com/ichi2/anki/ViewerResourceHandler.kt | 13 +++++++++++++ .../anki/cardviewer/AndroidCardRenderContext.kt | 4 ++-- .../com/ichi2/anki/previewer/PreviewerHelpers.kt | 5 +++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/AnkiDroid/src/main/assets/card_template.html b/AnkiDroid/src/main/assets/card_template.html index 64afe4651912..1d7c46bda791 100644 --- a/AnkiDroid/src/main/assets/card_template.html +++ b/AnkiDroid/src/main/assets/card_template.html @@ -10,7 +10,7 @@ ::script:: - + diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt index cff6087962da..5d7c00023dcf 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt @@ -25,10 +25,14 @@ import timber.log.Timber import java.io.ByteArrayInputStream import java.io.File import java.io.FileInputStream +import java.nio.file.Paths +import kotlin.io.path.pathString private const val RANGE_HEADER = "Range" +private const val MATHJAX_PATH_PREFIX = "/_anki/js/vendor/mathjax" class ViewerResourceHandler(context: Context) { + private val assetManager = context.assets private val mediaDir = CollectionHelper.getMediaDirectory(context) fun shouldInterceptRequest(request: WebResourceRequest): WebResourceResponse? { @@ -43,6 +47,15 @@ class ViewerResourceHandler(context: Context) { } try { + if (path.startsWith(MATHJAX_PATH_PREFIX)) { + val mathjaxAssetPath = Paths.get( + "backend/web/vendor/mathjax", + path.removePrefix(MATHJAX_PATH_PREFIX) + ).pathString + val inputStream = assetManager.open(mathjaxAssetPath) + return WebResourceResponse(guessMimeType(path), null, inputStream) + } + val file = File(mediaDir, path) if (!file.exists()) { return null diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/AndroidCardRenderContext.kt b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/AndroidCardRenderContext.kt index b8b221e9c48a..327e08c7f149 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/AndroidCardRenderContext.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/AndroidCardRenderContext.kt @@ -69,8 +69,8 @@ class AndroidCardRenderContext( val script = when (requiresMathjax) { false -> "" true -> - """ - """ + """ + """ } val cardClass = cardAppearance.getCardClass(ord + 1) + if (requiresMathjax) " mathjax-needs-to-render" else "" diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt index 3bdc56dea71e..91d2a34aed9f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt @@ -85,8 +85,9 @@ fun stdHtml(