Tell us what’s happening:
I failed a lot of tests but I think the code is works. Why?
2. When the value of #markdown-input is # title 1, convertMarkdown() should return title 1.
- 6. When the value of #markdown-input is
# title 1followed by# alternate titleon a new line, convertMarkdown() should returntitle 1alternate title. - 7. When the value of #markdown-input is
## title 2, convertMarkdown() should returntitle 2. - 10. When the value of #markdown-input is some text
## title 2, convertMarkdown() should not convert## title 2into an h2 element. - 11. When the value of #markdown-input is
## title 2followed by## title 2 alton a new line, convertMarkdown() should returntitle 2title 2 alt. - 12. When the value of #markdown-input is
### title 3, convertMarkdown() should returntitle 3. - 15. When the value of #markdown-input is some text
### title 3, convertMarkdown() should not convert### title 3into an h3 element. - 16. When the value of #markdown-input is
### title 3followed by### third titleon a new line, convertMarkdown() should returntitle 3third title. - 17. When the value of #markdown-input is
**this is bold**, convertMarkdown() should returnthis is bold. - 20. When the value of #markdown-input is
**this is bold**followed by**this is also bold**on a new line, convertMarkdown() should returnthis is boldthis is also bold. - 23. When the value of #markdown-input is
_this is bold_followed by_this is also bold_on a new line, convertMarkdown() should returnthis is boldthis is also bold. - 24. When the value of #markdown-input is
*this is italic*, convertMarkdown() should returnthis is italic. - 27. When the value of #markdown-input is
*this is italic*followed by*this is also italic*on a new line, convertMarkdown() should returnthis is italicthis is also italic. - 28. When the value of #markdown-input is
_this is italic_, convertMarkdown() should returnthis is italic. - 31. When the value of #markdown-input is
_this is italic_followed by_this is also italic_on a new line, convertMarkdown() should returnthis is italicthis is also italic. - 32. When the value of #markdown-input is either
# **title 1**or# _title 1_, convertMarkdown() should returntitle 1. - 35. When the value of #markdown-input is
, convertMarkdown() should return<img>(or corresponding HTML). - 38. When the value of #markdown-input is
followed byon a new line, convertMarkdown() should return<img><img>(or corresponding HTML). - 39. When the value of #markdown-input is
[link text](URL), convertMarkdown() should returnlink text. - 42. When the value of #markdown-input is
[link text](URL)followed by[link text 2](URL2)on a new line, convertMarkdown() should returnlink textlink text 2. - 43. When the value of #markdown-input is
> this is a quote, convertMarkdown() should returnthis is a quote. - 46. When the value of #markdown-input is
> this is a quotefollowed by> this is another quoteon a new line, convertMarkdown() should returnthis is quotethis is another quote. - 47. When the value of #markdown-input is some text
> not a quote anymore, convertMarkdown() should not convert> not a quote anymoreinto a blockquote element. - 48. When the value of #markdown-input is
> **this is a *quote***, convertMarkdown() should returnthis is a quote.
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 markdownInput=document.querySelector("#markdown-input");
const htmlOutput=document.querySelector("#html-output");
const preview=document.querySelector("#preview");
const h1=/^#\s(.*)/gm;
const h2=/^#{2}\s(.*)/gm;
const h3=/^#{3}\s(.*)/gm;
const strong=/(\*\*|__)(.*?)\1/g;
const em=/(\*|_)(.*?)\1/g;
const img=/!\[(.*)\]\((.*)\)/g;
const a=/\[(.*)\]\((.*)\)/g;
const blockquote=/^>\s(.*)/gm;
const convertMarkdown=input=> {
input=input.replace(blockquote,"<blockquote>$1</blockquote>");
input=input.replace(h3,"<h3>$1</h3>");
input=input.replace(h2,"<h2>$1</h2>");
input=input.replace(h1,"<h1>$1</h1>");
input=input.replace(strong,"<strong>$2</strong>");
input=input.replace(em,"<em>$2</em>");
input=input.replace(img,"<img alt=\"$1\" src=\"$2\">");
input=input.replace(a,"<a href=\"$2\">$1</a>");
return input;
}
markdownInput.addEventListener("input",()=> {
htmlOutput.innerText=convertMarkdown(markdownInput.value);
preview.innerHTML=convertMarkdown(markdownInput.value);
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Challenge Information:
Build a Markdown to HTML Converter - Build a Markdown to HTML Converter