Tell us what’s happening:
Waht seemes the problem? in the second test It’s output correct for me but the test isn’t working
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.getElementById("markdown-input")
const htmlOutput = document.getElementById("html-output")
const preview = document.getElementById("preview")
let markdownValue ;
let markdownHTML;
let debouncingTimer;
function createHeadingTag(headingTagContent) {
const headingRegexes = [];
headingTagContent.forEach(e => {
const headingNumber = e[1].length
const headingContent = e[2]
const headingRegex = new RegExp(`\\s*#{${headingNumber}} ${headingContent}`, 'gm')
headingRegexes.push({
"headingRegex": headingRegex,
"headingTag": `<h${headingNumber}>${headingContent}</h${headingNumber}>`})
})
headingRegexes.forEach(e => {
markdownValue = markdownValue.replace(e["headingRegex"], e["headingTag"])
markdownHTML = markdownHTML.replace(e["headingRegex"], e["headingTag"])
preview.innerHTML = markdownValue
})
return markdownHTML
}
function createEmTag() {
const italicRegex = /([*_])[ ]*([^*_]*)[ ]*\1/gm
markdownValue = markdownValue.replace(italicRegex, '<em>$2</em>')
markdownHTML = markdownHTML.replace(quoteRegex, '<em>$2</em>')
preview.innerHTML = markdownValue
return markdownHTML
}
function createStrongTag() {
const strongRegex = /([*_]{2})((?:(?![*_]{2})[\s\S])*)\1/gm
markdownValue = markdownValue.replace(strongRegex, '<strong>$2</strong>')
preview.innerHTML = markdownValue
return markdownHTML = markdownHTML.replace(quoteRegex, `<strong>$1</strong>`)
}
function createImageTag() {
const imageRegex = /!\[([^\]]*)\]\(([^\)]*)\)/gm
markdownValue = markdownValue.replace(imageRegex, '<img alt="$1" src="$2"/>')
preview.innerHTML = markdownValue
return markdownHTML= markdownHTML.replace(quoteRegex, '<img alt="$1" src="$2"/>')
}
function createLinkTag() {
const imageRegex = /(?<!!)\[([^\]]*)\]\(([^\)]*)\)/gm
markdownValue = markdownValue.replace(imageRegex, "<a href='$2'>$1</a>")
preview.innerHTML = markdownValue
return markdownHTML = markdownHTML.replace(quoteRegex,"<a href='$2'>$1</a>")
}
function createBlockquoteTag() {
const quoteRegex = /^\s*> ([\s\S]+)/gm
markdownValue = markdownValue.replace(quoteRegex, `<blockquote>$1</blockquote>`)
preview.innerHTML = markdownValue
return markdownHTML = markdownHTML.replace(quoteRegex, `<blockquote>$1</blockquote>`)
}
function convertMarkdown() {
const headingsRegex = /^\s*(#{1,6}) ([^\n]+)/gm
const italicRegex = /([*_])[ ]*([^*_]*)[ ]*\1/gm
const strongRegex =/([*_]{2})((?:(?![*_]{2})[\s\S])*)\1/gm
const imageRegex = /(?<=!)\[([^\]]*)\]\(([^\)]*)\)/gm
const linkRegex = /(?<!!)\[([^\]]*)\]\(([^\)]*)\)/gm
const quoteRegex = /^\s*> ([^\]]+)/gm
let isMatch = false
if(headingsRegex.test(markdownInput.value)) {
//to fix the problem when usign const with g flag
headingsRegex.lastIndex = 0
let headingTagContent = [...markdownInput.value.matchAll(headingsRegex)]
isMatch = true
return createHeadingTag(headingTagContent)
} if(strongRegex.test(markdownInput.value)) {
createStrongTag()
isMatch = true
} if(italicRegex.test(markdownInput.value)) {
createEmTag()
isMatch = true
} if(imageRegex.test(markdownInput.value)) {
createImageTag()
isMatch = true
} if(linkRegex.test(markdownInput.value)) {
createLinkTag()
isMatch = true
} if(quoteRegex.test(markdownInput.value)) {
createBlockquoteTag()
isMatch = true
} if(!isMatch) {
preview.innerHTML = markdownInput.value
return markdownInput.value
}
}
markdownInput.addEventListener("input", () => {
markdownHTML = markdownInput.value
markdownValue = markdownInput.value
clearTimeout(debouncingTimer)
debouncingTimer = setTimeout(() => {
htmlOutput.innerText = convertMarkdown()
console.log(convertMarkdown())
}, 1)
})
/**
*
code: [..."### osama \n ## ahmed".matchAll(/(^s*#+) (w+)/gm)]
[ [ '### osama',
'###',
'osama',
index: 0,
input: '### osama \n### ahmed',
groups: undefined ],
[ '### ahmed',
'###',
'ahmed',
index: 11,
input: '### osama \n### ahmed',
groups: undefined ] ]
code: [..."### osama".matchAll(/^s*(#+)/gm (w+))][0]
[ '### osama',
'###',
'osama',
index: 0,
input: '### osama',
groups: undefined ]
code: [..."### osama".matchAll(/^s*(#+)/gm (w+))][0][1] | [2]
###
osama
code: [..."*osama*".matchAll(/([*_])(w+)([*_])/gm)]
[ [ '*osama*',
'*',
'osama',
'*',
index: 0,
input: '*osama*',
groups: undefined ] ]
code: [..."**osama**".matchAll(/([*_])(w+)([*_])/gm)]
[ [ '**osama**',
'**',
'osama',
'**',
index: 0,
input: '**osama**',
groups: undefined ] ]
code: ..."".matchAll(/![([^]]+)](([^)]+))/gm)]
[ [ '',
'alt-text',
'example.jpg',
index: 0,
input: '',
groups: undefined ] ]
code: ..."[link text](www.youtube.com)".matchAll(/[([^]]+)](([w.]+))/gm)
[ '[link text](www.youtube.com)',
'link text',
'www.youtube.com',
index: 0,
input: '[link text](www.youtube.com)',
groups: undefined ]
code: [..."> to be or not to be".matchAll(/^s*> ([^
]+)/gm)]
[ [ '> to be or not to be',
'to be or not to be',
index: 0,
input: '> to be or not to be',
groups: undefined ] ]
*/
/* 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 OPR/131.0.0.0
Challenge Information:
Build a Markdown to HTML Converter - Build a Markdown to HTML Converter