DEV Community

Cover image for Build an HTML To Markdown Converter using ToolJet📋
Karan Rathod for ToolJet

Posted on • Originally published at blog.tooljet.com

Build an HTML To Markdown Converter using ToolJet📋

In this tutorial, we will walk through the steps to build an HTML to Markdown Converter using ToolJet. This application allows users to input HTML code and get the corresponding Markdown output which can be copied with a button click. Using ToolJet's intuitive pre-built components, we'll design an elegant UI in just 5 minutes. The HTML to markdown conversion will be managed by the Turndown JavaScript library, while the Markdown preview will be rendered using the react-markdown library.

ToolJet's Run JavaScript query and Text component can be utilized to convert HTML to Markdown and preview the content without using external libraries. However, in this tutorial, we are looking to demonstrate the process of using 3rd party libraries in ToolJet.

Here's a preview of the complete application:

Image description


Prerequisites:

ToolJet(https://github.com/ToolJet/ToolJet) : An open-source, low-code business application builder. Sign up for a free ToolJet cloud account or run ToolJet on your local machine using Docker.

Basics of JavaScript: A basic understanding of JavaScript is essential for this tutorial since we will use JavaScript and React libraries to handle the logic for converting HTML to Markdown and previewing the markdown content.

To begin, create a new application named HTML To Markdown Converter.


Step 1: Creating the UI

Header

  • Add a Container component at the top of the canvas and rename it to headerContainer.
  • Add an Icon and a Text component inside the Container. Rename them to logo and headerText respectively.
  • Set the text to "HTML to Markdown Converter" and select an appropriate icon for the app in the Icon component.

Image description

Input Section

  • Add another Container component on the left and rename it to inputContainer.
  • Inside the container, add a Text component labeled "Enter HTML Code:". Rename it to enterHTMLLabel.
  • Below the label, add a Textarea component for the HTML input and rename it to htmlInput.

Output Section

  • Add a container next to the input section and rename it to outputContainer.
  • Add a Text component labeled "Markdown Preview:" below the input section. Rename it to markdownLabel.
  • Add a Custom Component to display the Markdown output and rename it to markdownOutput.

Buttons

  • Add a Button component labeled "Convert" below the Textarea component. Rename the button to convertButton.
  • Add another Button component labeled "Copy Markdown" besides the Convert button and rename it to copyButton.

Image description

The UI of the application is done. Time to focus on the functionality.


Step 2: Adding Queries and Events

Import Turndown Library

  • Create a Run JavaScript code query named importTurndownLibrary to import the Turndown library.
  • Use the following code in the query to import the library:
// Function to add script dynamically
function addScript(src) {
    return new Promise((resolve, reject) => {
        const scriptTag = document.createElement('script');
        scriptTag.setAttribute('src', src);
        scriptTag.addEventListener('load', resolve);
        scriptTag.addEventListener('error', reject);
        document.body.appendChild(scriptTag);
    });
}

try {
    // Importing turndown
    await addScript('https://cdnjs.cloudflare.com/ajax/libs/turndown/7.0.0/turndown.js');

    // Showing a success alert
    await actions.showAlert("success", 'Turndown JS is imported');
    turndownService.turndown(components.textarea1.value);
} catch (error) {
    console.error(error);
}
Enter fullscreen mode Exit fullscreen mode
  • Enable the Run this query on application load? toggle to automatically run this query every time the app loads.

Image description

Convert HTML to Markdown

  • Create another Run JavaScript code query named convertToMarkdown.
  • Use the following code in the query to convert the text entered in the htmlInput component to markdown:
const turndownService = new TurndownService();
const markdown = turndownService.turndown(components.htmlInput.value);

return markdown;
Enter fullscreen mode Exit fullscreen mode

Image description

Button Actions

  • For the Convert button, create a new On click event, set the Action as Run Query, and select convertToMarkdown as the Query.

Image description

  • For the Copy Markdown button, create a new On click event, set the Action as Copy to clipboard the {{queries.convertToMarkdown.data}} as the Text.

Image description

Most of the key functionality is now configured. In the next step, we will use a Custom Component to import a React library to preview the markdown content.


Step 3: Creating Markdown Preview

Add Custom Component

  • In the Custom Component, add the data that will be returned by the convertToMarkdown query:
{{
   { preview: queries.convertToMarkdown.data}
}}
Enter fullscreen mode Exit fullscreen mode
  • Use the following code to render the Markdown preview:
import React from 'https://cdn.skypack.dev/react';
import ReactDOM from 'https://cdn.skypack.dev/react-dom';
import { Container } from 'https://cdn.skypack.dev/@material-ui/core';
import Markdown from 'https://esm.sh/react-markdown@9';

const MyCustomComponent = ({ data }) => (
    <Container>
        <Markdown>{data.preview}</Markdown>
    </Container>
);

const ConnectedComponent = Tooljet.connectComponent(MyCustomComponent);
ReactDOM.render(<ConnectedComponent />, document.body);
Enter fullscreen mode Exit fullscreen mode

Image description

With this, the HTML to Markdown converter is ready. Time to see it in action!


Step 4: Preview and Testing

  • Test the application by entering various HTML snippets and checking the Markdown output.
  • Ensure that the Copy Markdown button works correctly.

Image description


Conclusion

We have successfully built an HTML to Markdown Converter using ToolJet in just minutes! This application is designed to simplify your content formatting process, and you can easily expand its functionality based on your specific requirements. Explore the capabilities of ToolJet to create similar projects and enhance your workflow.

You can check out ToolJet docs for further learning or join the ToolJet Slack community for doubts and queries.

Top comments (1)

Collapse
 
polterguy profile image
Info Comment hidden by post author - thread only accessible via permalink
Thomas Hansen

Oh, wow! Another tutorial on converting HTML to Markdown using a low-code tool. How original! I bet the world was just holding its breath for this one. Because, you know, the countless other libraries and tools out there just weren’t cutting it.

But hey, kudos to ToolJet for making it “easier” to do something that a simple script could handle. And using third-party libraries like Turndown and react-markdown? Genius! Why reinvent the wheel when you can just slap someone else’s wheel onto your cart, right?

I mean, who needs Hyperlambda or Lisp when you can just drag and drop your way to mediocrity? Bravo! 👏

Thomas Ai-Ai Shitboat Handsom

“Give a man a fish, and he’ll eat for a day. Teach a man to fish, and he’ll spend the rest of his life trying to figure out how to use ToolJet.”

PLEASE FORGIVE ME!! 😂

ainiro.io/blog/how-to-create-an-ai...

Some comments have been hidden by the post's author - find out more