Skip to content

“â# Fix: Windows CRLF line ending compatibility for filters#1655

Closed
achuanya wants to merge 0 commit intomaizzle:masterfrom
achuanya:pr
Closed

“â# Fix: Windows CRLF line ending compatibility for filters#1655
achuanya wants to merge 0 commit intomaizzle:masterfrom
achuanya:pr

Conversation

@achuanya
Copy link
Contributor

Fix: Windows CRLF line ending compatibility for filters

Root Cause

The regex /\n/g only matched Unix-style line breaks, leaving \r characters which corrupted the output.

Solution

Updated regex to /\r?\n/g to support both LF and CRLF."

Problem

The filters.test.js test was failing on Windows with this error:

expected '<!-- Append -->\r\n<div>testing appen…' to be '<!-- Append -->\r\n<div>testing appen…'

The diff showed corrupted output for newline-to-br and strip-newlines filters:

- <div><br>  test<br>  test<br></div>
+ <br></div>

- <div>  test  test</div>
+ </div>

Root Cause

In src/transformers/filters/defaultFilters.js, two filter functions only matched Unix-style line endings (\n):

// Line 33
const newlineToBr = content => content.replace(/\n/g, '<br>')

// Line 79
const stripNewlines = content => content.replace(/\n/g, '')

Windows uses \r\n (CRLF) for line breaks. The regex /\n/g only replaced the \n part, leaving the \r (carriage return) character in the output.

The \r character moves the cursor back to the beginning of the line, causing the output to appear truncated/overwritten when displayed or compared.

Solution

Changed the regex from /\n/g to /\r?\n/g to handle both Windows (CRLF) and Unix (LF) line endings:

// Line 33
const newlineToBr = content => content.replace(/\r?\n/g, '<br>')

// Line 79
const stripNewlines = content => content.replace(/\r?\n/g, '')

Files Changed

  • src/transformers/filters/defaultFilters.js

Testing

All 119 tests pass after this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant