Skip to content

Commit 9254771

Browse files
committed
Add mathjax js loading filter
1 parent 899ea78 commit 9254771

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

.eleventy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ module.exports = function(eleventyConfig) {
110110
return (lastSpace > 0 ? truncated.substring(0, lastSpace) : truncated) + "...";
111111
});
112112

113+
/* Math detection filter: checks for LaTeX/MathJax delimiters */
114+
eleventyConfig.addFilter("hasMath", (content) => {
115+
if (!content) return false;
116+
// Detect: $...$ (inline), $$...$$ (display), \(...\), \[...\]
117+
const mathPattern = /\$\$[\s\S]+?\$\$|\$[^\$\n]+?\$|\\\([\s\S]+?\\\)|\\\[[\s\S]+?\\\]/;
118+
return mathPattern.test(content);
119+
});
120+
113121
/* Set directories */
114122
return {
115123
dir: {

src/_includes/_head.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@
6363
<!-- Favicon -->
6464
<link rel="icon" href="/favicon.ico">
6565

66-
<!--mathjax-->
67-
<script type="text/javascript" id="MathJax-script" async
68-
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
69-
</script>
70-
<!--use dollar signs for math-->
66+
<!-- MathJax: only load on pages with math content -->
67+
{% assign needsMath = content | hasMath %}
68+
{% if needsMath %}
7169
<script>
7270
MathJax = {
7371
tex: {
@@ -76,6 +74,8 @@
7674
}
7775
};
7876
</script>
77+
<script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
78+
{% endif %}
7979

8080
<!-- JSON-LD Structured Data -->
8181
<script type="application/ld+json">

0 commit comments

Comments
 (0)