From 9b87a5d420fe4794e02a576f2a168d1b0c187f53 Mon Sep 17 00:00:00 2001 From: Wes Copeland Date: Sun, 19 Jan 2025 15:01:25 -0500 Subject: [PATCH] fix(forums): normalize newlines from synthetic events --- .../ShortcodeCode/ShortcodeCode.tsx | 4 +- .../utils/preProcessShortcodesInBody.test.ts | 56 +++++++++++++++++++ .../utils/preProcessShortcodesInBody.ts | 6 +- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/resources/js/features/forums/components/ShortcodeRenderer/ShortcodeCode/ShortcodeCode.tsx b/resources/js/features/forums/components/ShortcodeRenderer/ShortcodeCode/ShortcodeCode.tsx index 55856bed36..96f9234d6a 100644 --- a/resources/js/features/forums/components/ShortcodeRenderer/ShortcodeCode/ShortcodeCode.tsx +++ b/resources/js/features/forums/components/ShortcodeRenderer/ShortcodeCode/ShortcodeCode.tsx @@ -15,8 +15,8 @@ export const ShortcodeCode: FC = ({ children }) => { return true; }); - return {processedChildren}; + return {processedChildren}; } - return {children}; + return {children}; }; diff --git a/resources/js/features/forums/utils/preProcessShortcodesInBody.test.ts b/resources/js/features/forums/utils/preProcessShortcodesInBody.test.ts index 06de3421b6..ce78d6c744 100644 --- a/resources/js/features/forums/utils/preProcessShortcodesInBody.test.ts +++ b/resources/js/features/forums/utils/preProcessShortcodesInBody.test.ts @@ -83,4 +83,60 @@ describe('Util: preProcessShortcodesInBody', () => { // ASSERT expect(result).toEqual(input); }); + + it('normalizes escaped newlines to actual newlines', () => { + // ARRANGE + const input = 'first line↵\nsecond line'; + + // ACT + const result = preProcessShortcodesInBody(input); + + // ASSERT + expect(result).toEqual('first line\nsecond line'); + }); + + it('handles mixed escaped and actual newlines', () => { + // ARRANGE + const input = 'first line↵\nsecond line\nthird line↵\nfourth line'; + + // ACT + const result = preProcessShortcodesInBody(input); + + // ASSERT + expect(result).toEqual('first line\nsecond line\nthird line\nfourth line'); + }); + + it('normalizes line endings when content includes shortcodes', () => { + // ARRANGE + const input = + 'Check out https://retroachievements.org/user/ScottAdams↵\nAnd also↵\nhttps://retroachievements.org/game/1234'; + + // ACT + const result = preProcessShortcodesInBody(input); + + // ASSERT + expect(result).toEqual('Check out [user=ScottAdams]\nAnd also\n[game=1234]'); + }); + + it('handles carriage returns and different line ending styles', () => { + // ARRANGE + const input = 'line1\r\nline2\rline3\nline4'; + + // ACT + const result = preProcessShortcodesInBody(input); + + // ASSERT + expect(result).toEqual('line1\nline2\nline3\nline4'); + }); + + it('preserves whitespace while normalizing line endings', () => { + // ARRANGE + const input = ' indented↵\n still indented ↵\n\n more space '; + + // ACT + const result = preProcessShortcodesInBody(input); + + // ASSERT + expect(result).toEqual(' indented\n still indented \n\n more space '); + }); }); diff --git a/resources/js/features/forums/utils/preProcessShortcodesInBody.ts b/resources/js/features/forums/utils/preProcessShortcodesInBody.ts index e0524cc73a..af662029e9 100644 --- a/resources/js/features/forums/utils/preProcessShortcodesInBody.ts +++ b/resources/js/features/forums/utils/preProcessShortcodesInBody.ts @@ -30,7 +30,11 @@ const createPatterns = (type: string) => [ ]; export function preProcessShortcodesInBody(body: string): string { - let result = body; + // First, normalize any escaped newlines back to actual newlines. + let result = body.replace(/↵\n/g, '\n'); + + // Then, normalize any remaining line endings. + result = result.replace(/\r\n|\r|\n/g, '\n'); for (const { type, shortcode } of shortcodeTypes) { const patterns = createPatterns(type);