Notes/README.md

324 lines
9.1 KiB
Markdown
Raw Normal View History

2023-04-03 07:01:23 -05:00
# CKEditor 5 mathematical feature · [![GitHub license](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/isaul32/ckeditor5-math/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/@isaul32/ckeditor5-math.svg?style=flat)](https://www.npmjs.com/package/@isaul32/ckeditor5-math)
2019-09-17 20:42:36 +03:00
2021-03-04 10:15:40 -06:00
ckeditor5-math is a TeX-based mathematical plugin for CKEditor 5. You can use it to insert, edit, and view mathematical equations and formulas. This plugin supports [MathJax], [KaTeX] and custom typesetting engines.
[mathjax]: https://www.mathjax.org/
[katex]: https://katex.org/
2019-09-17 21:06:18 +03:00
2019-10-03 18:36:34 +03:00
## Table of contents
- [Features](#features)
- [Demos](#demos)
- [Screenshots](#screenshots)
- [Requirements](#requirements)
- [Examples](#examples)
- [Installation](#installation)
- [Styles for Lark theme](#styles-for-lark-theme)
- [Configuration & Usage](#configuration--usage)
- [Plugin options](#plugin-options)
- [Available typesetting engines](#available-typesetting-engines)
- [Supported input and output formats](#supported-input-and-output-formats)
- [Paste support](#paste-support)
- [From plain text](#from-plain-text)
- [Autoformat support](#autoformat-support)
- [Preview workaround](#preview-workaround)
2019-10-03 18:36:34 +03:00
## Features
- TeX syntax
- Inline and display equations
- Preview view
- Multiple typesetting engines
- Have multiple input and output format
- Paste support
- from plain text
- Autoformat support
2019-10-03 18:36:34 +03:00
2019-10-06 13:31:08 +03:00
# Demos
- [Classic editor with MathJax](https://jsfiddle.net/isaul32/qktj9h7x/)
- [Classic editor with KaTex](https://jsfiddle.net/isaul32/3wjrkLdv/)
- [Balloon block editor with KaTex](https://jsfiddle.net/isaul32/q01mu8kp/)
2019-10-06 13:31:08 +03:00
2019-10-06 13:33:03 +03:00
## Screenshots
2019-10-06 13:33:03 +03:00
![Screenshot 1](/screenshots/1.png?raw=true "Screenshot 1")
![Screenshot 2](/screenshots/2.png?raw=true "Screenshot 2")
2020-02-27 18:01:21 +02:00
## Requirements
- Use same major version as your CKEditor 5 build
2020-02-27 18:01:21 +02:00
2020-11-07 20:02:46 +02:00
If you get duplicated modules error, you have mismatching versions.
2020-02-27 20:33:39 +02:00
2019-10-01 18:18:13 +03:00
## Examples
2019-10-01 18:18:13 +03:00
[Link to examples repository](https://github.com/isaul32/ckeditor5-math-examples)
2019-09-17 21:06:18 +03:00
## Installation
2019-10-03 18:36:34 +03:00
Use official classic or inline build as a base:
2023-02-06 19:35:50 +01:00
- [CKEditor 5 classic editor build](https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-build-classic)
- [CKEditor 5 inline editor build](https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-build-inline)
2019-10-03 18:36:34 +03:00
2019-09-17 21:06:18 +03:00
Install plugin with NPM or Yarn
2019-09-17 21:09:08 +03:00
2023-04-03 07:01:23 -05:00
`npm install @isaul32/ckeditor5-math --save-dev`
2019-09-17 21:06:18 +03:00
2023-04-03 07:01:23 -05:00
`yarn add @isaul32/ckeditor5-math --dev`
2021-05-05 10:28:31 -05:00
2019-09-17 21:06:18 +03:00
Add import into ckeditor.js file
```js
2023-04-03 07:01:23 -05:00
import Math from '@isaul32/ckeditor5-math/src/math';
import AutoformatMath from '@isaul32/ckeditor5-math/src/autoformatmath';
2019-09-17 21:06:18 +03:00
```
2019-10-03 18:51:24 +03:00
Add it to built-in plugins
2019-09-17 21:06:18 +03:00
```js
InlineEditor.builtinPlugins = [
// ...
2023-02-06 19:35:50 +01:00
Math,
AutoformatMath
2019-09-17 21:06:18 +03:00
];
```
**Add math button to toolbar**
2019-09-17 21:06:18 +03:00
```js
InlineEditor.defaultConfig = {
toolbar: {
items: [
// ...
'math'
2019-09-17 21:06:18 +03:00
]
}
2019-09-17 21:06:18 +03:00
};
```
2019-10-03 18:36:34 +03:00
### Styles for Lark theme
**Copy theme/ckeditor5-math folder** from [https://github.com/isaul32/ckeditor5/tree/master/packages/ckeditor5-theme-lark](https://github.com/isaul32/ckeditor5/tree/master/packages/ckeditor5-theme-lark) to your lark theme repository
2019-09-17 21:12:20 +03:00
2023-02-06 19:35:50 +01:00
### Using DLL builds
Use the [official DLL build](https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/alternative-setups/dll-builds.html) and additionally load the math plugin:
```html
2023-04-03 07:01:23 -05:00
<script src="path/to/node_modules/@isaul32/ckeditor5-math/build/math.js"></script>
2023-02-06 19:35:50 +01:00
<script>
CKEditor5.editorClassic.ClassicEditor
.create(editorElement, {
plugins: [
CKEditor5.math.Math,
...
],
...
});
</script>
```
## Configuration & Usage
2019-09-17 20:42:36 +03:00
2019-10-03 03:44:28 +03:00
### Plugin options
2019-10-03 03:44:28 +03:00
```js
InlineEditor.defaultConfig = {
2019-10-03 21:21:00 +03:00
// ...
2019-10-03 21:21:33 +03:00
math: {
2019-10-11 19:26:39 +03:00
engine: 'mathjax', // or katex or function. E.g. (equation, element, display) => { ... }
2021-03-02 07:20:44 -06:00
lazyLoad: undefined, // async () => { ... }, called once before rendering first equation if engine doesn't exist. After resolving promise, plugin renders equations.
2019-10-11 19:26:39 +03:00
outputType: 'script', // or span
className: 'math-tex', // class name to use with span output type, change e.g. MathJax processClass (v2) / processHtmlClass (v3) is set
2019-10-03 03:44:28 +03:00
forceOutputType: false, // forces output to use outputType
enablePreview: true, // Enable preview view
previewClassName: [], // Class names to add to previews
popupClassName: [], // Class names to add to math popup balloon
katexRenderOptions: {} // KaTeX only options for katex.render(ToString)
2019-10-03 21:21:00 +03:00
}
2019-10-03 03:44:28 +03:00
}
```
### Available typesetting engines
**MathJax**
- Tested with **latest 2.7**
- Has experimental (**CHTML**, **SVG**) support for **3.0.0** or newer version
2019-10-03 18:36:34 +03:00
[<img src="https://www.mathjax.org/badge/badge-square.svg" width="130" alt="KaTeX">](https://www.mathjax.org/)
**KaTeX**
- Tested with version **0.12.0**
2019-10-03 18:36:34 +03:00
[<img src="https://katex.org/img/katex-logo-black.svg" width="130" alt="KaTeX">](https://katex.org/)
- `katexRenderOptions` - pass [options](https://katex.org/docs/options.html).
```js
InlineEditor.defaultConfig = {
// ...
math: {
engine: 'katex'
katexRenderOptions: {
macros: {
"\\neq": "\\mathrel{\\char`≠}",
},
},
}
}
```
**Custom typesetting**
2019-10-03 18:36:34 +03:00
2019-10-15 21:32:25 +03:00
Custom typesetting is possible to implement with engine config. For example, custom typesetting feature can be used when use back end rendering.
```js
InlineEditor.defaultConfig = {
// ...
math: {
2019-10-11 19:26:39 +03:00
engine: ( equation, element, display, preview ) => {
// ...
}
}
}
```
2019-09-17 20:42:36 +03:00
- **equation** is equation in TeX format without delimiters.
- **element** is DOM element reserved for rendering.
- **display** is boolean. Typesetting should be inline when false.
- **preview** is boolean. Rendering in preview when true.
2019-08-31 20:48:37 +03:00
### Supported input and output formats
Supported input and output formats are:
```html
<!-- MathJax style http://docs.mathjax.org/en/v2.7-latest/advanced/model.html#how-mathematics-is-stored-in-the-page -->
<script type="math/tex">\sqrt{\frac{a}{b}}</script>
<script type="math/tex; mode=display">\sqrt{\frac{a}{b}}</script>
<!-- CKEditor 4 style https://ckeditor.com/docs/ckeditor4/latest/features/mathjax.html -->
<span class="math-tex">\( \sqrt{\frac{a}{b}} \)</span>
<span class="math-tex">\[ \sqrt{\frac{a}{b}} \]</span>
```
2019-10-01 18:18:13 +03:00
### Paste support
2019-10-03 18:36:34 +03:00
#### From plain text
Paste TeX equations with **delimiters**. For example:
2019-10-03 18:51:24 +03:00
```latex
\[ x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} \]
```
2019-10-03 18:51:24 +03:00
2019-10-01 18:18:13 +03:00
or
2019-10-03 18:51:24 +03:00
```latex
\( x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} \)
```
2019-10-01 18:18:13 +03:00
2020-11-07 22:19:00 +02:00
### Autoformat support
#### Inline mode
2020-11-07 22:19:00 +02:00
Ctrl+M can be used to add easily math formulas in inline mode.
#### Display mode
2020-11-07 22:19:00 +02:00
Autoformat for math can be used to add formula in display mode on a new line by adding `\[` or `$$`. This feature requires additional autoformat plugin to be added.
Add following lines into your build
```js
// ...
2023-04-03 07:01:23 -05:00
import AutoformatMath from '@isaul32/ckeditor5-math/src/autoformatmath';
2020-11-07 22:19:00 +02:00
InlineEditor.builtinPlugins = [
// ...
2023-02-06 19:35:50 +01:00
AutoformatMath
2020-11-07 22:19:00 +02:00
];
```
2023-02-06 19:35:50 +01:00
or use it with DLL build
```html
2023-04-03 07:01:23 -05:00
<script src="path/to/node_modules/@isaul32/ckeditor5-math/build/math.js"></script>
2023-02-06 19:35:50 +01:00
<script>
CKEditor5.editorInline.InlineEditorEditor
.create(editorElement, {
plugins: [
CKEditor5.math.AutoformatMath,
...
],
...
});
</script>
```
2019-10-03 18:36:34 +03:00
## Preview workaround
2021-03-04 10:18:31 -06:00
`.ck-reset_all *` css rules from ckeditor5-ui and ckeditor5-theme-lark break rendering in preview mode.
2019-10-03 18:36:34 +03:00
2020-10-02 21:06:25 +03:00
My solution for this is use rendering element outside of CKEditor DOM and place it to right place by using absolute position. Alternative solution could be using iframe, but then typesetting engine's scripts and styles have to copy to child document.
## TypeScript users
ckeditor5-math does not have typings yet. TypeScript builds of CKEditor will see
this:
```sh
src/ckeditor.ts:63:18 - error TS7016: Could not find a declaration file for module '@isaul32/ckeditor5-math/src/math'.
'/path/to/your/project/node_modules/@isaul32/ckeditor5-math/src/math.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/isaul32__ckeditor5-math` if it exists or add a new declaration (.d.ts) file containing
`declare module '@isaul32/ckeditor5-math/src/math';`
```
### Workaround for typings
1. Create a `d.ts` declaration file, e.g. `typings/ckeditor5-math.d.ts`
```typescript
declare module '@isaul32/ckeditor5-math';
declare module '@isaul32/ckeditor5-math/src/math';
declare module '@isaul32/ckeditor5-math/src/autoformatmath';
```
2. In your [`tsconfig.json`](https://www.typescriptlang.org/tsconfig)'s
root-level [`include`](https://www.typescriptlang.org/tsconfig#include)
option, make sure your declaration file is covered, e.g.
```json
{
"extends": "ckeditor5/tsconfig.json",
"include": [
"src",
"typings",
"../../typings"
]
}
```
## Development
Contributions, improvements and bug fixes are welcome. To aid in this, try out
our developer environment w/ live reload support and [CKEditor 5 inspector].
![Development environment](/screenshots/development-environment.png?raw=true "Screenshot of
development environment")
To enter a development loop with hot reload support:
- `git clone https://github.com/isaul32/ckeditor5-math.git`
- `cd ckeditor5-math`
- `yarn`
- `yarn start`
- http://localhost:8080
[ckeditor 5 inspector]: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/development-tools.html#ckeditor-5-inspector