diff --git a/package.json b/package.json --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "jquery": "^3.4.1", "js-cookie": "^2.2.1", "js-year-calendar": "^1.0.2", + "mathjax": "^3.0.5", "mocha-junit-reporter": "^1.23.3", "notebookjs": "^0.4.2", "object-fit-images": "^3.2.4", diff --git a/swh/web/assets/config/mathjax-js-files.js b/swh/web/assets/config/mathjax-js-files.js deleted file mode 100644 --- a/swh/web/assets/config/mathjax-js-files.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (C) 2019 The Software Heritage developers - * See the AUTHORS file at the top-level directory of this distribution - * License: GNU Affero General Public License version 3, or any later version - * See top-level LICENSE file for more information - */ - -function getLoadedMathJaxJsFilesInfo(mathJaxJsFiles, cdnPrefix, unpackedLocationPrefix) { - let ret = {}; - for (let mathJaxJsFile of mathJaxJsFiles) { - ret[cdnPrefix + mathJaxJsFile] = [{ - 'id': mathJaxJsFile, - 'path': unpackedLocationPrefix + mathJaxJsFile, - 'spdxLicenseExpression': 'Apache-2.0', - 'licenseFilePath': '' - }]; - } - return ret; -} - -let cdnPrefix = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/'; - -let unpackedLocationPrefix = 'https://raw.githubusercontent.com/mathjax/MathJax/2.7.5/unpacked/'; - -let mathJaxJsFiles = [ - 'MathJax.js', - 'config/TeX-AMS_HTML.js', - 'extensions/MathMenu.js', - 'jax/element/mml/optable/BasicLatin.js', - 'jax/output/HTML-CSS/jax.js', - 'jax/output/HTML-CSS/fonts/TeX/fontdata.js', - 'jax/output/HTML-CSS/fonts/TeX/AMS/Regular/Main.js', - 'jax/output/HTML-CSS/fonts/TeX/AMS/Regular/BBBold.js', - 'jax/output/HTML-CSS/fonts/TeX/AMS/Regular/GeneralPunctuation.js', - 'jax/output/HTML-CSS/autoload/mtable.js', - 'jax/output/HTML-CSS/fonts/TeX/AMS/Regular/MiscTechnical.js', - 'jax/output/HTML-CSS/fonts/TeX/Typewriter/Regular/Main.js', - 'jax/element/mml/optable/MathOperators.js', - 'jax/output/HTML-CSS/autoload/multiline.js', - 'jax/output/HTML-CSS/fonts/TeX/AMS/Regular/MathOperators.js', - 'jax/element/mml/optable/GeneralPunctuation.js' -]; - -module.exports = getLoadedMathJaxJsFilesInfo(mathJaxJsFiles, cdnPrefix, unpackedLocationPrefix); diff --git a/swh/web/assets/config/webpack.config.development.js b/swh/web/assets/config/webpack.config.development.js --- a/swh/web/assets/config/webpack.config.development.js +++ b/swh/web/assets/config/webpack.config.development.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018-2019 The Software Heritage developers + * Copyright (C) 2018-2020 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information @@ -21,8 +21,6 @@ const GenerateWebLabelsPlugin = require('./webpack-plugins/generate-weblabels-webpack-plugin'); const ProgressBarPlugin = require('progress-bar-webpack-plugin'); -const loadedMathJaxJsFiles = require('./mathjax-js-files'); - // are we running webpack-dev-server ? const isDevServer = process.argv.find(v => v.includes('webpack-dev-server')); // webpack-dev-server configuration @@ -319,8 +317,9 @@ }] } ], - // tell webpack to not parse minified pdfjs file to speedup build process - noParse: [path.resolve(nodeModules, 'pdfjs-dist/build/pdf.min.js')] + // tell webpack to not parse already minified files to speedup build process + noParse: [path.resolve(nodeModules, 'pdfjs-dist/build/pdf.min.js'), + path.resolve(nodeModules, 'mathjax/es5/tex-mml-chtml.js')] }, // webpack plugins plugins: [ @@ -369,10 +368,17 @@ }), // needed in order to use pdf.js new webpack.IgnorePlugin(/^\.\/pdf.worker.js$/), - new CopyWebpackPlugin([{ - from: path.resolve(nodeModules, 'pdfjs-dist/build/pdf.worker.min.js'), - to: path.resolve(__dirname, '../../../../static/js/') - }]), + new CopyWebpackPlugin([ + { + from: path.resolve(nodeModules, 'pdfjs-dist/build/pdf.worker.min.js'), + to: path.resolve(__dirname, '../../../../static/js/') + }, + { + from: path.resolve(nodeModules, 'mathjax/es5/output/chtml/fonts/woff-v2/**'), + to: path.resolve(__dirname, '../../../../static/fonts/'), + flatten: true + } + ]), new GenerateWebLabelsPlugin({ outputType: 'json', exclude: ['mini-css-extract-plugin', @@ -408,8 +414,7 @@ 'licenseFilePath': './LICENSE' } ] - }, - loadedMathJaxJsFiles + } ) }), new ProgressBarPlugin({ diff --git a/swh/web/assets/src/bundles/webapp/index.js b/swh/web/assets/src/bundles/webapp/index.js --- a/swh/web/assets/src/bundles/webapp/index.js +++ b/swh/web/assets/src/bundles/webapp/index.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018-2019 The Software Heritage developers + * Copyright (C) 2018-2020 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information @@ -26,3 +26,4 @@ export * from './history-counters'; export * from './badges'; export * from './sentry'; +export * from './math-typesetting'; diff --git a/swh/web/assets/src/bundles/webapp/math-typesetting.js b/swh/web/assets/src/bundles/webapp/math-typesetting.js new file mode 100644 --- /dev/null +++ b/swh/web/assets/src/bundles/webapp/math-typesetting.js @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2020 The Software Heritage developers + * See the AUTHORS file at the top-level directory of this distribution + * License: GNU Affero General Public License version 3, or any later version + * See top-level LICENSE file for more information + */ + +import {staticAsset} from 'utils/functions'; + +export async function typesetMath() { + + window.MathJax = { + chtml: { + fontURL: staticAsset('fonts/') + }, + tex: { + tags: 'ams', + useLabelIds: true, + inlineMath: [ ['$', '$'], ['\\(', '\\)'] ], + displayMath: [ ['$$', '$$'], ['\\[', '\\]'] ], + processEscapes: true, + processEnvironments: true + } + }; + + await import(/* webpackChunkName: "mathjax" */ 'utils/mathjax'); +} diff --git a/swh/web/assets/src/bundles/webapp/notebook-rendering.js b/swh/web/assets/src/bundles/webapp/notebook-rendering.js --- a/swh/web/assets/src/bundles/webapp/notebook-rendering.js +++ b/swh/web/assets/src/bundles/webapp/notebook-rendering.js @@ -1,5 +1,5 @@ /** - * Copyright (C) 2019 The Software Heritage developers + * Copyright (C) 2019-2020 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information @@ -120,40 +120,6 @@ nb.highlighter = highlightCode; nb.ansi = renderAnsi; - function initMathJax() { - - // same config as in nbviewer - window.MathJax = { - TeX: { - equationNumbers: { - autoNumber: 'AMS', - useLabelIds: true - } - }, - tex2jax: { - inlineMath: [ ['$', '$'], ['\\(', '\\)'] ], - displayMath: [ ['$$', '$$'], ['\\[', '\\]'] ], - processEscapes: true, - processEnvironments: true - }, - displayAlign: 'center', - 'HTML-CSS': { - styles: {'.MathJax_Display': {'margin': 0}}, - linebreaks: {automatic: true} - } - }; - - // MathJax is not easily webpackable in its current version - // (https://github.com/mathjax/MathJax/issues/1629) - // and is quite a monster regarding the number of files to distribute. - // So we will load it through a CDN for commodity of use here. - let head = document.getElementsByTagName('head')[0]; - let script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML'; - head.appendChild(script); - } - fetch(nbJsonUrl) .then(response => response.json()) .then(nbJson => { @@ -166,6 +132,6 @@ // set light red background color for stderr output cells $('pre.nb-stderr').parent().css('background', '#fdd'); // load MathJax library for math typesetting - initMathJax(); + swh.webapp.typesetMath(); }); } diff --git a/swh/web/assets/src/utils/mathjax.js b/swh/web/assets/src/utils/mathjax.js new file mode 100644 --- /dev/null +++ b/swh/web/assets/src/utils/mathjax.js @@ -0,0 +1,8 @@ +/** + * Copyright (C) 2020 The Software Heritage developers + * See the AUTHORS file at the top-level directory of this distribution + * License: GNU Affero General Public License version 3, or any later version + * See top-level LICENSE file for more information + */ + +export * from 'mathjax/es5/tex-mml-chtml'; diff --git a/yarn.lock b/yarn.lock --- a/yarn.lock +++ b/yarn.lock @@ -9522,6 +9522,11 @@ resolved "https://registry.yarnpkg.com/marked/-/marked-0.7.0.tgz#b64201f051d271b1edc10a04d1ae9b74bb8e5c0e" integrity sha512-c+yYdCZJQrsRjTPhUx7VKkApw9bwDkNbHUKo1ovgcfDjb2kc8rLuRbIFyXL5WOEUwzSSKo3IXpph2K6DqB/KZg== +mathjax@^3.0.5: + version "3.0.5" + resolved "https://registry.yarnpkg.com/mathjax/-/mathjax-3.0.5.tgz#707e703a9c1d95f0790bbd404b895566f459d514" + integrity sha512-9M7VulhltkD8sIebWutK/VfAD+m+6BIFqfpjDh9Pz/etoKUtjO6UMnOhUcDmNl6iApE8C9xrUmaMyNZkZAlrMw== + mathml-tag-names@^2.1.3: version "2.1.3" resolved "https://registry.yarnpkg.com/mathml-tag-names/-/mathml-tag-names-2.1.3.tgz#4ddadd67308e780cf16a47685878ee27b736a0a3"