feat: integrate MathJax support for LaTeX rendering
Added a customizer setting for the URL of a MathJax-node server. Implemented functions to render LaTeX expressions using the specified MathJax server, handling errors gracefully. Also included processing to convert LaTeX in HTML content before displaying it on the site.
This commit is contained in:
parent
3f6f3fbbb5
commit
93a8489fcf
1 changed files with 93 additions and 0 deletions
|
@ -862,3 +862,96 @@ function custom_articles_per_page($query)
|
|||
}
|
||||
|
||||
add_action('pre_get_posts', 'custom_articles_per_page');
|
||||
|
||||
function mathjax_customizer_settings($wp_customize)
|
||||
{
|
||||
// Add a section for MathJax settings
|
||||
$wp_customize->add_section('mathjax_settings', [
|
||||
'title' => __('MathJax Settings', 'duck-behavior-journal'),
|
||||
'priority' => 30,
|
||||
'description' => __('Specify the URL of your MathJax-node server application. The required server application can be found at <a href="https://git.private.coffee/privatecoffee/mathjax-server" target="_blank">https://git.private.coffee/privatecoffee/mathjax-server</a>.', 'duck-behavior-journal'),
|
||||
]);
|
||||
|
||||
// Add a setting for the MathJax-node server URL
|
||||
$wp_customize->add_setting('mathjax_server_url', [
|
||||
'default' => '',
|
||||
'sanitize_callback' => 'esc_url_raw',
|
||||
]);
|
||||
|
||||
// Add a control for the MathJax-node server URL
|
||||
$wp_customize->add_control('mathjax_server_url', [
|
||||
'label' => __('MathJax-node Server URL', 'duck-behavior-journal'),
|
||||
'section' => 'mathjax_settings',
|
||||
'type' => 'url',
|
||||
]);
|
||||
}
|
||||
|
||||
add_action('customize_register', 'mathjax_customizer_settings');
|
||||
|
||||
function renderMathJax($latex)
|
||||
{
|
||||
$url = get_theme_mod('mathjax_server_url', '');
|
||||
if (empty($url)) {
|
||||
return '<span class="error">' . htmlspecialchars($latex) . '</span>';
|
||||
}
|
||||
|
||||
$data = json_encode(['tex' => $latex]);
|
||||
|
||||
$options = [
|
||||
'http' => [
|
||||
'header' => "Content-Type: application/json\r\n" .
|
||||
"Content-Length: " . strlen($data) . "\r\n",
|
||||
'method' => 'POST',
|
||||
'content' => $data,
|
||||
],
|
||||
];
|
||||
|
||||
$context = stream_context_create($options);
|
||||
$result = file_get_contents($url, false, $context);
|
||||
|
||||
if ($result === FALSE) {
|
||||
return '<span class="error">Error rendering LaTeX</span>';
|
||||
}
|
||||
|
||||
$response = json_decode($result, true);
|
||||
|
||||
if (isset($response['error'])) {
|
||||
return '<span class="error">Error rendering LaTeX: ' . implode(', ', $response['error']) . '</span>';
|
||||
}
|
||||
|
||||
return $response['html'];
|
||||
}
|
||||
|
||||
function convertMathJaxInHTML($html)
|
||||
{
|
||||
$url = get_theme_mod('mathjax_server_url', '');
|
||||
if (empty($url)) {
|
||||
return $html; // If the URL is not specified, return the original HTML
|
||||
}
|
||||
|
||||
// Use a regular expression to find LaTeX expressions within the HTML
|
||||
$pattern = '/\$(.*?)\$/';
|
||||
$callback = function ($matches) {
|
||||
$latex = $matches[1];
|
||||
try {
|
||||
// Render the LaTeX to HTML using the MathJax-node server
|
||||
$rendered = renderMathJax($latex);
|
||||
return $rendered;
|
||||
} catch (Exception $e) {
|
||||
// Handle rendering errors
|
||||
return '<span class="error">' . htmlspecialchars($latex) . '</span>';
|
||||
}
|
||||
};
|
||||
|
||||
// Replace all LaTeX expressions in the HTML with rendered HTML
|
||||
$convertedHtml = preg_replace_callback($pattern, $callback, $html);
|
||||
|
||||
return $convertedHtml;
|
||||
}
|
||||
|
||||
function preprocess_mathjax_content($content)
|
||||
{
|
||||
return convertMathJaxInHTML($content);
|
||||
}
|
||||
|
||||
add_filter('the_content', 'preprocess_mathjax_content');
|
||||
|
|
Loading…
Reference in a new issue