Commit Graph

34 Commits

Author SHA1 Message Date
Emily Eisenberg
aaeab1200c Add MathML rendering to improve accessibility
Summary:
This adds support for rendering KaTeX to both HTML and MathML
with the intent of improving accessibility. To accomplish this, both
MathML and HTML are rendered, but with the MathML visually hidden and
the HTML spans aria-hidden. Hopefully, this should produce much better
accessibility for KaTeX.

Should fix/improve #38

Closes #189

Test Plan:
 - Ensure all the tests, and the new tests, still pass.
 - Ensure that for each of the group types in `buildHTML.js`, there is a
   corresponding one in `buildMathML.js`.
 - Ensure that the huxley screenshots didn't change (except for
   BinomTest, which changed because I fixed a bug in `buildHTML` where
   `genfrac` didn't have a `groupToType` mapping).
 - Run ChromeVox on the test page, render some math. (for example,
   `\sqrt{x^2}`)
   - Ensure that a mathy-sounding expression is read. (I hear "group
     square root of x squared math").
   - Ensure that nothing else is read (like no "x" or "2").
 - Ensure that MathML markup is generated correctly and is interpreted
   by the browser correctly by running
   `document.getElementById("math").innerHTML =
   katex.renderToString("\\sqrt{x^2}");` and seeing that the same speech
   is read.

Reviewers: john, alpert

Reviewed By: john, alpert

Subscribers: alpert, john

Differential Revision: https://phabricator.khanacademy.org/D16373
2015-03-01 18:33:20 -08:00
Emily Eisenberg
fd18f6979e Add an optional settings argument to render calls
Summary:
Add the ability to pass in options to the render calls which contain information about the parse. This information is passed around to the parser and builder, which parse and render differently depending on the options. Currently, this includes an option to render the math in display mode (i.e. centered, block level, and in displaystyle).

Also added some changes to make it easier to add new data to functions (now that new data doesn't need to be copied into the ParseFuncOrArg data structure, it is looked up when it is needed) and has more sane support for the `'original'` argType (as suggested by pull request #93).

Test Plan:
 - Make sure tests and lint pass
 - Make sure huxley screenshots didn't change, and new screenshot looks correct

Reviewers: alpert

Reviewed By: alpert

Differential Revision: https://phabricator.khanacademy.org/D13810
2015-02-19 15:26:57 -08:00
Emily Eisenberg
ea5ee95dbe Warn when the website is in quirks mode
Summary:
KaTeX doesn't work correctly in quirks mode. Warn in the console and disable
rendering if that happens.

Test Plan:
 - Make sure the test still loads and renders math
 - Make sure a warning is thrown when the doctype is removed, and no more math
   is rendered
 - Make sure the tests pass both on the web and with `make test`

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D13192
2014-09-17 15:13:56 -07:00
Emily Eisenberg
35d9d972fd Move js files into src/
Test plan:
- Make sure huxley tests, jasmine tests, make build, make metrics, make test all
  still work.

Auditors: alpert
2014-09-15 02:50:34 -07:00
Emily Eisenberg
6b674873ea Rename katex.process to katex.render
Test plan:
 - Make sure tests work and huxley tests didn't change

Auditors: alpert
2014-09-14 21:20:27 -07:00
Emily Eisenberg
f63af87f17 Add looots of comments
Summary:
Add comments everywhere! Also fix some small bugs like using Style.id
instead of Style.size, and rename some variables to be more descriptive.

Fixes #22

Test Plan:
 - Make sure the huxley screenshots didn't change
 - Make sure the tests still pass

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D13158
2014-09-14 19:23:39 -07:00
Ben Alpert
7df5b4bba8 Add code for generating HTML
Test Plan: Ran unit tests. Looked at `\blue{\displaystyle \left(\dfrac{a^\sigma}{\sin \theta}\right\Updownarrow \intop_{1/2}^{z^z} \sum_{i=0}^\infty x \,dx}` in Chrome and saw the future in my eyes.

Reviewers: emily

Reviewed By: emily

Subscribers: jessie

Differential Revision: http://phabricator.khanacademy.org/D13154
2014-09-12 17:59:26 -07:00
Emily Eisenberg
403dca64ab Improve testing
Summary:
Move dom creation into katex.js so our tests can test non-dom things, and add
some buildTree tests. Add some checks make utils.js work in node. Add support
for jasmine-node, to allow for command line unit testing.

Test Plan:
- Make sure tests work, in both the browser and with `make test`
- Make sure huxley screenshots didn't change

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D13125
2014-09-12 13:30:30 -07:00
Ben Eater
62af02bd4e Empty the output element before parsing the input
If there's an exception while parsing (because it's something katex doesn't support), the output doesn't get cleared. Typically when this happens, we'll fall back to MathJax and render that in a different element, leaving the previous KaTex-formatted math plus the new MathJax-formatted math next to each other in the page, which is never what you'd want.

Test Plan: Using the fallback mechanism in khan-exercises utils/tex.js, render something katex can deal with, like "3", then using the same element, reprocess with something katex can't deal with, like "\approx 3". Previously the result would be "3 \approx 3" (with the first 3 being left over from the original katex rendering. With this change, only the "\approx 3" remains on the page.

Auditors: alex, alpert
2013-11-12 14:09:13 -08:00
Emily Eisenberg
0a3a2271f4 Add ability to calculate heights/depths
Summary:
This diff does a couple different things:

 - There is now a metrics/ folder, which contains the property files describing
   the metrics if the fonts, as well as a script for reading and printing the
   metrics in javascript.
 - Fractions and superscripts/subscripts are now rendered in slightly different
   ways now (notably, no use of inline-table). This allows for much more precise
   positioning of the superscripts, subscripts, numerators, and denominators,
   while still having an appropriate baseline. Also, there is no longer a
   sup/sub/supsub distinction, there are only supsubs with null sup/sub.
 - Using the new font metrics and by implementing the formulas found in The TeX
   Book, Appendix G, the heights and depths of all of the sub-expressions in a
   formula are now calculated. These are currently used to:
    - Correctly position superscripts, subscripts, numerators, and denominators
    - Adjust the height and depth of the overall expression so it takes up the
      appropriate space
 - Because we have to add attributes (height and depth) to every attribute, I
   have changed the way DOM nodes are assembled. Now, instead of assembling the
   DOM elements inline (which is a problem because we need to track
   height/depth, and we shouldn't (and can't in IE 8) attach raw attributes to DOM
   nodes), we assemble a pseudo-DOM structure with the extra information, and
   then actually assemble it at the very end.

The main page also now has an updated expression to show off and test the new
and improved parsing.

Test Plan:
View the main page, make sure that the expression renders. Make sure
that the tests pass. Make sure that expressions have the correct calculated
height (this is most easily tested by viewing them on the main page and making
sure that the top of the expression lines up with the bottom of the input box).

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3442
2013-08-13 18:16:43 -07:00
Emily Eisenberg
cfb9f2b31b Refactor some stuff
Summary:
Pull node making into a separate module, make an "options" param for
the make_ functions, and pull the different types of groups into separate
functions.

Test Plan: Open the homepage, make sure everything still works.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3368
2013-08-06 14:12:06 -07:00
Ben Alpert
6883017bc4 Look at WebKit version instead of checking Safari
Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3317
2013-08-01 17:43:06 -07:00
Ben Alpert
cab5af40b6 Always throw real ParseErrors
Also sneak in a colon.

Auditors: emily
2013-07-28 17:54:41 -07:00
Emily Eisenberg
e744b02090 Move main node cleanup back
Summary: If someone wants the node to be cleared out, they will have to
do it themselves.

Auditors: alpert
2013-07-26 13:41:29 -07:00
Emily Eisenberg
62b4e68a1c Make safari only break on fractions
Summary: Also, make errors on the initial parse not prevent further
parses.

Auditors: alpert
2013-07-25 19:15:16 -07:00
Emily Eisenberg
905fb7fb71 Don't run in Safari
Summary: Safari has weird bugs associated with inline-table and
vertical-align, so we just won't render in Safari.

Auditors: alpert
2013-07-25 18:59:20 -07:00
Emily Eisenberg
79a50b3abe Make the linter happy
Auditors: alpert
2013-07-24 20:37:56 -07:00
Emily Eisenberg
fa101db4e1 Make katex always clean up after itself
Summary: This makes it more annoying to use locally, because it goes
away when you type something bad, but is much more useful in use in
khan-exercises and perseus.

Auditors: alpert
2013-07-24 14:49:21 -07:00
Ben Alpert
dd8923deca Dollar sign
Auditors: emily
2013-07-20 18:23:15 -07:00
Ben Alpert
41ad81d831 Make uppercase Greek letters use roman font
Fixes T1228.

Test Plan:
In `\Omega \omega`, Omega is roman and omega is italic.

Auditors: emily
2013-07-18 22:55:43 -07:00
Emily Eisenberg
383ca01434 Sync with Khan-exercises
Summary:
Add some things in to make this more useful to khan-exercises.
Notably, make KaTeX.process create and clean up its own .katex node, and rename
.mathmathmath -> .katex.

Test Plan: Make test, make sure the main page still renders stuff

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3061
2013-07-17 15:43:55 -07:00
Ben Alpert
c08fadfaa9 Parse single superscripts and subscripts
Summary:
Add the ability to parse lone `^x` and `_y`, etc. This basically just
involves more checking of cases in the increasingly ugly `parseAtom` function.
Also, now we manually check for the cases of double superscripts and
subscripts.

Test Plan: Make sure the tests pass. Make sure things like `^x` and `_y` parse.

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3095
2013-07-16 22:00:54 -07:00
Emily Eisenberg
975234e813 Fix some minor things
Summary: Fix llap/rlap again, fix lexing errors

Auditors: alpert
2013-07-15 12:12:15 -07:00
Ben Alpert
09311de814 Add Greek letters
Summary: Fixes T1190.

Test Plan: `\alpha \beta \gamma \delta \epsilon \varepsilon \zeta \eta \theta \vartheta \iota \kappa \lambda \mu \nu \xi \omicron \pi \varpi \rho \varrho \sigma \varsigma \tau \upsilon \phi \varphi \chi \psi \omega \Gamma \Delta \Theta \Lambda \Xi \Pi \Sigma \Upsilon \Phi \Psi \Omega`

Reviewers: emily

Reviewed By: emily

Maniphest Tasks: T1190

Differential Revision: http://phabricator.khanacademy.org/D3054
2013-07-14 22:54:01 -07:00
Ben Alpert
a5840aae4b Add more symbols
Summary:
', \angle, \circ, \gets, \infty, \leftarrow, \prime, \rightarrow, \times, \to

Fixes T1191.

Reviewers: emily

Reviewed By: emily

Maniphest Tasks: T1191

Differential Revision: http://phabricator.khanacademy.org/D3053
2013-07-14 22:53:43 -07:00
Ben Alpert
ed9d62d98c Inherit fraction bar color from surrounding text
Test Plan: Fractions look okay in Chrome, Firefox, IE.

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3051
2013-07-14 22:41:03 -07:00
Ben Alpert
e29fe0c683 Grosser color support for prettier results
Test Plan: `\orange{x}\blue{-4}`, `\orange{x-}\blue{-4}` look the same after desaturation as `x-4` and `x--4`. In addition, `\orange{x}\blue{{-4}}` looks appropriately atrocious (i.e., has no space whatsoever).

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3050
2013-07-14 21:34:05 -07:00
Ben Alpert
e472b0ba9d Add TeX style support
Test Plan:
`\blue\frac12 + \frac{2(y-z)}{2+\frac1{7+\frac31}} \div \orange{\arctan x^{2+\frac43}_{2}} * 2^{2^{2^2}}` looks reasonable, as does
`\blue\frac12 + \dfrac{2(y-z)}{2+\frac1{7+\frac31}} \div \orange{\arctan x^{2+\frac43}_{2}} * 2^{2^{2^2}}`.

Reviewers: emily

Reviewed By: emily

Differential Revision: http://phabricator.khanacademy.org/D3047
2013-07-14 16:55:46 -07:00
Emily Eisenberg
ed82784cba Non-capturing regex, don't catch some exceptions
Auditors: alpert
2013-07-13 20:55:35 -07:00
Emily Eisenberg
a831e94a51 Remove dependency on underscore
Summary:
Remove all uses of underscore. For the things we actually need, make a
"util" file and put them in there.

Test Plan:
Make sure the test still succeed, and that the main page still
works.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3043
2013-07-13 20:42:19 -07:00
Emily Eisenberg
0da85483c4 Remove Parser options usage
Summary: Now, have katex catch and print exceptions instead of the
parser

Auditors: alpert
2013-07-13 18:15:17 -07:00
Emily Eisenberg
4122fa2b99 Rewrite the parser
Summary:
Make our own parser that doesn't use jison, so that we can handle
funny TeX syntax, and to make it smaller.

Test Plan: Make sure the tests pass with the new parser.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D3029
2013-07-12 23:16:30 -07:00
Emily Eisenberg
507a552ffd Add some basic testing using jasmine
Summary:
Make some tests that test the parser. So far, there are no DOM tests,
but maybe later.

Test Plan:
Run `make serve` and then visit `/test/test.html`. Make sure all the
tests pass.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D2987
2013-07-11 18:32:44 -07:00
Emily Eisenberg
adb8b5d671 Rename everything to KaTeX
Summary: Rename all instances of mjlite -> katex

Test Plan: Run the local server, make sure things still work.

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D2977
2013-07-10 14:08:50 -07:00