Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/anishmprasad/mathex

A Mathematical Expression Composer for CKEditor
https://github.com/anishmprasad/mathex

ckeditor ckeditor-plugin ckeditor4 mathex

Last synced: about 9 hours ago
JSON representation

A Mathematical Expression Composer for CKEditor

Awesome Lists containing this project

README

        

# MathEx

A CKEditor Plugin for MathML and Latex Mathemathical Expressions

Editor accepted string format from input

Download mathex plugin from [CKEditor Addons](https://ckeditor.com/cke4/addon/MathEx)

default class was math if you change this you need to configure through ``` mathexClass ```

```html

\({ MathML or Tex Mathemathical expressions }\)

```

Encoder Helper

``` let str = "a+b2" ```

``` this helper function added escape character before and html Decode for editor readable format ```

``` decoder(str) // \( >math<>mrow<>msup<>mfenced<>mrow<>mi<a>/mi<>mo<+>/mo<>mi<b>/mi<>/mrow<>/mfenced<>mn>2&gt/mn<>/msup<>/mrow<>/math> \) ```

```javascript

function encoder(text){
if (text){
var myregexp = /]+?class="math".*?>([\s\S]*?)<\/span>/g;
return text.replace(myregexp, function replacer(match) {
return match.replace(/([\s\S]*?)<\/math>/g , function replacerData(match) {
let tempString = match.replace(//g, "\\(");
return this.htmlEncode(tempString.replace(/<\/math>/g, "\\)"))
}.bind(this))
}.bind(this))
}
}

```

Decoder Helper

``` let str = "\({ MathML or Tex Mathemathical expressions }\)" ```

``` this helper function removed escape character before ```

``` decoder(str) // MathML or Tex Mathemathical expressions ```

```javascript

function decoder(str) {
let tempString = str.replace(/\\\(/g, "");
return tempString.replace(/<\/math>\\\)/g, "");
}

```

htmlEncode Helper

``` htmlEncode( 'A > B & C < D' ) // 'A > B & C < D' ```

```javascript

var ampRegex = /&/g,
gtRegex = />/g,
ltRegex = / B & C < D' ) ); // 'A > B & C < D'
*
* @param {String} text The string to be encoded.
* @returns {String} The encoded string.
*/
htmlEncode = text => {
// Backwards compatibility - accept also non-string values (casting is done below).
// Since 4.4.8 we return empty string for null and undefined because these values make no sense.
if (text === undefined || text === null) {
return '';
}

return String(text).replace(ampRegex, '&').replace(gtRegex, '>').replace(ltRegex, '<');
}

```

htmlDecode Helper

``` htmlDecode( '<a & b >' ) // '' ```

```javascript

var ampRegex = /&/g,
gtRegex = />/g,
ltRegex = /',
amp: '&',
quot: '"',
nbsp: '\u00a0',
shy: '\u00ad'
}

allEscDecode(match, code) {
if (code[0] == '#') {
return String.fromCharCode(parseInt(code.slice(1), 10));
} else {
return namedEntities[code];
}
}

/**
* Decodes HTML entities that browsers tend to encode when used in text nodes.
*
* console.log( htmlDecode( '<a & b >' ) ); // '
'
*
* Read more about chosen entities in the [research].
*
* @param {String} The string to be decoded.
* @returns {String} The decoded string.
*/
htmlDecode = text => {
// See:
// * http://jsperf.com/wth-is-going-on-with-jsperf JSPerf has some serious problems, but you can observe
// that combined regexp tends to be quicker (except on V8). It will also not be prone to fail on '&lt;'
return text.replace(allEscRegex, this.allEscDecode);
}

```
### Licence
GPL 2.0