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

Tell us what’s happening:

guys I need help with this one please I don’t know why it doesn’t work

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>

</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 */
function convertMarkdown() {
  const input = document.getElementById("markdown-input").value;
  const lines = input.split("\n");
  let html = "";

  function inlineFormat(text) {
    // images
    text = text.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, '<img alt="$1" src="$2">');

    // links
    text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');

    // bold (** or __)
    text = text.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
    text = text.replace(/__(.+?)__/g, "<strong>$1</strong>");

    // italic (* or _)
    text = text.replace(/\*(.+?)\*/g, "<em>$1</em>");
    text = text.replace(/_(.+?)_/g, "<em>$1</em>");

    return text;
  }

  lines.forEach(line => {
    // h3
    if (/^\s*###\s+/.test(line)) {
      html += `<h3>${inlineFormat(line.replace(/^\s*###\s+/, ""))}</h3>`;
    }
    // h2
    else if (/^\s*##\s+/.test(line)) {
      html += `<h2>${inlineFormat(line.replace(/^\s*##\s+/, ""))}</h2>`;
    }
    // h1
    else if (/^\s*#\s+/.test(line)) {
      html += `<h1>${inlineFormat(line.replace(/^\s*#\s+/, ""))}</h1>`;
    }
    // blockquote
    else if (/^\s*>\s+/.test(line)) {
      html += `<blockquote>${inlineFormat(line.replace(/^\s*>\s+/, ""))}</blockquote>`;
    }
    // standalone content (bold, italic, links, images)
    else {
      html += inlineFormat(line);
    }
  });

  return html;
}

// INPUT EVENT HANDLING
document.getElementById("markdown-input").addEventListener("input", () => {
  const output = convertMarkdown();

  document.getElementById("html-output").textContent = output;
  document.getElementById("preview").innerHTML = output;
});

Your browser information:

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

Challenge Information:

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

hello! Is your index.html connected to script.js?

oh thanks

oh thanks :grinning_face:

1 Like