Im creating my own version of a MarkDown render. My goal is to read a string or file line by line then return the MD rendered as HTML. The problem im facing is my return and line by line functionaility is not-returning what I want and I dont know why. Ive tried debugging but everything seemed fine. Ive also tried chaning my for loop but still no luck.
//function used to render MD for the user
function render(md){
let code = ""; //For functions return
let mdLines = md.split('\n');
for(let i = 0; i < mdLines.length; i++){
//Statements to see what kind of MD header a line is.
//I only have these statments as I just started this and am still figuring how I want to approach.
if(mdLines[i].slice(0, 1) == "#"){
code += code.concat("<h1>" + mdLines[i].replace("#", "") + "</h1>")
}
else if(mdLines[i].slice(0, 2) == "##"){
code += code.concat("<h2>" + mdLines[i].replace("##", "") + "</h2>")
}
else if(mdLines[i].slice(0, 3) == "###"){
code += code.concat("<h3>" + mdLines[i].replace("###", "") + "</h3>")
}
else if(mdLines[i].slice(0, 4) == "####"){
code += code.concat("<h4>" + mdLines[i].replace("#", "") + "</h4>")
}
else if(mdLines[i].slice(0, 5) == "#####"){
code += code.concat("<h5>" + mdLines[i].replace("#", "") + "</h5>")
}
else if(mdLines[i].slice(0, 6) == "######"){
code += code.concat("<h6>" + mdLines[i].replace("######", "") + "</h6>")
}
};
return code;
}
//editor
//This is what the user's .js file would be.
//I have it set up like this for testing.
let text1 = "## he#llo \n there \n# yooo"
let text2 = "# he#llo \n there \n## yooo"
console.log(render(text1));
console.log(render(text2));
text1 returns <h1># he#llo </h1><h1># he#llo </h1><h2> he#llo </h2>
text2 reutrns <h1> he#llo </h1>
text1 should return <h2>he#llo</h2> there <h1> yooo </h1>
text2 should return <h1> he#llo </h1> there <h2> yooo </h2>
If someone could help me get the proper returns and some potentially reusable code for this issue it would be greatly appreciated.
Ive also looked up the issue with some select terms but it seems no one has documented my very specific issue.
Follow Up
I originally had it as multiple if statements but changed it to else if to not make it a duplicate.
I would also like to shorten this code and not use multiple else if statements if possible. My theories would be regEX or case
NOTE if you were looking to render MD without coding your own render there are many librays to help you do that.
Renders: https://byby.dev/js-markdown-libs
Here's a better and more general way to handle this. In addition, it won't strip out "#" that are in the middle of a string, as yours would. This relies on the fact that a ### header must be followed by a space.
Output:
Followup
Here's what you apparently MEANT to test with:
And here's the output: