Create helper for converting md to html

This commit is contained in:
Nikhil Nawgiri 2024-08-27 16:50:14 +02:00
parent 035dea3795
commit e8c096108b

View file

@ -0,0 +1,13 @@
import { Remarkable } from "remarkable";
const replaceNewlinesOutsideOfCodeBlocks = (html: string) => (
// Replace all \n with <br> inside the matched <p>...</p> block
html.replace(/(<p>[\s\S]*?)\n([\s\S]*?<\/p>)/g, (match) => (
match.replace(/\n(?!<\/p>)/g, '<br>')
))
);
export const convertMarkdownToHtml = (markdown: string) => {
const md = new Remarkable();
return replaceNewlinesOutsideOfCodeBlocks(md.render(markdown));
};