Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #43 from pkra/wilbowma
Browse files Browse the repository at this point in the history
Basic support for MathJax custom script types
  • Loading branch information
pkra authored May 2, 2017
2 parents 9012659 + 2478b91 commit 260cd7d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ exports.mjpage = function(htmlstring, configOptions, typesetOptions, callback) {
const window = doc.defaultView;
const document = window.document;

//rewrite custom scripts types from core MathJax
const rewriteScripts = function(oldType, newType){
const scripts = document.querySelectorAll('script[type="' + oldType + '"]');
for (let script of scripts) script.setAttribute('type', newType);
}
rewriteScripts('math/tex','math/inline-TeX');
rewriteScripts('math/tex; mode=display','math/TeX');
rewriteScripts('math/asciimath','math/asciiMath');

// configure mathjax-node
mathjax.config(config);

Expand Down Expand Up @@ -114,8 +123,13 @@ exports.mjpage = function(htmlstring, configOptions, typesetOptions, callback) {
window.ascii.PreProcess();
}

const scripts = document.querySelectorAll(`
script[type="math/TeX"],
script[type="math/inline-TeX"],
script[type="math/AsciiMath"],
script[type="math/MathML"],
script[type="math/MathML-block"]`);
// convert with mathjax-node
const scripts = document.querySelectorAll('script[type^="math/"]');
const process = function(index) {
const script = scripts[index];
if (script) {
Expand Down Expand Up @@ -213,7 +227,7 @@ exports.mjpage = function(htmlstring, configOptions, typesetOptions, callback) {
border: 0;
margin: 0
}
.mjpage__block {
text-align: center;
margin: 1em 0em;
Expand Down
51 changes: 51 additions & 0 deletions test/script-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const tape = require('tape');
const mjpage = require('../lib/main.js').mjpage;
const jsdom = require('jsdom').jsdom;

tape('Custom script types are processed correctly', function(t) {
t.plan(4);
const inlineInput = '<script type="math/tex"> e^{i\\pi} = -1</script>';
mjpage(inlineInput, {
format: ['TeX']
}, {
svg: true
}, function(output) {
const window = jsdom(output).defaultView;
const expected = window.document.querySelector('.mjpage');
t.ok(expected, '"math/tex" treated as inline');
});

const displayInput = '<script type="math/tex; mode=display"> e^{i\\pi} = -1</script>';
mjpage(displayInput, {
format: ['TeX']
}, {
svg: true
}, function(output) {
const window = jsdom(output).defaultView;
const expected = window.document.querySelector('.mjpage__block');
t.ok(expected, '"math/tex; mode=display" treated as block');
});

const asciiInput = '<script type="math/asciimath">alpha</script>';
mjpage(displayInput, {
format: ['TeX']
}, {
svg: true
}, function(output) {
const window = jsdom(output).defaultView;
const expected = window.document.querySelector('.mjpage');
t.ok(expected, '"math/asciimath" treated as asciimath');
});

const crazyCaseInput = '<script type="math/blarg"> e^{i\\pi} = -1</script>';
mjpage(crazyCaseInput, {
format: ['TeX']
}, {
svg: true
}, function(output) {
const window = jsdom(output).defaultView;
const expected = window.document.querySelector('.mjpage');
t.notOk(expected, 'Other scripts are ignored');
});

});

0 comments on commit 260cd7d

Please sign in to comment.