s3943923 / automatarium

2024 Sem 2 Group
https://automatarium.tdib.xyz
MIT License
0 stars 0 forks source link

Add markdown to text area #28

Open s3944192 opened 1 week ago

S3882476 commented 1 week ago

I found something here

this requires install the markdown from react

npm install marked

then this works on my react example program

import React, { useState } from 'react'; import { marked } from 'marked';

const MarkdownEditor = () => { const [markdown, setMarkdown] = useState('');

const handleChange = (event) => {
    setMarkdown(event.target.value);
};

// Convert markdown to HTML  
const getMarkdownText = () => {
    const rawMarkup = marked(markdown, { sanitize: true }); 
    return { __html: rawMarkup };
};

return (
    <div>
        <h1>Markdown Editor</h1>  
        <textarea        
            rows="10"
            cols="50"
            value={markdown}            <--!  this is text area input from template -->
            onChange={handleChange}
            placeholder="Type your markdown here..."
        />
        <h2>Output</h2>
        <div dangerouslySetInnerHTML={getMarkdownText()} />
    </div>
);

};

export default MarkdownEditor;