First pass at KaTeX plugin

Finally fixes #458 thanks to the KaTeX plugin from Khan Academy
This commit is contained in:
Jermolene 2014-09-21 23:03:07 +01:00
parent d047ccdc84
commit 96b7d0eeba
49 changed files with 565 additions and 0 deletions

View file

@ -0,0 +1,42 @@
# [<img src="https://khan.github.io/KaTeX/katex-logo.svg" width="130" alt="KaTeX">](http://khan.github.io/KaTeX/) [![Build Status](https://travis-ci.org/Khan/KaTeX.svg?branch=master)](https://travis-ci.org/Khan/KaTeX)
KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web.
* **Fast:** KaTeX renders its math synchronously and doesn't need to reflow the page. See how it compares to a competitor in [this speed test](http://jsperf.com/katex-vs-mathjax/).
* **Print quality:** KaTeXs layout is based on Donald Knuths TeX, the gold standard for math typesetting.
* **Self contained:** KaTeX has no dependencies and can easily be bundled with your website resources.
* **Server side rendering:** KaTeX produces the same output regardless of browser or environment, so you can pre-render expressions using Node.js and send them as plain HTML.
KaTeX supports all major browsers, including Chrome, Safari, Firefox, Opera, and IE 8 - IE 11.
## Usage
Download the built files from [the releases page](https://github.com/khan/katex/releases). Include the `katex.min.js` and `katex.min.css` files on your page:
```html
<link rel="stylesheet" type="text/css" href="/path/to/katex.min.css">
<script src="/path/to/katex.min.js" type="text/javascript"></script>
```
Call `katex.render` with a TeX expression and a DOM element to render into:
```js
katex.render("c = \\pm\\sqrt{a^2 + b^2}", element);
```
To generate HTML on the server, you can use `katex.renderToString`:
```js
var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}");
// '<span class="katex">...</span>'
```
Make sure to include the CSS and font files, but there is no need to include the JavaScript.
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md)
## License
KaTeX is licenced under the [MIT License](http://opensource.org/licenses/MIT).

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

View file

@ -0,0 +1,84 @@
{
"tiddlers": [
{
"file": "fonts/KaTeX_AMS-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_AMS-Regular.woff"
}
},{
"file": "fonts/KaTeX_Main-Bold.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Bold.woff"
}
},{
"file": "fonts/KaTeX_Main-Italic.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Italic.woff"
}
},{
"file": "fonts/KaTeX_Main-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Regular.woff"
}
},{
"file": "fonts/KaTeX_Math-BoldItalic.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-BoldItalic.woff"
}
},{
"file": "fonts/KaTeX_Math-Italic.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-Italic.woff"
}
},{
"file": "fonts/KaTeX_Math-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-Regular.woff"
}
},{
"file": "fonts/KaTeX_Size1-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size1-Regular.woff"
}
},{
"file": "fonts/KaTeX_Size2-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size2-Regular.woff"
}
},{
"file": "fonts/KaTeX_Size3-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size3-Regular.woff"
}
},{
"file": "fonts/KaTeX_Size4-Regular.woff",
"fields": {
"type": "application/font-woff",
"title": "$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size4-Regular.woff"
}
},{
"file": "katex.without-font-face.min.css",
"fields": {
"type": "text/plain",
"title": "$:/plugins/tiddlywiki/katex/katex.min.css"
}
},{
"file": "katex.min.js",
"fields": {
"type": "application/javascript",
"title": "$:/plugins/tiddlywiki/katex/katex.min.js",
"module-type": "library"
}
}
]
}

View 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
}}
}];
};
})();

View file

@ -0,0 +1,5 @@
{
"title": "$:/plugins/tiddlywiki/katex",
"description": "Mathematics typesetting plugin wrapping KaTeX",
"author": "JeremyRuston"
}

View file

@ -0,0 +1,178 @@
title: $:/plugins/tiddlywiki/katex/styles
tags: [[$:/tags/Stylesheet]]
\rules only filteredtranscludeinline transcludeinline macrodef macrocallinline
/* KaTeX styles */
{{$:/plugins/tiddlywiki/katex/katex.min.css}}
/* Override font URLs */
@font-face {
font-family: 'KaTeX_AMS';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_AMS-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Caligraphic';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Caligraphic-Bold.woff'>>) format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Caligraphic';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Caligraphic-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Fraktur';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Fraktur-Bold.woff'>>) format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Fraktur';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Fraktur-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Greek';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Greek-Bold.woff'>>) format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Greek';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Greek-BoldItalic.woff'>>) format('woff');
font-weight: bold;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_Greek';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Greek-Italic.woff'>>) format('woff');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_Greek';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Greek-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Main';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Bold.woff'>>) format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Main';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Italic.woff'>>) format('woff');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_Main';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Main-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Math';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-BoldItalic.woff'>>) format('woff');
font-weight: bold;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_Math';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-Italic.woff'>>) format('woff');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_Math';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Math-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_SansSerif';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_SansSerif-Bold.woff'>>) format('woff');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_SansSerif';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_SansSerif-Italic.woff'>>) format('woff');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'KaTeX_SansSerif';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_SansSerif-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Script';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Script-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Size1';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size1-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Size2';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size2-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Size3';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size3-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Size4';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Size4-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'KaTeX_Typewriter';
src: url(<<datauri '$:/plugins/tiddlywiki/katex/fonts/KaTeX_Typewriter-Regular.woff'>>) format('woff');
font-weight: normal;
font-style: normal;
}

View file

@ -0,0 +1,69 @@
/*\
title: $:/plugins/tiddlywiki/katex/wrapper.js
type: application/javascript
module-type: widget
Wrapper for `katex.min.js` that provides a `<$latex>` widget. It is also available under the alias `<$katex>`
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var katex = require("$:/plugins/tiddlywiki/katex/katex.min.js"),
Widget = require("$:/core/modules/widgets/widget.js").widget;
var KaTeXWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
/*
Inherit from the base widget class
*/
KaTeXWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
KaTeXWidget.prototype.render = function(parent,nextSibling) {
// Housekeeping
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
// Get the source text
var text = this.getAttribute("text",this.parseTreeNode.text || "");
// Render it into a span
var span = this.document.createElement("span");
katex.render(text,span);
// Insert it into the DOM
parent.insertBefore(span,nextSibling);
this.domNodes.push(span);
};
/*
Compute the internal state of the widget
*/
KaTeXWidget.prototype.execute = function() {
// Nothing to do for a katex widget
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
KaTeXWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.text) {
this.refreshSelf();
return true;
} else {
return false;
}
};
exports.latex = KaTeXWidget;
exports.katex = KaTeXWidget;
})();