Initial commit

This commit is contained in:
Sam
2017-07-06 16:50:38 -04:00
commit 5b22a196fa
1021 changed files with 24150 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
## Discourse Math plugin
Adds MathJax support to your Discourse instance.
@@ -0,0 +1,72 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
import loadScript from 'discourse/lib/load-script';
let initializedMathJax = false;
function initMathJax() {
if (initializedMathJax) { return; }
window.MathJax = {
jax: ['input/TeX', 'input/AsciiMath', 'input/MathML', 'output/CommonHTML'],
TeX: {extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"]},
extensions: ["toMathML.js"],
showProcessingMessages: false,
root: '/plugins/discourse-math/mathjax'
};
initializedMathJax = true;
}
function ensureMathJax(){
initMathJax();
return loadScript('/plugins/discourse-math/mathjax/MathJax.1.7.1.js');
}
function decorate(elem, isPreview){
const $elem= $(elem);
const tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : "";
const $mathWrapper = $(`<${tag} style="display: none;"><script type="math/tex${display}"></script></${tag}>`);
const $math = $mathWrapper.children();
$math.html($elem.text());
$elem.after($mathWrapper);
Em.run.later(this, ()=> {
window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM
if (elem.parentElement.offsetParent !== null) {
window.MathJax.Hub.Typeset($math[0], ()=> {
$elem.remove();
$mathWrapper.show();
});
}
});
}, isPreview ? 200 : 0);
}
function mathjax($elem) {
const mathElems = $elem.find('.math');
if (mathElems.length > 0) {
const isPreview = $elem.hasClass('d-editor-preview');
ensureMathJax().then(()=>{
mathElems.each((idx,elem) => decorate(elem, isPreview));
});
}
}
function initializeMath(api) {
api.decorateCooked(mathjax);
}
export default {
name: "apply-math",
initialize(container) {
const siteSettings = container.lookup('site-settings:main');
if (siteSettings.discourse_math_enabled) {
withPluginApi('0.5', initializeMath);
}
}
};
@@ -0,0 +1,157 @@
// inspired by https://github.com/classeur/markdown-it-mathjax/blob/master/markdown-it-mathjax.js
//
//
//
function isSafeBoundary(code, md) {
if (code === 36) {
return false;
}
if (md.utils.isWhiteSpace(code)) {
return true;
}
if (md.utils.isMdAsciiPunct(code)) {
return true;
}
if (md.utils.isPunctChar(code)) {
return true;
}
return false;
}
function inlineMath(state, silent) {
let pos = state.pos,
posMax = state.posMax;
if (silent || state.src.charCodeAt(pos) !== 36 /* $ */ || posMax < pos+2) {
return false;
}
// too short
if (state.src.charCodeAt(pos+1) === 36 /* $ */) {
return false;
}
if (pos > 0) {
let prev = state.src.charCodeAt(pos-1);
if (!isSafeBoundary(prev, state.md)) {
return false;
}
}
let found;
for(let i=pos+1; i<posMax; i++) {
let code = state.src.charCodeAt(i);
if (code === 36 /* $ */ && state.src.charCodeAt(i-1) !== 92 /* \ */) {
found = i;
break;
}
}
if (!found) {
return false;
}
if (found+1 <= posMax) {
let next = state.src.charCodeAt(found+1);
if (!isSafeBoundary(next, state.md)) {
return false;
}
}
let data = (state.src.slice(pos+1,found));
let token = state.push('html_raw', '', 0);
const escaped = state.md.utils.escapeHtml(data);
token.content = `<span class='math'>${escaped}</span>`;
state.pos = found+1;
return true;
}
function isBlockMarker(state, start, max, md) {
if (state.src.charCodeAt(start) !== 36 /* $ */) {
return false;
}
start++;
if (state.src.charCodeAt(start) !== 36 /* $ */) {
return false;
}
start++;
// ensure we only have newlines after our $$
for(let i=start; i < max; i++) {
if (!md.utils.isSpace(state.src.charCodeAt(i))) {
return false;
}
}
return true;
}
function blockMath(state, startLine, endLine, silent){
let
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
if (!isBlockMarker(state, start, max, state.md)) {
return false;
}
if (silent) { return true; }
let nextLine = startLine;
let closed = false;
for (;;) {
nextLine++;
// unclosed $$ is considered math
if (nextLine >= endLine) {
break;
}
if (isBlockMarker(state, state.bMarks[nextLine] + state.tShift[nextLine], state.eMarks[nextLine], state.md)) {
closed = true;
break;
}
}
let token = state.push('html_raw', '', 0);
let endContent = closed ? state.eMarks[nextLine-1] : state.eMarks[nextLine];
let content = state.src.slice(state.bMarks[startLine+1] + state.tShift[startLine+1], endContent);
const escaped = state.md.utils.escapeHtml(content);
token.content = `<div class='math'>\n${escaped}\n</div>\n`;
state.line = closed ? nextLine+1 : nextLine;
return true;
}
export function setup(helper) {
if (!helper.markdownIt) {
return;
}
helper.registerOptions((opts, siteSettings) => {
opts.features.math = siteSettings.discourse_math_enabled;
});
helper.registerPlugin(md => {
md.inline.ruler.after('escape', 'math', inlineMath);
md.block.ruler.after('code', 'math', blockMath, {
alt: ['paragraph', 'reference', 'blockquote', 'list']
});
});
}
+6
View File
@@ -0,0 +1,6 @@
# encoding: utf-8
#
en:
site_settings:
discourse_math_enabled: 'Enable Discourse Math plugin (will add special processing to $ and $$ blocks)'
+4
View File
@@ -0,0 +1,4 @@
plugins:
discourse_math_enabled:
default: false
client: true
+6
View File
@@ -0,0 +1,6 @@
# the idea here is to support server rendering
# there is a node example and we could inject this in a post processing job
class DiscourseMath::MathRenderer
def self.render(mathjax, options={})
end
end
+6
View File
@@ -0,0 +1,6 @@
# name: discourse-math
# about: Official mathjax plugin for Discourse
# version: 0.9
# authors: Sam Saffron (sam)
enabled_site_setting :discourse_math_enabled
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/CHTML-preview.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Callback.Queue(["Require",MathJax.Ajax,"[MathJax]/extensions/fast-preview.js"],["loadComplete",MathJax.Ajax,"[MathJax]/extensions/CHTML-preview.js"]);
File diff suppressed because one or more lines are too long
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/HTML-CSS/handle-floats.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["HTML-CSS/handle-floats"]={version:"2.7.1"};MathJax.Hub.Startup.signal.Post("HTML-CSS handle-floats Ready");MathJax.Ajax.loadComplete("[MathJax]/extensions/HTML-CSS/handle-floats.js");
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+19
View File
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/HTML.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/HTML"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b=MathJax.InputJax.TeX;var a=b.Definitions;a.Add({macros:{href:"HREF_attribute","class":"CLASS_attribute",style:"STYLE_attribute",cssId:"ID_attribute"}},null,true);b.Parse.Augment({HREF_attribute:function(e){var d=this.GetArgument(e),c=this.GetArgumentMML(e);this.Push(c.With({href:d}))},CLASS_attribute:function(d){var e=this.GetArgument(d),c=this.GetArgumentMML(d);if(c["class"]!=null){e=c["class"]+" "+e}this.Push(c.With({"class":e}))},STYLE_attribute:function(d){var e=this.GetArgument(d),c=this.GetArgumentMML(d);if(c.style!=null){if(e.charAt(e.length-1)!==";"){e+=";"}e=c.style+" "+e}this.Push(c.With({style:e}))},ID_attribute:function(e){var d=this.GetArgument(e),c=this.GetArgumentMML(e);this.Push(c.With({id:d}))},GetArgumentMML:function(d){var c=this.ParseArg(d);if(c.inferred&&c.data.length==1){c=c.data[0]}else{delete c.inferred}return c}});MathJax.Hub.Startup.signal.Post("TeX HTML Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/HTML.js");
+19
View File
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/action.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/action"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b=MathJax.InputJax.TeX,a=MathJax.ElementJax.mml;b.Definitions.Add({macros:{toggle:"Toggle",mathtip:"Mathtip",texttip:["Macro","\\mathtip{#1}{\\text{#2}}",2]}},null,true);b.Parse.Augment({Toggle:function(d){var e=[],c;while((c=this.GetArgument(d))!=="\\endtoggle"){e.push(b.Parse(c,this.stack.env).mml())}this.Push(a.maction.apply(a,e).With({actiontype:a.ACTIONTYPE.TOGGLE}))},Mathtip:function(d){var c=this.ParseArg(d),e=this.ParseArg(d);this.Push(a.maction(c,e).With({actiontype:a.ACTIONTYPE.TOOLTIP}))}});MathJax.Hub.Startup.signal.Post("TeX action Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/action.js");
+19
View File
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/autobold.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/autobold"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var a=MathJax.InputJax.TeX;a.prefilterHooks.Add(function(d){var c=d.script.parentNode.insertBefore(document.createElement("span"),d.script);c.visibility="hidden";c.style.fontFamily="Times, serif";c.appendChild(document.createTextNode("ABCXYZabcxyz"));var b=c.offsetWidth;c.style.fontWeight="bold";if(b&&c.offsetWidth===b){d.math="\\boldsymbol{"+d.math+"}"}c.parentNode.removeChild(c)});MathJax.Hub.Startup.signal.Post("TeX autobold Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/autobold.js");
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/autoload-all.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/autoload-all"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var h={action:["mathtip","texttip","toggle"],AMSmath:["mathring","nobreakspace","negmedspace","negthickspace","intI","iiiint","idotsint","dddot","ddddot","sideset","boxed","substack","injlim","projlim","varliminf","varlimsup","varinjlim","varprojlim","DeclareMathOperator","operatorname","genfrac","tfrac","dfrac","binom","tbinom","dbinom","cfrac","shoveleft","shoveright","xrightarrow","xleftarrow"],begingroup:["begingroup","endgroup","gdef","global"],cancel:["cancel","bcancel","xcancel","cancelto"],color:["color","textcolor","colorbox","fcolorbox","definecolor"],enclose:["enclose"],extpfeil:["Newextarrow","xlongequal","xmapsto","xtofrom","xtwoheadleftarrow","xtwoheadrightarrow"],mhchem:["ce","cee","cf"]};var c={AMSmath:["subarray","smallmatrix","equation","equation*"],AMScd:["CD"]};var d,g,b,a={macros:{},environment:{}};for(d in h){if(h.hasOwnProperty(d)){if(!MathJax.Extension["TeX/"+d]){var f=h[d];for(g=0,b=f.length;g<b;g++){a.macros[f[g]]=["Extension",d]}}}}for(d in c){if(c.hasOwnProperty(d)){if(!MathJax.Extension["TeX/"+d]){var e=c[d];for(g=0,b=e.length;g<b;g++){a.environment[e[g]]=["ExtensionEnv",null,d]}}}}MathJax.InputJax.TeX.Definitions.Add(a);MathJax.Hub.Startup.signal.Post("TeX autoload-all Ready")});MathJax.Callback.Queue(["Require",MathJax.Ajax,"[MathJax]/extensions/TeX/AMSsymbols.js"],["loadComplete",MathJax.Ajax,"[MathJax]/extensions/TeX/autoload-all.js"]);
+19
View File
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/bbox.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/bbox"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b=MathJax.InputJax.TeX,a=MathJax.ElementJax.mml;b.Definitions.Add({macros:{bbox:"BBox"}},null,true);b.Parse.Augment({BBox:function(e){var p=this.GetBrackets(e,""),o=this.ParseArg(e);var k=p.split(/,/),g,d,c;for(var l=0,j=k.length;l<j;l++){var f=k[l].replace(/^\s+/,"").replace(/\s+$/,"");var n=f.match(/^(\.\d+|\d+(\.\d*)?)(pt|em|ex|mu|px|in|cm|mm)$/);if(n){if(g){b.Error(["MultipleBBoxProperty","%1 specified twice in %2","Padding",e])}var h=this.BBoxPadding(n[1]+n[3]);if(h){g={height:"+"+h,depth:"+"+h,lspace:h,width:"+"+(2*n[1])+n[3]}}}else{if(f.match(/^([a-z0-9]+|\#[0-9a-f]{6}|\#[0-9a-f]{3})$/i)){if(d){b.Error(["MultipleBBoxProperty","%1 specified twice in %2","Background",e])}d=f}else{if(f.match(/^[-a-z]+:/i)){if(c){b.Error(["MultipleBBoxProperty","%1 specified twice in %2","Style",e])}c=this.BBoxStyle(f)}else{if(f!==""){b.Error(["InvalidBBoxProperty","'%1' doesn't look like a color, a padding dimension, or a style",f])}}}}}if(g){o=a.mpadded(o).With(g)}if(d||c){o=a.mstyle(o).With({mathbackground:d,style:c})}this.Push(o)},BBoxStyle:function(c){return c},BBoxPadding:function(c){return c}});MathJax.Hub.Startup.signal.Post("TeX bbox Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/bbox.js");
File diff suppressed because one or more lines are too long
@@ -0,0 +1,19 @@
/*
* /MathJax/extensions/TeX/boldsymbol.js
*
* Copyright (c) 2009-2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Extension["TeX/boldsymbol"]={version:"2.7.1"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var a=MathJax.ElementJax.mml;var d=MathJax.InputJax.TeX;var b=d.Definitions;var c={};c[a.VARIANT.NORMAL]=a.VARIANT.BOLD;c[a.VARIANT.ITALIC]=a.VARIANT.BOLDITALIC;c[a.VARIANT.FRAKTUR]=a.VARIANT.BOLDFRAKTUR;c[a.VARIANT.SCRIPT]=a.VARIANT.BOLDSCRIPT;c[a.VARIANT.SANSSERIF]=a.VARIANT.BOLDSANSSERIF;c["-tex-caligraphic"]="-tex-caligraphic-bold";c["-tex-oldstyle"]="-tex-oldstyle-bold";b.Add({macros:{boldsymbol:"Boldsymbol"}},null,true);d.Parse.Augment({mmlToken:function(f){if(this.stack.env.boldsymbol){var e=f.Get("mathvariant");if(e==null){f.mathvariant=a.VARIANT.BOLD}else{f.mathvariant=(c[e]||e)}}return f},Boldsymbol:function(h){var e=this.stack.env.boldsymbol,f=this.stack.env.font;this.stack.env.boldsymbol=true;this.stack.env.font=null;var g=this.ParseArg(h);this.stack.env.font=f;this.stack.env.boldsymbol=e;this.Push(g)}});MathJax.Hub.Startup.signal.Post("TeX boldsymbol Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/boldsymbol.js");

Some files were not shown because too many files have changed in this diff Show More