Designing a Markdown note taking application in ReactJS
In this article, we will learn how to design a Markdown note-taking application in React that can help one to quickly jot down notes and download the rendered output. The use of Markdown allows one to spend less time on formatting the text and therefore create rich text content using only the Markdown syntax.
We will be using the React library to quickly create the User Interface as it would handle the updating of the DOM as required. We will use Bootstrap CSS to design the application and make it user-friendly. We will also use a Blob to save the content to our device.
We will be following the below steps for creating our application:
Step 1: Creating the project
We will start by creating a new react project using the create-react-app tool. You can use the following command for creating one with the desired name. Please ensure that Node and npm are installed on the system.
npx create-react-app react-md-note-app
Project Structure: We will then delete all the files that are not necessary. The file structure after deleting the files is given below:
Step 2: Installing necessary modules
We will start by adding the Bootstrap CDN to the index.html page. The CDN links can be found here and they can directly be added in the head section of the index page.
We will also need two npm packages, one is the markdown-it package that does the heavy-lifting of parsing the Markdown content and giving us the rendered output in HTML. We can simply use this HTML for displaying our output. The other module is the file-saver package that simplifies the task of saving the given blob to the system. The following command is used for installing these two packages:
npm install markdown-it file-saver
Step 3: Creating a simple User Interface using Bootstrap
We will create the layout using the Bootstrap Grid system to ensure that our application is mobile responsive and looks good without writing a lot of CSS.
The application can be divided into 3 parts; the markdown editor that will allow the user to enter the notes in Markdown, the output area that shows the rendered HTML, and the file saving options that contain the input field for the filename and the buttons for saving the content. Each of the parts is nested using the Card component of Bootstrap and the relevant components are added to the input elements.
We will also need some CSS rules that can be imported into our App.js file. We can create a new file named App.css and write the rules as below:
Step 4: Creating the hooks that will be used to manage the state
We will create 3 hooks using the useState method of React for managing the state in the 3 parts of our application. Each of the hooks returns the variable itself with its initial state and the function that can be used to set the state of that variable. React will automatically re-render the component when its state changes.
This will allow us to simply use the respective functions and the variables for handling the state of each component.
Step 5: Using the state variables in the application
We will now use these variables inside the 3 components we created earlier.
The markdownText variable is used in the textarea where we will write our markdown text. We will use this in the value property of the textarea. Using the setMarkdownText function we can change the value of the text here and it would be displayed via this value property.
The renderedHTML variable is used in the div element that will show the rendered output of the markdown. As the MarkdownIt module will return the HTML from the markdown, we will need to set the inner HTML of this element directly. This will require the use of the dangerouslySetInnerHTML property of the element and passing an object with the key of _html.
Note: This is a feature in React that discourages the practice of directly modifying the HTML to prevent Cross-Site Scripting (XSS) attacks on a webpage. It is recommended to sanitize any user-input HTML content before directly using it.
Finally, the fileName variable is used in the file saver options so that it can be used to enter the filename of the content to be saved. We will also bind the onChange event handler to setFileName so that any value entered by the user is updated to the variable. We can do this by accessing the event target and getting the value property.
Step 6: Creating the functions we have defined earlier
The handleTextInput() function: This function will take the text that is entered in the markdown editor and use MarkdownIt to render the HTML needed.
We will first get the text that is entered by getting the value of the target. We then initialize a new instance of MarkdownIt and use its render method. We will then use setMarkdownHTML function to set this in the output area where our final HTML will be displayed. This function will be used in the onChange property of the markdown editor.
The saveHTML() and saveMarkdown() functions: These functions will be used to save content to our system with the given filename. In both of these functions, we will create a Blob object with the content to be saved. We can use the renderedHTML and markdownText variables to get the current text content of the application.
The type of the blob is specified next. We can use text/html for the rendered output so that it is saved as an HTML file and only text for the markdown text.
We will now use the saveAs() method of the FileSaver module to save the file to the system. This method uses two parameters, the first one is the blob object that has to be saved and the second one is the file name that should be used to save the file. We can get the file name by using the fileName variable and pass the blob object we created earlier.
We can bind these functions with the respective onClick event handlers of the two buttons we have created. Clicking on the buttons will open a Save As dialog on the browser with the given filename and the user can choose to save the file as required.
Step 6: Running and Building the application
We can run this application by using the following command. This will start React’s development server that can be used for debugging our application.
npm run start
We can also build this application so that it could be hosted on any platform that supports static file hosting. We can build our application using the following command to run the project:
npm run build