Skip to content

Commit

Permalink
Use backend mathjax and jquery
Browse files Browse the repository at this point in the history
  • Loading branch information
MorenoTropical authored and mikehardy committed Oct 11, 2024
1 parent fd74509 commit 5524c3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/assets/card_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</style>
<link rel="stylesheet" type="text/css" href="file:///android_asset/backend/web/reviewer_extras.css">
::script::
<script src="file:///android_asset/jquery.min.js"> </script>
<script src="file:///android_asset/backend/web/jquery.min.js"> </script>
<script src="file:///android_asset/scripts/card.js" type="text/javascript"> </script>
<script src="file:///android_asset/scripts/js-api.js" type="text/javascript"> </script>
<script src="file:///android_asset/backend/web/reviewer_extras_bundle.js"> </script>
Expand Down
13 changes: 13 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class AndroidCardRenderContext(
val script = when (requiresMathjax) {
false -> ""
true ->
""" <script src="file:///android_asset/mathjax/conf.js"></script>
<script src="file:///android_asset/mathjax/tex-chtml.js"></script>"""
""" <script src="file:///android_asset/backend/web/mathjax.js"></script>
<script src="file:///android_asset/backend/web/vendor/mathjax/tex-chtml-full.js"></script>"""
}
val cardClass = cardAppearance.getCardClass(ord + 1) + if (requiresMathjax) " mathjax-needs-to-render" else ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ fun stdHtml(
<div id="_mark" hidden>&#x2605;</div>
<div id="_flag" hidden>&#x2691;</div>
<div id="qa"></div>
<script src="file:///android_asset/jquery.min.js"></script>
<script src="file:///android_asset/mathjax/tex-chtml.js"></script>
<script src="file:///android_asset/backend/web/jquery.min.js"></script>
<script src="file:///android_asset/backend/web/mathjax.js"></script>
<script src="file:///android_asset/backend/web/vendor/mathjax/tex-chtml-full.js"></script>
<script src="file:///android_asset/scripts/ankidroid.js"></script>
<script src="file:///android_asset/backend/web/reviewer.js"></script>
<script>bridgeCommand = function(){};</script>
Expand Down

0 comments on commit 5524c3a

Please sign in to comment.