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

Tell us what’s happening:

Why does the code not work in this scenario? Code failing in tests 6, 11, 16 and 46.

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 src="script.js"></script>
</html>
/* file: script.js */
let markdown = document.getElementById("markdown-input");
markdown.addEventListener("input", convertMarkdown);
let htmlOut = document.getElementById("html-output");
let preview = document.getElementById("preview");

convertMarkdown();
function convertMarkdown(){
  let regexHeader = /(\#+ )([^#]+)/gi; 
  let regexBold = /\*\*(.+)\*\*|\_\_(.+)\_\_/gi
  let regexItalics = /(?<!\*)\*(.*?)\*(?!\*)|(?<!_)_(.+)\_(?!_)/gi;
  let regexImg = /!\[(.+)\]\((.+)\)/gi;
  let regexLink = /(?<!!)\[(.+)\]\((.+)\)/gi
  let regexQuote = /> ([^>]+)/gi;
  let mkdown = markdown.value;
  let m = mkdown;

  if(mkdown[0] == ">"){
    m = mkdown.replace(regexQuote, quoteReplace);
  }
  if(mkdown[0] == "#"){
    m = mkdown.replace(regexHeader, headerReplace);
  }
  m = m.replace(regexBold, boldReplace);
  m = m.replace(regexItalics, italsReplace);
  m = m.replace(regexImg, imgReplace);
  m = m.replace(regexLink, linkReplace);
  

  htmlOut.textContent = m;
  preview.innerHTML = m;
  return m;
}

function headerReplace(match, p1 ,p2){
  if(p1) return `<h${p1.length - 1}>${p2}</h${p1.length - 1}>`; 
}

function boldReplace(match, p1, p2){
  if(p1) return `<strong>${p1}</strong>`;
  return `<strong>${p2}</strong>`;
}

function italsReplace(match, p1, p2){
  if(p1) return `<em>${p1}</em>`;
  return `<em>${p2}</em>`;
}

function imgReplace(match, p1, p2){
  return `<img alt="${p1}" src="${p2}">`;
}

function linkReplace(match, p1, p2){
  return `<a href="${p2}">${p1}</a>`;
}

function quoteReplace(match, p1){
  return `<blockquote>${p1}</blockquote>`;
}
/* 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;
    }
}

Your browser information:

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

Challenge Information:

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

Please talk about what debugging you’ve tried and how you got stuck figuring out what is wrong with your code

I tried running the example tests and they seem to work fine, I assume its not working because of the if statements I use to prevent matches in cases there is text before bolds and headers, but don’t know what is wrong about them, it does work in my test. Am I suposed to use regex to do those checks instead of using if statements? I tried doing that but cound’nt figure it out without inpacting the code functionality.

Sorry, aparently I missinterpreted what was asked, I thought the bolds and headers needed to match multiple times in the same line, not in new lines, will keep trying, my bad.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.