Skip to content

Commit e5ccf4d

Browse files
committed
Document: add MathJax script in all HTML document before returning the document for download - refs BT#21424
1 parent 57c05b7 commit e5ccf4d

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

main/inc/lib/document.lib.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ public static function file_send_for_download(
427427

428428
switch ($contentType) {
429429
case 'text/html':
430+
$enableMathJaxScript = api_get_setting('enabled_mathjax');
430431
if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') {
431432
$contentType .= '; charset=UTF-8';
432433
} else {
@@ -480,15 +481,23 @@ public static function file_send_for_download(
480481
['https%3A%2F%2F', 'https://'],
481482
$content
482483
);
484+
if ($enableMathJaxScript) {
485+
$content = self::includeMathJaxScript($content);
486+
}
483487
echo $content;
484488
} else {
485-
if (function_exists('ob_end_clean') && ob_get_length()) {
486-
// Use ob_end_clean() to avoid weird buffering situations
487-
// where file is sent broken/incomplete for download
488-
ob_end_clean();
489+
if ($enableMathJaxScript) {
490+
$content = file_get_contents($full_file_name);
491+
$content = self::includeMathJaxScript($content);
492+
echo $content;
493+
} else {
494+
if (function_exists('ob_end_clean') && ob_get_length()) {
495+
// Use ob_end_clean() to avoid weird buffering situations
496+
// where file is sent broken/incomplete for download
497+
ob_end_clean();
498+
}
499+
readfile($full_file_name);
489500
}
490-
491-
readfile($full_file_name);
492501
}
493502

494503
return true;
@@ -7516,4 +7525,26 @@ private static function getButtonDelete(
75167525

75177526
return $btn;
75187527
}
7528+
7529+
/**
7530+
* Include MathJax script in document.
7531+
*
7532+
* @param string file content $content
7533+
*
7534+
* @return string file content
7535+
*/
7536+
private static function includeMathJaxScript($content)
7537+
{
7538+
$scriptTag = '<script src="'.api_get_path(WEB_PUBLIC_PATH).'assets/MathJax/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>';
7539+
// Find position of </body> tag
7540+
$pos = strpos($content, '</body>');
7541+
// If </body> tag found, insert the script tag before it
7542+
if ($pos !== false) {
7543+
$content = substr_replace($content, $scriptTag, $pos, 0);
7544+
} else {
7545+
// If </body> tag not found, just append the script tag at the end
7546+
$content .= $scriptTag;
7547+
}
7548+
return $content;
7549+
}
75197550
}

0 commit comments

Comments
 (0)