From 21f75c1db1c340bceedf2a224d43b5a59a6de60f Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Wed, 28 Aug 2024 21:51:03 +0200 Subject: [PATCH] don't help user format markdown if content already contains correct empty line syntax --- src/lib/utils/post-utils.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib/utils/post-utils.ts b/src/lib/utils/post-utils.ts index e0d008ad..01dcee51 100644 --- a/src/lib/utils/post-utils.ts +++ b/src/lib/utils/post-utils.ts @@ -24,10 +24,16 @@ export function getTextColorForBackground(rgb: string): string { export const formatMarkdown = (content: string): string => { let md = content; if (md) { - // Replace single newline with "/n /n" if followed by a newline - md = md.replace(/\n(?=\n)/g, '\n \n'); - // Replace single newline with double newline if between two characters - md = md.replace(/\n(?=\S)/g, '\n\n'); + // Check if the content already contains valid Markdown patterns + const alreadyFormattedPattern = /\n \n/; + + // help the user by formatting the content if it's not already formatted + if (!alreadyFormattedPattern.test(md)) { + // Replace single newline with "/n /n" if followed by a newline + md = md.replace(/\n(?=\n)/g, '\n \n'); + // Replace single newline with double newline if between two characters + md = md.replace(/\n(?=\S)/g, '\n\n'); + } } return md; };