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

Tell us what’s happening:

So the output & preview is working exactly as it should. But it’s not passing the tests… any hint as to why???

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>
     <script src="./script.js"></script>
</body>

</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 markdownInput = document.getElementById("markdown-input");
const htmlOutput = document.getElementById("html-output");
const preview = document.getElementById("preview");


const h1 = /^# ([^\r\n#]+)/gm;
  const h2 = /^## ([^\r\n#]+)/gm;
  const h3 = /^### ([^\r\n#]+)/gm;
  const boldA = /^\*{2}([^\r\n\*]+)\*{2}$/gm;
  const boldU = /^\_{2}([^\r\n\_]+)\_{2}$/gm;
  const italicA = /^\*{1}([^\r\n\*]+)\*{1,1}$/gm;
  const italicU = /^\_{1}([^\r\n\_]+)\_{1,1}$/gm;
  const exc = /^\!\[([^\r\n\!]+)\]\(([^\r\n\!]+)\)$/gm;
  const link = /^\[([^\r\n]+)\(([^\r\n\!]+)\)$/gm;
  console.log(link.test("[alt-text](image-source)"))
  const quote = /^\> ([^\r\n\>]+)/gm;

function convertMarkdown() {
  const string = markdownInput.value;
  const newString = string
  .replace(h1, "<h1>$1</h1>")
  .replace(h2, "<h1>$1</h1>")
  .replace(h3, "<h3>$1</h3>")
  .replace(boldA, "<strong>$1</strong>")
  .replace(boldU, "<strong>$1</strong>")
  .replace(italicA, "<em>$1</em>")
  .replace(italicU, "<em>$1</em>")
  .replace(exc, "<img alt=$1 src=\"$2\">")
  .replace(link, "<a href=\"$1\">$2</a>")
  .replace(quote, "<blockquote>$1</blockquote>")
  htmlOutput.innerText = newString;
  preview.innerHTML = newString;
  return newString
};


markdownInput.addEventListener("keyup", () => {
  convertMarkdown(markdownInput.value)
})

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

you should be using a different event type. What if I paste text in the box? it will not work

I’ve changed it to “input” and although its passed some extra tests im still failing quite a few that should be passing.

I’m not sure how to test for strings that are in the $1. I’ve tried to google it but I cant find a solution. Do I need to change my approach???

if I write ## title 2 I get an h1, that is not correct

Thankyou! Completely missed that. Fixed that.

Any suggestion for how I can turn the $1/2 into strings to check for regex?

if you use a callback as second argument of replace instead of a string, you should be able to do that

okay so I keep running into the same issue. Do i need to use a loop or something to re-check the string??

Super confused as to how to pass the tests where there is a regex within the regex!