src/markdown.js
function markdownToHtml( text ) {
    // note: in JS $ matches end of line as well as end of string, and ^ both beginning of line and string
    const html = text
        // html encoding of < because libXMLJs Element.text() converts html entities
        .replace( /</gm, '&lt;' )
        // html encoding of < because libXMLJs Element.text() converts html entities
        .replace( />/gm, '&gt;' )
        // span
        .replace( /&lt;\s?span([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?span\s?&gt;/gm, _createSpan )
        // sup
        .replace( /&lt;\s?sup([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sup\s?&gt;/gm, _createSup )
        // sub
        .replace( /&lt;\s?sub([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sub\s?&gt;/gm, _createSub )
        // "\" will be used as escape character for *, _
        .replace( /&/gm, '&amp;' )
        .replace( /\\\\/gm, '&92;' )
        .replace( /\\\*/gm, '&42;' )
        .replace( /\\_/gm, '&95;' )
        .replace( /\\#/gm, '&35;' )
        // strong
        .replace( /__(.*?)__/gm, '<strong>$1</strong>' )
        .replace( /\*\*(.*?)\*\*/gm, '<strong>$1</strong>' )
        // emphasis
        .replace( /_([^\s][^_\n]*)_/gm, '<em>$1</em>' )
        .replace( /\*([^\s][^*\n]*)\*/gm, '<em>$1</em>' )
        // links
        .replace( /\[([^\]]*)\]\(([^)]+)\)/gm, '<a href="$2" target="_blank">$1</a>' )
        // headers
        .replace( /^\s*(#{1,6})\s?([^#][^\n]*)(\n|$)/gm, _createHeader )
        // unordered lists 
        .replace( /(\n(\*|\+|-) (.*))+$/gm, _createUnorderedList )
        // ordered lists 
        .replace( /(\n([0-9]+\.) (.*))+$/gm, _createOrderedList )
        // reverting escape of special characters
        .replace( /&35;/gm, '#' )
        .replace( /&95;/gm, '_' )
        .replace( /&92;/gm, '\\' )
        .replace( /&42;/gm, '*' )
        .replace( /&amp;/gm, '&' )
        // paragraphs
        .replace( /([^\n]+)\n{2,}/gm, _createParagraph )
        // any remaining newline characters
        .replace( /([^\n]+)\n/gm, '$1<br>' );

    return html;
}
function markdownToHtml( text ) {
    // note: in JS $ matches end of line as well as end of string, and ^ both beginning of line and string
    const html = text
        // html encoding of < because libXMLJs Element.text() converts html entities
        .replace( /</gm, '&lt;' )
        // html encoding of < because libXMLJs Element.text() converts html entities
        .replace( />/gm, '&gt;' )
        // span
        .replace( /&lt;\s?span([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?span\s?&gt;/gm, _createSpan )
        // sup
        .replace( /&lt;\s?sup([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sup\s?&gt;/gm, _createSup )
        // sub
        .replace( /&lt;\s?sub([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sub\s?&gt;/gm, _createSub )
        // "\" will be used as escape character for *, _
        .replace( /&/gm, '&amp;' )
        .replace( /\\\\/gm, '&92;' )
        .replace( /\\\*/gm, '&42;' )
        .replace( /\\_/gm, '&95;' )
        .replace( /\\#/gm, '&35;' )
        // strong
        .replace( /__(.*?)__/gm, '<strong>$1</strong>' )
        .replace( /\*\*(.*?)\*\*/gm, '<strong>$1</strong>' )
        // emphasis
        .replace( /_([^\s][^_\n]*)_/gm, '<em>$1</em>' )
        .replace( /\*([^\s][^*\n]*)\*/gm, '<em>$1</em>' )
        // links
        .replace( /\[([^\]]*)\]\(([^)]+)\)/gm, '<a href="$2" target="_blank">$1</a>' )
        // headers
        .replace( /^\s*(#{1,6})\s?([^#][^\n]*)(\n|$)/gm, _createHeader )
        // unordered lists 
        .replace( /(\n(\*|\+|-) (.*))+$/gm, _createUnorderedList )
        // ordered lists 
        .replace( /(\n([0-9]+\.) (.*))+$/gm, _createOrderedList )
        // reverting escape of special characters
        .replace( /&35;/gm, '#' )
        .replace( /&95;/gm, '_' )
        .replace( /&92;/gm, '\\' )
        .replace( /&42;/gm, '*' )
        .replace( /&amp;/gm, '&' )
        // paragraphs
        .replace( /([^\n]+)\n{2,}/gm, _createParagraph )
        // any remaining newline characters
        .replace( /([^\n]+)\n/gm, '$1<br>' );

    return html;
}
function markdownToHtml ( text ) { // note: in JS $ matches end of line as well as end of string, and ^ both beginning of line and string const html = text // html encoding of < because libXMLJs Element.text() converts html entities . replace ( / < / gm , '&lt;' ) // html encoding of < because libXMLJs Element.text() converts html entities . replace ( / > / gm , '&gt;' ) // span . replace ( / &lt;\s?span([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?span\s?&gt; / gm , _createSpan ) // sup . replace ( / &lt;\s?sup([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sup\s?&gt; / gm , _createSup ) // sub . replace ( / &lt;\s?sub([^/\n]*)&gt;((?:(?!&lt;\/).)+)&lt;\/\s?sub\s?&gt; / gm , _createSub ) // "\" will be used as escape character for *, _ . replace ( / & / gm , '&amp;' ) . replace ( / \\\\ / gm , '&92;' ) . replace ( / \\\* / gm , '&42;' ) . replace ( / \\_ / gm , '&95;' ) . replace ( / \\# / gm , '&35;' ) // strong . replace ( / __(.*?)__ / gm , '<strong>$1</strong>' ) . replace ( / \*\*(.*?)\*\* / gm , '<strong>$1</strong>' ) // emphasis . replace ( / _([^\s][^_\n]*)_ / gm , '<em>$1</em>' ) . replace ( / \*([^\s][^*\n]*)\* / gm , '<em>$1</em>' ) // links . replace ( / \[([^\]]*)\]\(([^)]+)\) / gm , '<a href="$2" target="_blank">$1</a>' ) // headers . replace ( / ^\s*(#{1,6})\s?([^#][^\n]*)(\n|$) / gm , _createHeader ) // unordered lists  . replace ( / (\n(\*|\+|-) (.*))+$ / gm , _createUnorderedList ) // ordered lists  . replace ( / (\n([0-9]+\.) (.*))+$ / gm , _createOrderedList ) // reverting escape of special characters . replace ( / &35; / gm , '#' ) . replace ( / &95; / gm , '_' ) . replace ( / &92; / gm , '\\' ) . replace ( / &42; / gm , '*' ) . replace ( / &amp; / gm , '&' ) // paragraphs . replace ( / ([^\n]+)\n{2,} / gm , _createParagraph ) // any remaining newline characters . replace ( / ([^\n]+)\n / gm , '$1<br>' ) ; return html ; }
Transforms XForm label and hint textnode content with a subset of Markdown into HTML

Supported:
- _, __, *, **, [](), #, ##, ###, ####, #####,
- span tags and html-encoded span tags,
- single-level unordered markdown lists and single-level ordered markdown lists
- newline characters

Also HTML encodes any unsupported HTML tags for safe use inside web-based clients

@param  {string} text text content of a textnode
@return {string}      transformed text content of a textnode
Transforms XForm label and hint textnode content with a subset of Markdown into HTML