6
6
First, set up the environment for React. In this example, we will use Create React App:
7
7
8
8
``` bash
9
- npx create-react-app markdown-editor --template typescript
10
- cd markdown-editor
9
+ npx create-react-app markdown-editor --template gravity-ui-pure && cd markdown-editor
10
+ ```
11
+ Ensure that the ` typescript ` version in ` devDependencies ` matches the version specified in ` overrides ` in ` package.json ` . If there is a mismatch, update it by running:
12
+
13
+ ``` bash
14
+ npm install typescript@< version_from_overrides> --save-dev
11
15
```
12
16
13
17
### 2. Installing the Markdown editor
@@ -17,32 +21,95 @@ Install the Markdown editor
17
21
npm install @gravity-ui/markdown-editor
18
22
```
19
23
20
- ### 3. Installing dependencies
21
- Ensure that you have the necessary dependencies listed in peerDependencies. Include the following packages:
24
+ ### 3. Install peer dependencies
25
+ Ensure that you have the necessary dependencies listed in [ peerDependencies] ( https://github.com/gravity-ui/markdown-editor/blob/main/package.json ) and install it . Include the following packages:
22
26
- ` @diplodoc/transform `
23
- - ` react `
24
- - ` react-dom `
25
- - ` @gravity-ui/uikit `
27
+ - ` highlight.js `
28
+ - ` katex `
29
+ - ` lowlight `
30
+ - ` markdown-it `
26
31
27
- Check the peerDependencies section in the ` package.json ` file to ensure all necessary dependencies are installed correctly.
32
+ ### 4. Configuring the application
33
+ Add the ` Editor.tsx ` :
28
34
29
- To install the dependencies, use:
35
+ ``` tsx
36
+ import React from ' react' ;
37
+ import {MarkdownEditorView , MarkupString , useMarkdownEditor } from ' @gravity-ui/markdown-editor' ;
30
38
31
- ``` bash
32
- npm install @diplodoc/transform react react-dom @gravity-ui/uikit
39
+ export interface EditorProps {
40
+ onSubmit: (value : MarkupString ) => void ;
41
+ }
42
+
43
+ export const Editor: React .FC <EditorProps > = ({onSubmit }) => {
44
+ const editor = useMarkdownEditor ({
45
+ md: {
46
+ html: false ,
47
+ },
48
+ });
49
+
50
+ React .useEffect (() => {
51
+ function submitHandler() {
52
+ // Serialize current content to markdown markup
53
+ const value = editor .getValue ();
54
+ onSubmit (value );
55
+ }
56
+
57
+ editor .on (' submit' , submitHandler );
58
+ return () => {
59
+ editor .off (' submit' , submitHandler );
60
+ };
61
+ }, [onSubmit ]);
62
+
63
+ return <MarkdownEditorView stickyToolbar autofocus editor = { editor } />;
64
+ };
33
65
```
34
66
35
- ### 4. Configuring Webpack
36
- In order for the ` diplodoc/transform ` process to function correctly, please add the [ webpack resolve-fallbacks] ( https://webpack.js.org/configuration/resolve/#resolvefallback ) .
67
+ Update the ` App.tsx ` with ` Editor ` component:
68
+
69
+ ``` tsx
70
+ import {ThemeProvider , Toaster , ToasterComponent , ToasterProvider } from ' @gravity-ui/uikit' ;
71
+ import {MarkupString } from ' @gravity-ui/markdown-editor' ;
72
+
73
+ import {Editor } from ' ./Editor' ;
74
+
75
+ const toaster = new Toaster ();
37
76
38
- To accomplish this, please install CRACO and configure it follow the instructions below:
77
+ const App = () => {
78
+ const handleSubmit = (value : MarkupString ) => {
79
+ console .log (value );
80
+ };
81
+
82
+ return (
83
+ <ThemeProvider theme = " light" >
84
+ <ToasterProvider toaster = { toaster } >
85
+ <ToasterComponent />
86
+ <Editor onSubmit = { handleSubmit } />
87
+ </ToasterProvider >
88
+ </ThemeProvider >
89
+ );
90
+ };
91
+
92
+ export default App ;
93
+ ````
94
+
95
+ ### 5. Configuring Webpack
96
+ To prevent errors related to missing polyfills for Node .js core modules in Webpack 5 , such as :
97
+
98
+ - ` Can't resolve 'process' `
99
+ - ` Can't resolve 'fs' `
100
+ - ` Can't resolve 'path' `
101
+ - ` Can't resolve 'url' `
102
+
103
+ These errors occur because Webpack 5 no longer includes polyfills for Node .js modules by default . To fix this , you need to configure [fallback modules ](https :// webpack.js.org/configuration/resolve/#resolvefallback).
104
+
105
+ We recommend using CRACO to apply these configurations .
39
106
40
107
1. Install CRACO :
41
108
42
109
` ` ` bash
43
110
npm install @craco/craco
44
111
` ` `
45
- 2 . Create a file called craco.config.js in the root of the project and add the following configuration:
112
+ 2. Create a file called ` craco.config.js ` in the root of the project and add the following configuration :
46
113
47
114
` ` ` javascript
48
115
module.exports = {
@@ -52,13 +119,14 @@ module.exports = {
52
119
fs: false,
53
120
process: false,
54
121
path: false,
122
+ url: false,
55
123
};
56
124
return webpackConfig;
57
125
},
58
126
},
59
127
};
60
128
` ` `
61
- 3 . Update package.json to use craco for scripts:
129
+ 3. Update ` package.json ` to use CRACO for scripts :
62
130
63
131
` ` ` json
64
132
{
@@ -70,51 +138,13 @@ module.exports = {
70
138
}
71
139
}
72
140
` ` `
73
- ### 5. Configuring the application
74
- Add ThemeProvider to App:
141
+ This setup ensures that your project is compatible with Webpack 5 and prevents missing module errors .
75
142
76
- ``` tsx
77
- import { ThemeProvider , ToasterProvider } from ' @gravity-ui/uikit' ;
78
- import { toaster } from ' @gravity-ui/uikit/toaster-singleton' ;
143
+ 6. After these changes , start the development server :
79
144
80
- // ...
81
- function App() {
82
- return (
83
- <ThemeProvider theme = " light" >
84
- <ToasterProvider toaster = { toaster } >
85
- <Editor onSubmit = { (value ) => console .log (value )} />
86
- </ToasterProvider >
87
- </ThemeProvider >
88
- );
89
- }
145
+ ` ` ` bash
146
+ npm start
90
147
` ` `
91
- Add the Editor component to App:
92
148
93
- ``` tsx
94
- import React from ' react' ;
95
- import { useMarkdownEditor , MarkdownEditorView } from ' @gravity-ui/markdown-editor' ;
96
149
97
- function Editor({ onSubmit }) {
98
- const editor = useMarkdownEditor ({ allowHTML: false });
99
150
100
- React .useEffect (() => {
101
- function submitHandler() {
102
- // Serialize current content to markdown markup
103
- const value = editor .getValue ();
104
- onSubmit (value );
105
- }
106
-
107
- editor .on (' submit' , submitHandler );
108
- return () => {
109
- editor .off (' submit' , submitHandler );
110
- };
111
- }, [onSubmit ]);
112
-
113
- return <MarkdownEditorView stickyToolbar autofocus editor = { editor } />;
114
- }
115
- ```
116
- Add styles:
117
-
118
- ``` ts
119
- import ' @gravity-ui/uikit/styles/styles.css' ;
120
- ```
0 commit comments