Build a Markdown to HTML Converter - Build a Markdown to HTML Converter

Tell us what’s happening:

My code seems running fine but I can’t get pass to problem #1, 2, 5 and 6. Based on my console.log the output returns coverted html yet #2 is not cleared. Please help. Thanks!

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown to HTML Converter</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <h1>Markdown to HTML Converter</h1>
    <div id="container">
        <div class="container">
            <h2>Markdown Input:</h2>
            <textarea id="markdown-input" placeholder="Enter your markdown here..."></textarea>
        </div>
        <div class="container">
            <h2>Raw HTML Output:</h2>
            <div id="html-output"></div>
        </div>
        <div class="container">
            <h2>HTML Preview:</h2>
            <div id="preview"></div>
        </div>
    </div>
</body>
<script type="module" src="script.js"></script>
</html>
/* file: styles.css */
* {
     box-sizing: border-box;
}
 body {
     font-family: Arial, sans-serif;
     padding: 20px;
}
 #markdown-input {
     width: 100%;
     height: 100px;
}
 #html-output, #preview {
     height: 100px;
     display: inline-block;
     width: 100%;
     border: 1px solid #ccc;
     padding: 10px;
     margin: auto;
     white-space: pre-wrap;
     background-color: #f9f9f9;
}
 @media (min-width: 600px) {
     #markdown-input, #html-output, #preview {
         height: 200px;
         margin: 0;
    }
     #container {
         display: flex;
         justify-content: space-evenly;
         gap: 10px;
    }
}
/* file: script.js */


const inputMarkdown = document.getElementById('markdown-input');
const htmlOutput = document.getElementById('html-output');
const preview = document.getElementById('preview');
let heading1Regex = /^# (\w.*)/gm;


function convertMarkdown() {
  let input = inputMarkdown.value;

   //covert input to html if matches the pattern
  const convertedText = input.replace(heading1Regex, '<h1>$1</h1>');
 
  //display coverted text to HTML output
  htmlOutput.innerText = convertedText;

  //Preview of coverted HTML 
  preview.innerHTML = convertedText;

  //returns coverted input text to HTML
  console.log(convertedText);

}

inputMarkdown.addEventListener('input', convertMarkdown);





Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Markdown to HTML Converter - Build a Markdown to HTML Converter

  1. The convertMarkdown function should use regular expressions to convert the markdown input from #markdown-input into HTML and should return a string containing the HTML code.

[ReferenceError: convertMarkdown is not defined]

Please re-read the above user story carefully. Your code is throwing an error because convertMarkdown() returns undefined.

1 Like

Thank you for your time to reply. I have been struggling to find out why this gives me error.

I focused on the function error and found out that instead of using
<script src="script.js"></script> in html,

I’ve been using <script type="module" src="script.js"></script>
which gives the error. Now i solved the problem #1 to 6. Thank you! ``