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

Tell us what’s happening:

I am working on the Markdown Previewer project. I am passing most tests, but I am consistently failing requirements 4, 9, and 34. These relate to the H1 and H2 headers and the nested bold text within an H1.
Even though my #preview looks correct visually, the test suite is not acknowledging the elements. I suspect it might be a whitespace issue or the order of my regex replacements.

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" />
    <link rel="stylesheet" href="styles.css" />
    <title>Markdown HTML Converter</title>
  </head>
  <body>
    <h1>Markdown 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: script.js */
const editor = document.getElementById("markdown-input");
const rawOutput = document.getElementById("html-output");
const preview = document.getElementById("preview");

const convertMarkdown = () => {
  const input = document.getElementById("markdown-input").value;
  let html = input;

  html = html.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
  html = html.replace(/__(.*?)__/g, "<strong>$1</strong>");
  html = html.replace(/\*(.*?)\*/g, "<em>$1</em>");
  html = html.replace(/_(.*?)_/g, "<em>$1</em>");

  html = html.replace(/^### (.*$)/gm, "<h3>$1</h3>");
  html = html.replace(/^## (.*$)/gm, "<h2>$1</h2>");
  html = html.replace(/^# (.*$)/gm, "<h1>$1</h1>");

  html = html.replace(/!\[(.*?)\]\((.*?)\)/g, '<img alt="$1" src="$2">');
  html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>');
  html = html.replace(/^> (.*$)/gm, "<blockquote>$1</blockquote>");

  return html;
};

editor.addEventListener("input", () => {
  const result = convertMarkdown();
  rawOutput.textContent = result;
  preview.innerHTML = result;
});

/* file: styles.css */
:root {
  --bg: #0b0e14;
  --panel: #151921;
  --accent: #4fd1c5;
  --text: #cbd5e1;
  --border: #2d3748;
}

body {
  background-color: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

h1 {
  text-align: center;
  color: var(--accent);
  font-weight: 300;
  letter-spacing: 2px;
  text-transform: uppercase;
  margin: 20px 0;
  font-size: 1.5rem;
}

/* Layout Container Utama */
#container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 20px;
  min-height: 100vh;
}

/* Panel Individual */
.container {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 250px;
  padding: 15px;
  transition: border-color 0.3s ease;
}

.container:focus-within {
  border-color: var(--accent);
}

h2 {
  font-size: 0.9rem;
  color: #718096;
  margin-top: 0;
  margin-bottom: 10px;
  text-transform: uppercase;
}

/* Editor, Output, Preview Styling */
#markdown-input,
#html-output,
#preview {
  width: 100%;
  height: 100%;
  padding: 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: #e2e8f0;
  box-sizing: border-box;
  resize: none;
  font-family: "Fira Code", "Consolas", monospace;
  font-size: 16px;
  line-height: 1.8;
  letter-spacing: 0.5px;
  outline: none;
  overflow-y: auto;
  flex-grow: 1;
  width: 100%;
  box-sizing: border-box;
}

#markdown-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 10px rgba(79, 209, 197, 0.1);
}

/* Khusus Preview */
#preview {
  background: #ffffff;
  color: #1a202c;
  font-family: "Georgia", serif;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

Your browser information:

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

Challenge Information:

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

Hi @wahyuniharries

The updated CSS is causing the tests to fail.

Happy coding

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