mirror of
https://github.com/TiddlyWiki/TiddlyWiki5.git
synced 2026-04-29 05:06:36 +00:00
First pass at KaTeX plugin
Finally fixes #458 thanks to the KaTeX plugin from Khan Academy
This commit is contained in:
61
plugins/tiddlywiki/katex/latex-parser.js
Normal file
61
plugins/tiddlywiki/katex/latex-parser.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/katex/latex-parser.js
|
||||
type: application/javascript
|
||||
module-type: wikirule
|
||||
|
||||
Wiki text inline rule for LaTeX. For example:
|
||||
|
||||
```
|
||||
$$latex-goes-here$$
|
||||
```
|
||||
|
||||
This wikiparser can be modified using the rules eg:
|
||||
|
||||
```
|
||||
\rules except latex-parser
|
||||
\rules only latex-parser
|
||||
```
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.name = "latex-parser";
|
||||
exports.types = {inline: true};
|
||||
|
||||
exports.init = function(parser) {
|
||||
this.parser = parser;
|
||||
// Regexp to match
|
||||
this.matchRegExp = /\$\$(?!\$)/mg;
|
||||
};
|
||||
|
||||
exports.parse = function() {
|
||||
// Move past the match
|
||||
this.parser.pos = this.matchRegExp.lastIndex;
|
||||
var reEnd = /\$\$/mg;
|
||||
// Look for the end marker
|
||||
reEnd.lastIndex = this.parser.pos;
|
||||
var match = reEnd.exec(this.parser.source),
|
||||
text;
|
||||
// Process the text
|
||||
if(match) {
|
||||
text = this.parser.source.substring(this.parser.pos,match.index);
|
||||
this.parser.pos = match.index + match[0].length;
|
||||
} else {
|
||||
text = this.parser.source.substr(this.parser.pos);
|
||||
this.parser.pos = this.parser.sourceLength;
|
||||
}
|
||||
return [{
|
||||
type: "latex",
|
||||
attributes: {
|
||||
text: {
|
||||
type: "text",
|
||||
value: text
|
||||
}}
|
||||
}];
|
||||
};
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user