From 55f534bbc62563fe62d5a238998ee0af53096634 Mon Sep 17 00:00:00 2001
From: Antoine Lambert <anlambert@softwareheritage.org>
Date: Fri, 1 Mar 2024 17:26:15 +0100
Subject: [PATCH] webapp/assets/math-typesetting: Add mathjax extensions in
 static assets

Some Jupyter notebooks rendered by the webapp might require the use of
mathjax extensions that were not available in the static assets.

So ensure those extensions are available and can be lazily loaded when
required.
---
 assets/config/webpack.config.development.js   | 38 ++++++++++++++++++-
 .../webapp/assets/webapp/math-typesetting.js  |  5 +++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/assets/config/webpack.config.development.js b/assets/config/webpack.config.development.js
index 2704c351f..b1be34c4a 100644
--- a/assets/config/webpack.config.development.js
+++ b/assets/config/webpack.config.development.js
@@ -57,6 +57,29 @@ fs.readdirSync(appsDir).forEach(app => {
   }
 });
 
+const mathjaxExtensionsLicenses = {};
+
+const mathjaxTexExtensionsPath = path.join(nodeModules, 'mathjax/es5/input/tex/extensions');
+const mathjaxMMLExtensionsPath = path.join(nodeModules, 'mathjax/es5/input/mml/extensions');
+
+for (const mathjaxExtData of [
+  {'extsType': 'tex', 'extsPath': mathjaxTexExtensionsPath},
+  {'extsType': 'mml', 'extsPath': mathjaxMMLExtensionsPath}]) {
+  fs.readdirSync(mathjaxExtData['extsPath']).forEach(extension => {
+    if (extension.endsWith('.js')) {
+      mathjaxExtensionsLicenses[`js/mathjax/input/${mathjaxExtData['extsType']}/extensions/${extension}`] = [
+        {
+          'id': `mathjax/es5/input/${mathjaxExtData['extsType']}/extensions/${extension}`,
+          'path': `./node_modules/mathjax/es5/input/${mathjaxExtData['extsType']}/extensions/${extension}`,
+          'spdxLicenseExpression': 'Apache-2.0',
+          'licenseFilePath': './node_modules/mathjax/LICENSE'
+
+        }
+      ];
+    }
+  });
+}
+
 // common loaders for css related assets (css, sass)
 const cssLoaders = [
   MiniCssExtractPlugin.loader,
@@ -388,6 +411,18 @@ module.exports = {
         {
           from: path.resolve(nodeModules, 'mathjax/es5/output/chtml/fonts/woff-v2/**'),
           to: path.resolve(__dirname, '../../static/fonts/[name][ext]')
+        },
+        {
+          from: path.resolve(nodeModules, 'mathjax/es5/input/tex/extensions/'),
+          to: path.resolve(__dirname, '../../static/js/mathjax/input/tex/extensions/')
+        },
+        {
+          from: path.resolve(nodeModules, 'mathjax/es5/input/mml/extensions/'),
+          to: path.resolve(__dirname, '../../static/js/mathjax/input/mml/extensions/')
+        },
+        {
+          from: path.resolve(nodeModules, 'mathjax/es5/sre/mathmaps/'),
+          to: path.resolve(__dirname, '../../static/js/mathjax/sre/mathmaps/')
         }
       ]
     }),
@@ -447,7 +482,8 @@ module.exports = {
               'spdxLicenseExpression': 'BSD-3-Clause',
               'licenseFilePath': 'https://github.com/matomo-org/matomo/blob/master/js/LICENSE.txt'
             }
-          ]
+          ],
+          ...mathjaxExtensionsLicenses
         }
       )
     }),
diff --git a/swh/web/webapp/assets/webapp/math-typesetting.js b/swh/web/webapp/assets/webapp/math-typesetting.js
index c47815fd6..fcd175df9 100644
--- a/swh/web/webapp/assets/webapp/math-typesetting.js
+++ b/swh/web/webapp/assets/webapp/math-typesetting.js
@@ -20,6 +20,11 @@ export async function typesetMath() {
       displayMath: [ ['$$', '$$'], ['\\[', '\\]'] ],
       processEscapes: true,
       processEnvironments: true
+    },
+    loader: {
+      paths: {
+        mathjax: staticAsset('js/mathjax')
+      }
     }
   };
 
-- 
GitLab