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

Tell us what’s happening:

I’m stuck on this test :

Failed: 38. When the value of #markdown-input is ![alt-text](image-source) followed by ![alt-text-2](image-source-2) on a new line, convertMarkdown() should return <img alt="alt-text" src="image-source"><img alt="alt-text-2" src="image-source-2">.

Although I’m passing this one :

Passed: 42. When the value of “#markdown-input” is [link text](URL) followed by [link text 2](URL 2) on a new line, “convertMarkdown()” should return <a href="URL">link text</a><a href="URL2">link text 2</a>.

When my input is :

![alt-text](image-source)
![alt-text-2](image-source-2)

[link text](URL)
[link text 2](URL2) 

The output in #html-output is

<img alt="alt-text" src="image-source">
<img alt="alt-text-2" src="image-source-2">

<a href="URL">link text</a>
<a href="URL2">link text 2</a> 

I have tried adding .replace(/\n(?=<img)/g, "") to ‘manually’ replace line breaks after img, but I still fail 38.

The only difference between regexImg and regexLink is ^!, I don’t understand why one passes the test and the other not.

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

// Defining regexes to be used in convertMarkdown()
const regexH1 = /^#\s+([^\n]+)/gm;
const regexH2 = /^##\s+([^\n]+)/gm;
const regexH3 = /^###\s+([^\n]+)/gm;
const regexBold = /(?:\*\*|__)(.*?)(?:\*\*|__)/gm;
const regexItalic = /(?:\*|_)(.*?)(?:\*|_)/gm;
const regexImg = /^!\[(.*?)\]\((.*?)\s?(?:"(.*?)")?\)/gm;
const regexLink = /\[(.*?)\]\((.*?)\s?(?:"(.*?)")?\)/gm;
const regexQuote = /^>\s+([^\n]+)/gm;

function convertMarkdown() {
  const markdownContent = markdownInput.value;
  let convertedText = markdownContent
    .replace(regexImg, '<img alt="$1" src="$2">')
    .replace(regexLink, '<a href="$2">$1</a>')
    .replace(regexBold, "<strong>$1</strong>")
    .replace(regexItalic, "<em>$1</em>")
    .replace(regexQuote , "<blockquote>$1</blockquote>")
    .replace(regexH3, "<h3>$1</h3>")
    .replace(regexH2, "<h2>$1</h2>")
    .replace(regexH1, "<h1>$1</h1>");
  return convertedText;
}

markdownInput.addEventListener("input", function() {
  htmlOutput.textContent = convertMarkdown();
});
markdownInput.addEventListener("input", function() {
  htmlPreview.innerHTML = htmlOutput.textContent;
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0

Challenge Information:

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

Nevermind, found it.
I needed to remove the ^ anchor, at the beginning of regexImg