Skip to content

Commit 80ad40f

Browse files
authored
feat!: use diplodoc/latex-extension instead of markdown-it-katex (#184)
- added packages to peerDependencies: `@diplodoc/latex-extension`, `katex`, `markdown-it` - editor's Math extension now use `@diplodoc/latex-extension` instead of `markdown-it-katex` - Math extension removed from YfmPreset/YfmSpecsPreset and package root export - added options to Math extension Example of using a Math extension: ```js import {Math} from '@doc-tools/yfm-editor/_/extensions/yfm/Math'; // ... builder.use(Math, { // required loadRuntimeScript: async () => { await Promise.all([ import('@diplodoc/latex-extension/runtime'), import('@diplodoc/latex-extension/runtime/styles'), ]); }, // optional; if you need custom sanitizing sanitize: (html) => /* sanitize html */ html, // optional; options to be passed to katex katexOptions: {}, }); ```
1 parent 7365cf7 commit 80ad40f

File tree

15 files changed

+246
-116
lines changed

15 files changed

+246
-116
lines changed

demo/HtmlPreview.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22

3+
import {useLatex} from '@diplodoc/latex-extension/react';
34
import transform from '@diplodoc/transform';
45

56
import {MarkupString, colorClassName} from '../src';
67
import type {ClassNameProps} from '../src/classname';
78

8-
import {plugins} from './md-plugins';
9+
import {LATEX_RUNTIME, plugins} from './md-plugins';
910

1011
type PlaygroundHtmlPreviewProps = ClassNameProps & {
1112
value: MarkupString;
@@ -15,20 +16,42 @@ type PlaygroundHtmlPreviewProps = ClassNameProps & {
1516
linkifyTlds?: string | string[];
1617
};
1718

19+
type Meta = {script?: string[]; style?: string[]};
20+
1821
export const PlaygroundHtmlPreview: React.FC<PlaygroundHtmlPreviewProps> =
1922
function PlaygroundHtmlPreview({value, allowHTML, breaks, linkify, linkifyTlds, className}) {
2023
const divRef = React.useRef<HTMLDivElement>(null);
24+
const renderLatex = useLatex();
2125

22-
const html = React.useMemo(() => {
26+
const result = React.useMemo(() => {
2327
return transform(value, {
2428
allowHTML,
2529
breaks,
2630
plugins,
2731
linkify,
2832
linkifyTlds,
2933
defaultClassName: colorClassName, // markdown-it-color
30-
}).result.html;
34+
}).result;
3135
}, [allowHTML, breaks, linkify, linkifyTlds, value]);
3236

33-
return <div ref={divRef} className={className} dangerouslySetInnerHTML={{__html: html}} />;
37+
// Load katex only if one or more formulas should be rendered
38+
if (((result.meta ?? {}) as Meta).script?.includes(LATEX_RUNTIME)) {
39+
import('@diplodoc/latex-extension/runtime');
40+
}
41+
if (((result.meta ?? {}) as Meta).style?.includes(LATEX_RUNTIME)) {
42+
// @ts-expect-error
43+
import('@diplodoc/latex-extension/runtime/styles');
44+
}
45+
46+
React.useEffect(() => {
47+
renderLatex({throwOnError: false});
48+
}, [result.html, renderLatex]);
49+
50+
return (
51+
<div
52+
ref={divRef}
53+
className={className}
54+
dangerouslySetInnerHTML={{__html: result.html}}
55+
/>
56+
);
3457
};

demo/Playground.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
logger,
2121
useYfmEditor,
2222
} from '../src';
23+
import {Math} from '../src/extensions/yfm/Math';
2324
import {wHiddenData, wToolbarConfig} from '../src/toolbar/config/wysiwyg';
2425

2526
import {PlaygroundHtmlPreview} from './HtmlPreview';
@@ -92,7 +93,15 @@ const Playground = React.memo<PlaygroundProps>((props) => {
9293
underline: {underlineKey: keys.underline},
9394
code: {codeKey: keys.code},
9495
})
95-
.use(YfmPreset, {}),
96+
.use(YfmPreset, {})
97+
.use(Math, {
98+
loadRuntimeScript: async () => {
99+
await Promise.all([
100+
import('@diplodoc/latex-extension/runtime'), // @ts-expect-error
101+
import('@diplodoc/latex-extension/runtime/styles'),
102+
]);
103+
},
104+
}),
96105
[breaks, renderStorage],
97106
);
98107

