66First, set up the environment for React. In this example, we will use Create React App:
77
88``` 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
1115```
1216
1317### 2. Installing the Markdown editor
@@ -17,32 +21,95 @@ Install the Markdown editor
1721npm install @gravity-ui/markdown-editor
1822```
1923
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:
2226- ` @diplodoc/transform `
23- - ` react `
24- - ` react-dom `
25- - ` @gravity-ui/uikit `
27+ - ` highlight.js `
28+ - ` katex `
29+ - ` lowlight `
30+ - ` markdown-it `
2631
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 ` :
2834
29- To install the dependencies, use:
35+ ``` tsx
36+ import React from ' react' ;
37+ import {MarkdownEditorView , MarkupString , useMarkdownEditor } from ' @gravity-ui/markdown-editor' ;
3038
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+ };
3365```
3466
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 ();
3776
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 .
39106
401071. Install CRACO :
41108
42109` ` ` bash
43110npm install @craco/craco
44111` ` `
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 :
46113
47114` ` ` javascript
48115module.exports = {
@@ -52,13 +119,14 @@ module.exports = {
52119 fs: false,
53120 process: false,
54121 path: false,
122+ url: false,
55123 };
56124 return webpackConfig;
57125 },
58126 },
59127};
60128` ` `
61- 3 . Update package.json to use craco for scripts:
129+ 3. Update ` package.json ` to use CRACO for scripts :
62130
63131` ` ` json
64132{
@@ -70,51 +138,13 @@ module.exports = {
70138 }
71139}
72140` ` `
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 .
75142
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 :
79144
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
90147` ` `
91- Add the Editor component to App:
92148
93- ``` tsx
94- import React from ' react' ;
95- import { useMarkdownEditor , MarkdownEditorView } from ' @gravity-ui/markdown-editor' ;
96149
97- function Editor({ onSubmit }) {
98- const editor = useMarkdownEditor ({ allowHTML: false });
99150
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