Skip to content

fix: do not escape newlines #3320

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

Merged
merged 9 commits into from
May 14, 2025
Merged

Conversation

dword-design
Copy link
Contributor

@dword-design dword-design commented Apr 21, 2025

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)

πŸ“š Description

Currently newlines are escaped when putting strings as fields into @nuxt/content. That means that the newlines that are returned from e.g. queryCollection aren't real newlines but slashes followed by an n char. This is clearly visible when converting the field value to JSON, then the output looks something like line1\\nline2 instead of line1\nline2. Concretely this breaks my nuxt-content-body-html module that generates a html for @nuxt/content posts. In the final result e.g. in code blocks, there will just be \n strings instead of real line breaks.

I think there are two issues:

  1. The newlines are escaped when generating the inserts
  2. Actual newlines are used to split the decompressed dump on client side

I've fixed them by keeping the newlines in the queries and using JSON.stringify and JSON.parse to communicate the dump between server and client instead of spliting by \n.

I haven't fixed the tests yet since I don't know yet if the PR gets merged.

Reproduction

https://codesandbox.io/p/devbox/j4yj8k

Copy link

pkg-pr-new bot commented Apr 21, 2025

npm i https://pkg.pr.new/@nuxt/content@3320

commit: 7bc0f4a

@dword-design dword-design changed the title do not escape newlines fix: do not escape newlines Apr 21, 2025
@farnabaz
Copy link
Member

What issue does this PR aim to fix? Could you provide a reproduction sample?

@dword-design
Copy link
Contributor Author

@farnabaz I added more details and a reproduction link.

@luksak
Copy link

luksak commented May 6, 2025

I just encountered this issue myself. The PR fixes the bug for me. Thanks a lot!

@farnabaz
Copy link
Member

I see
Unfortunately, not all database providers support multi-line queries. That's what module replaces new lines with \\n.
The issue here is not about encoding the newlines; the issue is that the module does not decode the column value when retrieving it.

What we can do is to change the refineContentFields function in runtime/internal/collections.ts, to decode the \\n back to \n.

@dword-design
Copy link
Contributor Author

Which don't? I tried out the ones available.

@farnabaz
Copy link
Member

@dword-design You were right, double checked with databases, and all seems fine.

Copy link
Member

@farnabaz farnabaz left a comment

Choose a reason for hiding this comment

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

Thanks

@farnabaz farnabaz merged commit eb60ecb into nuxt:main May 14, 2025
2 checks passed
@dword-design
Copy link
Contributor Author

Nice. Possible to have a release? πŸ˜€

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.

3 participants