Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forums): normalize newlines from synthetic events #3083

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const ShortcodeCode: FC<ShortcodeCodeProps> = ({ children }) => {
return true;
});

return <span className="codetags font-mono">{processedChildren}</span>;
return <span className="codetags mb-3 font-mono">{processedChildren}</span>;
}

return <span className="codetags">{children}</span>;
return <span className="codetags mb-3 font-mono">{children}</span>;
Copy link
Member

@Jamiras Jamiras Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've applied the mb-3 to the #3074 PR (for both this and ShortcodeQuotes). You should remove it from here to eliminate the merge conflict (I've touched this code for trimming the leading <br>).

};
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A unicode character for escaping a newline? I've never seen that before.

Should you use an escape sequence for it (\u21B5) to prevent possible encoding issues?


// 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);
Expand Down