Markdown to LaTeX Simplified implementation to convert Markdown to LaTeX spring board for a more complex system Headers Blockquotes Ordered lists 1 Unordered lists Paragraphs Links Images Inline em emphasis Inline strong emphasis script matches at least one character matches any number including 0 of characters The indicates a lazy expression so it will match as few characters as possible delimiter start of line end of line zero or more of any character except newline m enables multi line mode Examples Pattern ab will return a match for capture group with result of ab While the pattern ab will not match and not returning anything function md2tex s heading h1 h2 h3 s s replace n n g section 2 n s s replace n n g subsection 2 n s s replace n n g subsubsection 2 n s s replace n n g subsubsubsection 2 n block quotes s s replace n r n n n g begin quote 2 end quote n n bold s s replace g textbf 1 emph s s replace g textit 1 block listing s s replace r n g begin lstlisting 1 end lstlisting inline listing s s replace g lstinline 1 bullets s s replace n gm bullet 2 n return s end md2tex var test Title Markdown is sweet Do you agree Subsection test var 2 This isn t always so easy block of code block of code2 A quote cats are like bats one two thre3 console log in test console log to md2tex test script elaboration on the basic version so code blocks are stripped from the processing and reinserted at the end since you don t want your code blocks to have any changes script type md id mymd A Title Once upon a time A block of code A title ignored in a code block Frogs are not always green Some can be blue Another code block ignore bold syntax and other syntax types from MD script script Here is an example what s happending just the code part matches all removes them then at the end puts them back with the extra sprinkles around let s document getElementById mymd innerText console log original s const regexp r n g const array s matchAll regexp s s replace r n g CODE do other syntax processing know that the code blocks are safely stored console log stripped s Restore code blocks with added wrapping for let i 0 i array length i s s replace CODE code array i code console log restored s script script matches at least one character matches any number including 0 of characters The indicates a lazy expression so it will match as few characters as possible delimiter start of line end of line zero or more of any character except newline m enables multi line mode Examples Pattern ab will return a match for capture group with result of ab While the pattern ab will not match and not returning anything function md2tex s remove code block listing save them for later const regexp r n g const array s matchAll regexp s s replace r n g CODE heading h1 h2 h3 s s replace n n g section 2 n s s replace n n g subsection 2 n s s replace n n g subsubsection 2 n s s replace n n g subsubsubsection 2 n block quotes s s replace n r n n n g begin quote 2 end quote n n bold s s replace g textbf 1 emph s s replace g textit 1 inline listing s s replace g lstinline 1 bullets s s replace n gm bullet 2 n Restore code blocks with added wrapping for let i 0 i array length i s s replace CODE begin lstlisting array i end lstlisting end for i return s end md2tex var test Title Markdown is sweet Do you agree Subsection test var 2 This isn t always so easy block of code This hash shouldn t be changed block of code2 A quote cats are like bats one two thre3 console log in test console log to md2tex test script
em back with the extra sprinkles around let s document getElementById mymd innerText console log original s const regexp r n g const array s matchAll regexp s s replace r n g CODE do other syntax processing know that the code blocks are safely stored console log stripped s Restore code blocks with added wrapping for let i 0 i array length i s s replace CODE code array i code console log restored s script script matches at least one character matches any number including 0 of characters The indicates a lazy expression so it will match as few characters as possible delimiter start of line end of line zero or more of any character except newline m enables multi line mode Examples Pattern ab will return a match for capture group with result of ab While the pattern ab will not match and not returning anything function md2tex s remove code block listing save them for later const regexp r n g const array s matchAll regexp s s replace r n g CODE heading h1 h2 h3 s s replace n n g section 2 n s s replace n n g subsection 2 n s s replace n n g subsubsection 2 n s s replace n n g subsubsubsection 2 n block quotes s s replace n r n n n g begin quote 2 end quote n n bold s s replace g textbf 1 emph s s replace g textit 1 inline listing s s replace g lstinline 1 bullets s s replace n gm bullet 2 n Restore code blocks with added wrapping for let i 0 i array length i s s replace CODE begin lstlisting array i end lstlisting end for i return s end md2tex var test Title Markdown is sweet Do you agree Subsection test var 2 This isn t always so easy block of code This hash shouldn t be changed block of code2 A quote cats are like bats one two thre3 console log in test console log to md2tex test script