mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 19:28:39 +00:00
Improve examples, add defer, async, and pre- loading KaTeX (#1580)
* Wrap `katex.render` examples with try...catch * Add starter template with defer in README.md * Update documentation to use `defer` and `onload` * Add documentation on async and pre loading KaTeX * Use sha384 for SRI * Revise wording according to comments * Minor changes * Fix article
This commit is contained in:
29
docs/api.md
29
docs/api.md
@@ -6,23 +6,38 @@ title: API
|
||||
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);
|
||||
katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
|
||||
throwOnError: false
|
||||
});
|
||||
```
|
||||
|
||||
To avoid escaping the backslash (double backslash), you can use
|
||||
[`String.raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
|
||||
(but beware that `${`, `\u` and `\x` may still need escaping):
|
||||
```js
|
||||
katex.render(String.raw`c = \pm\sqrt{a^2 + b^2}`, element);
|
||||
katex.render(String.raw`c = \pm\sqrt{a^2 + b^2}`, element, {
|
||||
throwOnError: false
|
||||
});
|
||||
```
|
||||
|
||||
If KaTeX can't parse the expression, it throws a `katex.ParseError` by default.
|
||||
See [handling errors](error.md) for configuring how to handle errors.
|
||||
|
||||
## Server side rendering or rendering to a string
|
||||
## Server-side rendering or rendering to a string
|
||||
To generate HTML on the server or to generate an HTML string of the rendered math, you can use `katex.renderToString`:
|
||||
|
||||
```js
|
||||
var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}");
|
||||
var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
|
||||
throwOnError: false
|
||||
});
|
||||
// '<span class="katex">...</span>'
|
||||
```
|
||||
|
||||
## Handling errors
|
||||
|
||||
The examples above use the `throwOnError: false` option, which renders invalid
|
||||
inputs as the TeX source code in red (by default), with the error message as
|
||||
hover text. Without this option, invalid LaTeX will cause a
|
||||
`katex.ParseError` exception to be thrown. See [handling errors](error.md).
|
||||
|
||||
## Configuring KaTeX
|
||||
|
||||
The last argument to `katex.render` and `katex.renderToString` can contain
|
||||
[a variety of rendering options](options.md).
|
||||
|
Reference in New Issue
Block a user