Tell us what’s happening:
Failing test 38 but my output seems the same
- When the value of
#markdown-input
is
followed by
on a new line,convertMarkdown()
should return<img alt="alt-text" src="image-source"><img alt="alt-text-2" src="image-source-2">
.
Here’s my result returned by the function compared to the desired output:
result <img alt="alt-text" src="image-source"><img alt="alt-text-2" src="image-source-2">
<img alt="alt-text" src="image-source"><img alt="alt-text-2" src="image-source-2">
Not sure what to check next.
Your code so far
<!-- file: index.html -->
/* file: script.js */
let input = document.getElementById("markdown-input");
let raw = document.getElementById("html-output");
let preview = document.getElementById("preview");
let regex = {
strong: /(?:\*{2}|_{2})(.*)(?:\*{2}|_{2})$/,
em: /(?<!\*)(?:\*|_)(?!\*)(.*)(?<!\*)(?:\*|_)/,
h1: /^# (.*)$/,
h2: /^## ([\w\d\s]*)$/,
h3: /^### ([\w\d\s]*)$/,
blockquote: /^> (.*)$/,
img: /^!\[([\w\d\s-]*)\]\(([\w\d\s-]*)\)$/,
a: /^\[([\w\d\s-]*)\]\(([\w\d\s-]*)\)$/,
};
function convertMarkdown() {
console.log("New text")
preview.innerHTML = "";
let result = "";
let lines = input.value.split("\n");
let element;
for (let line of lines) {
console.log("current line ", line);
for (let key in regex) {
if (line.match(regex[key])) {
console.log("match", key, line.match(regex[key])[0]);
if (key == "em" |
key == "strong") {
line = line.replace(line.match(regex[key])[0], `<${key}>${line.match(regex[key])[1]}</${key}>`)
console.log(line)
}
if (key == "blockquote" |
key == "h1" |
key == "h2" |
key == "h3" ) {
element = document.createElement(key)
element.innerHTML = line.match(regex[key])[1];
}
if (key == "img") {
element = document.createElement(key);
element.alt = line.match(regex[key])[1];
element.src = line.match(regex[key])[2];
}
if (key == "a") {
element = document.createElement(key);
element.innerText = line.match(regex[key])[1];
element.href = line.match(regex[key])[2];
}
}
}
if (element) {
console.log("append", element.innerText)
preview.appendChild(element);
} else {
console.log("+=", line)
preview.innerHTML += line;
}
}
let text = preview.innerHTML
.replace(/</g, '<')
.replace(/>/g, '>')
result += text;
console.log("result", result);
return result;
}
convertMarkdown();
input.addEventListener("input", () => {
raw.textContent = convertMarkdown();
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Challenge Information:
Build a Markdown to HTML Converter - Build a Markdown to HTML Converter