Document: add MathJax script in all HTML document before returning the document for download - refs BT#21424

pull/5347/head
NicoDucou 1 year ago
parent 57c05b7f81
commit e5ccf4d6e8
  1. 43
      main/inc/lib/document.lib.php

@ -427,6 +427,7 @@ class DocumentManager
switch ($contentType) {
case 'text/html':
$enableMathJaxScript = api_get_setting('enabled_mathjax');
if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') {
$contentType .= '; charset=UTF-8';
} else {
@ -480,15 +481,23 @@ class DocumentManager
['https%3A%2F%2F', 'https://'],
$content
);
if ($enableMathJaxScript) {
$content = self::includeMathJaxScript($content);
}
echo $content;
} else {
if (function_exists('ob_end_clean') && ob_get_length()) {
// Use ob_end_clean() to avoid weird buffering situations
// where file is sent broken/incomplete for download
ob_end_clean();
if ($enableMathJaxScript) {
$content = file_get_contents($full_file_name);
$content = self::includeMathJaxScript($content);
echo $content;
} else {
if (function_exists('ob_end_clean') && ob_get_length()) {
// Use ob_end_clean() to avoid weird buffering situations
// where file is sent broken/incomplete for download
ob_end_clean();
}
readfile($full_file_name);
}
readfile($full_file_name);
}
return true;
@ -7516,4 +7525,26 @@ class DocumentManager
return $btn;
}
/**
* Include MathJax script in document.
*
* @param string file content $content
*
* @return string file content
*/
private static function includeMathJaxScript($content)
{
$scriptTag = '<script src="'.api_get_path(WEB_PUBLIC_PATH).'assets/MathJax/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>';
// Find position of </body> tag
$pos = strpos($content, '</body>');
// If </body> tag found, insert the script tag before it
if ($pos !== false) {
$content = substr_replace($content, $scriptTag, $pos, 0);
} else {
// If </body> tag not found, just append the script tag at the end
$content .= $scriptTag;
}
return $content;
}
}

Loading…
Cancel
Save