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

Tell us what’s happening:

Please can someone help me out. I can’t pass 32 and many more :pensive_face::pensive_face:

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


function convertMarkdown(){
  let value = markdown.value
  const heading = /^(#{1,3})\s+(.+)/gm
  if(heading.test(value)){
    return  value.replace(heading, (fullContent, firstCaptured, secondCaptured) => {
  return `<h${firstCaptured.length}>${secondCaptured}</h${firstCaptured.length}>`})
  

  }
  const strong = /(_|\*){2}(.+)(_|\*){2}/gm
if(strong.test(value)){
  return  value.replace(strong, (rawContext,el,content) => {
    return `<strong>${content}</strong>`
   })
   
} 
   const italic = /^(\*|_)(.+)(\*|_)$/gm

   if(italic.test(value)){
    return value.replace(italic, (rawContent, first, content) => {
    return `<em>${content}</em>`
   })
   }
  const img = /^(!\[.+])(.+)$/gm
  if(img.test(value)){
    return value.replace(img, (rawContent, first, second) => {
    return `<img alt="${first.slice(2, first.length - 1)}" src="${second.slice(1, second.length - 1)}">`
    })
  }
  const anchor = /^(\[.+])(.+)$/gm
  if(anchor.test(value)){
     return value.replace(anchor, (rawContent, first, second) => {
      return `<a href="${second.slice(1, second.length - 1)}>${first.slice(1, first.length - 1)}</a>`
     } )
  }
  const quote = /^(>)\s+(.+)/gm
  if(quote.test(value)){
   return value.replace(quote, (rawContent, first, second) => {
    return `<blockquote>${second}</blockquote>`
   })
  }

  return value
}
markdown.addEventListener("input", () => {
rawOut.innerText = convertMarkdown()
output.innerHTML = convertMarkdown()
})

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Mobile Safari/537.36

Challenge Information:

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-markdown-to-html-converter/66f55eac933ff64ce654ca74.md at main · freeCodeCamp/freeCodeCamp · GitHub

I already figured out some issues of my code

That’s great. If you need more help, please post your updated code, formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

I don’t think it’s available on my phone :thinking::thinking:

What exactly is it you don’t think is available on your phone?

I don’t think the (M+) is on my phone. I haven’t seen it before

When you reply to a post, it’s right there next to the A. I just checked it from my phone:

But because of the limited screen real estate, the button will toggle the rich text editor rather than opening it up next to the regular editor.

That said, you don’t need that to format your code. You just need the three backticks or the “Preformatted text” </> button for that.

Happy coding

thank you. i already got a laptop so i will try it out