1/1/1970
This project is a simple Markdown editor built with React. You can write Markdown syntax on the left side, and it will automatically render into an HTML preview on the right side.
Provide clear instructions on how to clone the repository and install dependencies.
# Clone the repository
git clone https://github.com/your-username/markdown-editor.git
# Navigate to the project directory
cd markdown-editor
# Install dependencies
npm install
# Start the development server
npm startHighlight the key features of your Markdown editor to showcase its functionality.
Provide an example of how to use the editor, including some common Markdown syntax.
# This is a Heading
You can write **bold** text, _italicized_ text, or `inline code`.
```javascript
// This is a code block in JavaScript
function greet(name) {
return `Hello, ${name}!`;
}For More you can learn from Markdown documentation
Mention any features or improvements you plan to add in the future.
## Future Enhancements
- **Save and Load Files:** Allow users to save their Markdown as a file or load Markdown files into the editor.
- **Dark Mode:** Add a toggle for dark mode to make the editor more comfortable to use in low-light conditions.
- **Plugins:** Support for additional plugins to extend the editor's functionality.Create a new React application using create-react-app:
npx create-react-app markdown-editorTo render Markdown content, install the React Markdown package:
npm install react-markdownTo add syntax highlighting for code blocks in your Markdown, install the [React Syntax Highlighter](to highlight according to coding languages) package:
npm i react-syntax-highlighterApp.jsIn the App.js file, start by setting up a basic structure with a text area for input and a ReactMarkdown component for rendering the Markdown preview.
import React, { useState } from 'react';
import ReactMarkdown from 'react-markdown';
import './App.css';
export default function App() {
return (
<div className="App">
<textarea className="textarea" />
<ReactMarkdown className="markdown" />
</div>
);
}Use Flexbox to divide the screen into two halves, with the text area on the left and the Markdown preview on the right.
/* App.css */
.App {
display: flex;
}
.textarea {
width: 50%;
height: 100vh;
padding: 20px;
font-size: 2rem;
outline: none;
}
.markdown {
width: 50%;
height: 100vh;
padding: 20px;
}App.jsAdd a state to manage the input from the text area and update the Markdown preview as the user types.
import React, { useState } from 'react';
import ReactMarkdown from 'react-markdown';
import './App.css';
export default function App() {
// State to store the input from the textarea
const [input, setInput] = useState('');
return (
<div className="App">
<textarea
autoFocus
className="textarea"
value={input}
onChange={(e) => setInput(e.target.value)}
/>
<ReactMarkdown source={input} className="markdown" />
</div>
);
}To highlight code blocks according to their respective languages, integrate react-syntax-highlighter into the App.js file.
First, choose a style from the available options, such as docco, prism, etc., and import it into your project.
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
const CodeBlock = ({ value, language }) => {
return (
<SyntaxHighlighter language={language ?? null} style={docco}>
{value ?? ''}
</SyntaxHighlighter>
);
};App.jsModify the ReactMarkdown component to use the custom CodeBlock component for rendering code blocks with syntax highlighting.
import React, { useState } from 'react';
import ReactMarkdown from 'react-markdown';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import './App.css';
export default function App() {
const [input, setInput] = useState('');
return (
<div className="App">
<textarea
autoFocus
className="textarea"
value={input}
onChange={(e) => setInput(e.target.value)}
/>
<ReactMarkdown
className="markdown"
source={input}
renderers={{
code: CodeBlock,
}}
/>
</div>
);
}
// Custom component for rendering code blocks with syntax highlighting
const CodeBlock = ({ value, language }) => {
return (
<SyntaxHighlighter language={language ?? null} style={docco}>
{value ?? ''}
</SyntaxHighlighter>
);
};