Click­ing on the Mark­down box pastes text from the clip­board, auto­mat­i­cal­ly con­verts it to HTML, and copies the result­ing HTML back to the clip­board.

Head Section

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Markdown to HTML Converter</title>
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>

Body Section

<body>

  <h2>Markdown to HTML Converter</h2>
  
  <!-- Input area for Markdown text -->
  <textarea id="markdown-input" placeholder="Click here to paste from clipboard..." rows="10" style="width: 100%;" onclick="pasteAndConvert()"></textarea>
  
  <!-- Output area for HTML result -->
  <h3>HTML Output (automatically copied):</h3>
  <textarea id="html-output" readonly rows="10" style="width: 100%;"></textarea>

  <script>
    // Function to paste content from the clipboard, convert to HTML, and copy HTML to clipboard
    async function pasteAndConvert() {
      try {
        // Paste clipboard text into the Markdown input
        const text = await navigator.clipboard.readText();
        const markdownInput = document.getElementById("markdown-input");
        markdownInput.value = text;

        // Convert the pasted Markdown to HTML
        const htmlOutput = marked.parse(text);

        // Display the HTML in the output box
        document.getElementById("html-output").value = htmlOutput;

        // Copy the HTML output to the clipboard
        await copyToClipboard(htmlOutput);

        // Notify the user
        alert("HTML output has been copied to clipboard!");
      } catch (err) {
        alert("Failed to read clipboard contents: " + err);
      }
    }

    // Helper function to copy text to clipboard
    async function copyToClipboard(text) {
      try {
        await navigator.clipboard.writeText(text);
      } catch (err) {
        console.error("Failed to copy text to clipboard: ", err);
      }
    }
  </script>

</body>

Explanation

  1. pasteAndConvert(): This func­tion runs when you click on the Mark­down input box.

    • It reads the clip­board con­tent and pastes it into the Mark­down box.
    • It con­verts the past­ed Mark­down to HTML using marked.parse(text).
    • It copies the con­vert­ed HTML back to the clip­board and alerts the user.
  2. copyToClipboard(): A helper func­tion that copies text to the clip­board, used to ensure that the HTML out­put is copied back smooth­ly.

Now, you can click on the Mark­down box, and it will:

  • Paste clip­board con­tent into the Mark­down box.
  • Con­vert it to HTML.
  • Auto­mat­i­cal­ly copy the HTML out­put back to the clip­board and noti­fy you that it’s done.

John Deacon

John is a researcher and practitioner committed to building aligned, authentic digital representations. Drawing from experience in digital design, systems thinking, and strategic development, John brings a unique ability to bridge technical precision with creative vision, solving complex challenges in situational dynamics with aims set at performance outcomes.

View all posts