Notes/README.md

188 lines
5.7 KiB
Markdown
Raw Normal View History

2021-03-04 10:11:23 -06: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/ckeditor5-math.svg?style=flat)](https://www.npmjs.com/package/ckeditor5-math)
2019-09-17 20:42:36 +03:00
2021-03-04 10:13:07 -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.
2019-09-17 21:06:18 +03:00
2019-10-03 18:36:34 +03:00
## Table of contents
- [Features](#features)
2019-10-06 13:33:03 +03:00
- [Demos](#demos)
2019-10-03 18:36:34 +03:00
- [Screenshots](#screenshots)
2020-02-27 18:01:21 +02:00
- [Requirements](#requirements)
2019-10-03 18:36:34 +03:00
- [Examples](#examples)
- [Installation](#installation)
2019-10-03 21:21:00 +03:00
* [Styles for Lark theme](#styles-for-lark-theme)
2019-10-03 18:39:46 +03:00
- [Configuration & Usage](#configuration--usage)
2019-10-03 21:21:00 +03:00
* [Plugin options](#plugin-options)
* [Available typesetting engines](#available-typesetting-engines)
* [Supported input and output formats](#supported-input-and-output-formats)
2019-10-03 18:36:34 +03:00
- [Paste support](#paste-support)
2019-10-03 21:21:00 +03:00
* [From plain text](#from-plain-text)
2020-11-07 22:19:00 +02:00
- [Autoformat support](#autoformat-support)
2019-10-03 18:36:34 +03:00
- [Preview workaround](#preview-workaround)
## Features
- TeX syntax
- Inline and display equations
- Preview view
- Multiple typesetting engines
- Have multiple input and output format
- Paste support
2019-10-03 21:21:00 +03:00
- from plain text
2020-11-07 22:19:00 +02:00
- Autoformat support
2019-10-03 18:36:34 +03:00
2019-10-06 13:31:08 +03:00
# Demos
2020-10-18 19:22:32 +03:00
- [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
![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
2020-11-07 20:02:46 +02:00
- 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
[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:
- [CKEditor 5 classic editor build](https://github.com/ckeditor/ckeditor5-build-classic)
- [CKEditor 5 inline editor build](https://github.com/ckeditor/ckeditor5-build-inline)
2019-09-17 21:06:18 +03:00
Install plugin with NPM or Yarn
2019-09-17 21:09:08 +03:00
2019-09-17 21:06:18 +03:00
`npm install ckeditor5-math --save-dev`
Add import into ckeditor.js file
```js
import Mathematics from 'ckeditor5-math/src/math';
```
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 = [
// ...
Mathematics
2019-09-17 21:06:18 +03:00
];
```
2019-10-03 18:51:24 +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
2020-11-03 21:48:08 +02:00
__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
## Configuration & Usage
2019-09-17 20:42:36 +03:00
2019-10-03 03:44:28 +03:00
### Plugin options
```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
2019-10-03 03:44:28 +03:00
forceOutputType: false, // forces output to use outputType
enablePreview: true // Enable preview view
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__
2019-10-03 18:51:24 +03:00
- 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/)
__Custom typesetting__
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-10-01 18:18:13 +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.
2019-10-03 18:51:24 +03:00
- __preview__ is boolean. Rendering in preview when true.
2019-09-17 20:42:36 +03:00
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
Ctrl+M can be used to add easily math formulas in inline mode.
#### Display mode
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
// ...
import AutoformatMathematics from 'ckeditor5-math/src/autoformatmath';
InlineEditor.builtinPlugins = [
// ...
AutoformatMathematics
];
```
2019-10-03 18:36:34 +03:00
## Preview workaround
__.ck-reset_all *__ css rules from ckeditor5-ui and ckeditor5-theme-lark break rendering in preview mode.
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.