demo/md-content.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ export const initialMdContent = `
1717
1818
А это блочная формула:
1919
20-
$$\\begin{array}{c}
20+
$$
2121
22-
\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} &
23-
= \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\
22+
f(\\relax{x}) = \\int_{-\\infty}^\\infty
23+
\\hat f(\\xi)\\,e^{2 \\pi i \\xi x}
24+
\\,d\\xi
2425
25-
\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\
26-
27-
\\nabla \\cdot \\vec{\\mathbf{B}} & = 0
28-
29-
\\end{array}$$
26+
$$
3027
3128
_Кликни в формулу, чтобы отредактировать её_
3229

demo/md-plugins.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {transform as latex} from '@diplodoc/latex-extension';
12
import anchors from '@diplodoc/transform/lib/plugins/anchors';
23
import checkbox from '@diplodoc/transform/lib/plugins/checkbox';
34
import code from '@diplodoc/transform/lib/plugins/code';
@@ -15,10 +16,11 @@ import video from '@diplodoc/transform/lib/plugins/video';
1516
import type {PluginWithParams} from 'markdown-it/lib';
1617
import color from 'markdown-it-color';
1718
import ins from 'markdown-it-ins';
18-
import math from 'markdown-it-katex';
1919
import mark from 'markdown-it-mark';
2020
import sub from 'markdown-it-sub';
2121

22+
export const LATEX_RUNTIME = 'extension:latex';
23+
2224
const defaultPlugins: PluginWithParams[] = [
2325
meta,
2426
deflist,
@@ -35,6 +37,12 @@ const defaultPlugins: PluginWithParams[] = [
3537
imsize,
3638
checkbox,
3739
];
38-
const extendedPlugins = defaultPlugins.concat(sub, ins, mark, math, color);
40+
const extendedPlugins = defaultPlugins.concat(
41+
sub,
42+
ins,
43+
mark,
44+
latex({bundle: false, validate: false, runtime: LATEX_RUNTIME}),
45+
color,
46+
);
3947

4048
export {extendedPlugins as plugins};

package-lock.json

Lines changed: 44 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"require": "./build/cjs/markdown-it/*",
5151
"import": "./build/esm/markdown-it/*"
5252
},
53+
"./_/*": {
54+
"types": "./build/esm/*",
55+
"require": "./build/cjs/*",
56+
"import": "./build/esm/*"
57+
},
5358
"./styles/*": "./build/esm/styles/*"
5459
},
5560
"main": "build/cjs/index.js",
@@ -71,6 +76,9 @@
7176
],
7277
"markdown-it/*": [
7378
"./build/esm/markdown-it/*"
79+
],
80+
"_/*": [
81+
"./build/esm/*"
7482
]
7583
}
7684
},
@@ -93,7 +101,6 @@
93101
"is-number": "^7.0.0",
94102
"markdown-it-color": "^2.1.1",
95103
"markdown-it-ins": "^3.0.1",
96-
"markdown-it-katex": "2.0.3",
97104
"markdown-it-mark": "^3.0.1",
98105
"markdown-it-sub": "^1.0.0",
99106
"prosemirror-codemark": "0.4.2",
@@ -113,6 +120,7 @@
113120
"tslib": "^2.3.1"
114121
},
115122
"devDependencies": {
123+
"@diplodoc/latex-extension": "1.0.3",
116124
"@diplodoc/transform": "4.5.0",
117125
"@gravity-ui/components": "2.0.0",
118126
"@gravity-ui/eslint-config": "3.1.1",
@@ -127,7 +135,7 @@
127135
"@types/gulp": "4.0.9",
128136
"@types/gulp-sass": "5.0.0",
129137
"@types/jest": "^27.0.3",
130-
"@types/katex": "0.5.0",
138+
"@types/katex": "0.16.7",
131139
"@types/lodash": "^4.14.177",
132140
"@types/react": "18.0.28",
133141
"@types/react-dom": "18.0.11",
@@ -160,9 +168,12 @@
160168
},
161169
"peerDependencies": {
162170
"@diplodoc/transform": "^4.5.0",
171+
"@diplodoc/latex-extension": "^1.0.3",
163172
"@gravity-ui/components": "^2.0.0",
164173
"@gravity-ui/uikit": "^5.0.0",
174+
"katex": "^0.16.9",
165175
"lodash": "^4.17.20",
176+
"markdown-it": "^13.0.0",
166177
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
167178
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
168179
},

src/extensions/yfm/Math/MathSpecs/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mathMdPlugin from 'markdown-it-katex';
1+
import {transform} from '@diplodoc/latex-extension';
22

33
import type {ExtensionAuto} from '../../../../core';
44
import {nodeTypeFactory} from '../../../../utils/schema';
@@ -10,7 +10,7 @@ export const mathIType = nodeTypeFactory(MathNode.Inline);
1010
export const mathBType = nodeTypeFactory(MathNode.Block);
1111

1212
export const MathSpecs: ExtensionAuto = (builder) => {
13-
builder.configureMd((md) => md.use(mathMdPlugin));
13+
builder.configureMd((md) => md.use(transform({bundle: false, validate: false}), {output: ''}));
1414
builder
1515
.addNode(MathNode.Inline, () => ({
1616
spec: {

src/extensions/yfm/Math/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
removeEmptyMathInlineIfCursorIsAtBeginning,
1515
} from './commands';
1616
import {mathBType, mathIType} from './const';
17-
import {mathViewAndEditPlugin} from './view-and-edit';
17+
import {MathNodeViewOptions, mathViewAndEditPlugin} from './view-and-edit';
1818

1919
import './index.scss';
2020

@@ -26,7 +26,14 @@ const mathBAction = 'toMathBlock';
2626

2727
const mathITemplate = 'f(x)=';
2828

29-
export const Math: ExtensionAuto = (builder) => {
29+
// !!! YfmPreset/YfmSpecsPreset does not use or re-export the Math extension
30+
31+
export type MathOptions = Pick<
32+
MathNodeViewOptions,
33+
'loadRuntimeScript' | 'sanitize' | 'katexOptions'
34+
>;
35+
36+
export const Math: ExtensionAuto<MathOptions> = (builder, opts) => {
3037
builder.use(MathSpecs);
3138

3239
builder.addKeymap(() => ({
@@ -39,7 +46,10 @@ export const Math: ExtensionAuto = (builder) => {
3946

4047
builder
4148
.addPlugin(() =>
42-
mathViewAndEditPlugin({reactRenderer: builder.context.get('reactrenderer')!}),
49+
mathViewAndEditPlugin({
50+
...opts,
51+
reactRenderer: builder.context.get('reactrenderer')!,
52+
}),
4353
)
4454
.addInputRules((deps) => ({
4555
rules: [

0 commit comments

Comments
 (0)