Tell us what’s happening:
A lot of tests are failing, but the preview and raw html output seems to be correct.
Any ideas about what is going wrong ?
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: script.js */
const markdownInput = document.querySelector("#markdown-input");
const htmlOutput = document.querySelector("#html-output");
const preview = document.querySelector("#preview");
const convertMarkdown = (markdownText) => {
const h1 = /^#{1}\s+(.*)$/gim;
const h2 = /^#{2}\s+(.*)$/gim;
const h3 = /^#{3}\s+(.*)$/gim;
const h4 = /^#{4}\s+(.*)$/gim;
const h5 = /^#{5}\s+(.*)$/gim;
const h6 = /^#{6}\s+(.*)$/gim;
const bold = /(\*\*|__)(.*?)\1/g;
const italic = /(?<!\*)\*([^*\n]+)\*(?!\*)/g;
const link = /\[([^\]]+)\]\(([^)]+)\)/gim;
const quote = /^(>\s?.+(?:\n>\s?.+)*)/gm;
let html = markdownText.replace(h6, `<h6>$1</h6>`);
html = html.replace(h5, `<h5>$1</h5>`);
html = html.replace(h4, `<h4>$1</h4>`);
html = html.replace(h3, `<h3>$1</h3>`);
html = html.replace(h2, `<h2>$1</h2>`);
html = html.replace(h1, `<h1>$1</h1>`);
html = html.replace(bold, `<strong>$2</strong>`);
html = html.replace(italic, `<em>$1</em>`);
html = html.replace(link, `<a href="$2">$1</a>`);
html = html.replace(quote, `<blockquote>$1</blockquote>`);
html = html
.split(/\n{2,}/)
.map((block) => {
if (/^\s*<\/?(h\d|blockquote)/.test(block)) {
return block;
}
return `<p>${block.trim()}</p>`;
})
.join("");
return html.trim().replaceAll(/\n/g, "");
};
markdownInput.addEventListener("input", () => {
const markDownToConvert = markdownInput.value;
const converted = convertMarkdown(markDownToConvert);
preview.innerHTML = converted;
htmlOutput.innerText = converted;
console.log(converted);
});
/* 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 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0
Challenge Information:
Build a Markdown to HTML Converter - Build a Markdown to HTML Converter