From e8c096108beabc24b82008b0e6f8b389ef908943 Mon Sep 17 00:00:00 2001 From: Nikhil Nawgiri Date: Tue, 27 Aug 2024 16:50:14 +0200 Subject: [PATCH] Create helper for converting md to html --- src/markdownToHtmlHelper.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/markdownToHtmlHelper.ts diff --git a/src/markdownToHtmlHelper.ts b/src/markdownToHtmlHelper.ts new file mode 100644 index 0000000..ab5edb9 --- /dev/null +++ b/src/markdownToHtmlHelper.ts @@ -0,0 +1,13 @@ +import { Remarkable } from "remarkable"; + +const replaceNewlinesOutsideOfCodeBlocks = (html: string) => ( + // Replace all \n with
inside the matched

...

block + html.replace(/(

[\s\S]*?)\n([\s\S]*?<\/p>)/g, (match) => ( + match.replace(/\n(?!<\/p>)/g, '
') + )) +); + +export const convertMarkdownToHtml = (markdown: string) => { + const md = new Remarkable(); + return replaceNewlinesOutsideOfCodeBlocks(md.render(markdown)); +};