简体   繁体   中英

Change markdown Bullets to html in Javascript

At the moment I have a function

export function markdownToHtml(mdText) {
  const toHTML = mdText
    .replace(/^### (.*$)/gim, "<h3>$1</h3>") // h3 tag
    .replace(/^## (.*$)/gim, "<h2>$1</h2>") // h2 tag
    .replace(/^# (.*$)/gim, "<h1>$1</h1>") // h1 tag
    .replace(/\*\*(.*)\*\*/gim, "<b>$1</b>") // bold text
    .replace(/\*(.*)\*/gim, "<i>$1</i>") // italic text
    .replace(/\[(.*?)\]\((.*?)\)/gim, "<a href='$2'>$1</a>") // link
  return toHTML.trim() // using trim method to remove whitespace
}

where I am parsing some of the Markdown styles to HTML but I'm missing the regex to parse bullet points.

any Idea? (not using a dependecy is better)

Something like this?

It does not wrap in UL

 function markdownToHtml(mdText) { const toHTML = mdText.split("\\n").map(text => text .trim() // using trim method to remove whitespace .replace(/^### (.*$)/i, "<h3>$1</h3>") // h3 tag .replace(/^## (.*$)/i, "<h2>$1</h2>") // h2 tag .replace(/^# (.*$)/i, "<h1>$1</h1>") // h1 tag .replace(/^\\* (.*$)/i, "<li>$1</li>") // <li> tag .replace(/\\*\\*(.*)\\*\\*/i, "<b>$1</b>") // bold text .replace(/\\*(.*)\\*/i, "<i>$1</i>") // italic text .replace(/\\*(.*)\\*/i, "<i>$1</i>") // italic text .replace(/\\[(.*?)\\]\\((.*?)\\)/i, "<a href='$2'>$1</a>")) // link return toHTML.join("\\n") } console.log(markdownToHtml(`### Title\\n* *Bold bullet*`))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM