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: add line break to end of each code block line #62

Merged
merged 4 commits into from
Oct 19, 2023
Merged

fix: add line break to end of each code block line #62

merged 4 commits into from
Oct 19, 2023

Conversation

nobkd
Copy link
Contributor

@nobkd nobkd commented Oct 8, 2023

Similar to nuxt/content#2008 to fix nuxt/content#1883

Current solution not working.

Not even sure, why this shouldn't work. If you have any idea, please let me know!


Do I have to add it as text node of real element? 🤔

Would have to traverse hast to find the last text node in line and add \n to its value.
For empty lines, will probably have to add new span?

(Maybe applying \200b to non-empty lines because of adding \n to empty ones?)

@farnabaz
Copy link
Collaborator

farnabaz commented Oct 10, 2023

Hey @nobkd
It is strange because for me it seems fine in Firefox. Just checked https://content.nuxt.com/get-started/installation#add-to-a-project

@nobkd
Copy link
Contributor Author

nobkd commented Oct 10, 2023

I selected multiple lines of code in the code block by hand and copied them. Then I paste them in some editor.
Result is always, that the complete code is in one line.

I checked Firefox on Linux (Firefox 118.0.1) and Windows (Firefox 118.0.2), and it happens on both.

@nobkd
Copy link
Contributor Author

nobkd commented Oct 14, 2023

I've checked again, and the hast really still contains the \ns at the end of each line of a code block.
I'll check the code again, because it still not works in FF for me, even after purging cache or switching devices...


EDIT:

I understand...

Tags that only contain newlines are removed:

// Kepp none new line text nodes
if (node.type === 'text' && node.value !== '\n') {
return {
type: 'text',
value: node.value
}
}

It's alright to do this, but not when they are within a code block.
Also, the newlines currently aren't inside the line spans.
Therefore, removing the newline check adds a newline in between the lines spans, which then looks like 2 lines.

@nobkd
Copy link
Contributor Author

nobkd commented Oct 15, 2023

The version, I just pushed, only works for lines that contain code / text.
Currently, empty lines will be dropped when copying by hand in Firefox.

Not sure how to handle that while still keeping general newline stripping...


Edit:

I've played around a bit:

  • I tried adding a keep attribute to Text node type, but it always gets stripped from the node attributes.
  • A possible and working, but not so great way, would be to add a string that never appears in code, e.g. ---newline-node--- and replace it with a newline \n inside the compiler. That can break, if the string is really used by a user

Do you have better ideas?

@nobkd
Copy link
Contributor Author

nobkd commented Oct 18, 2023

@farnabaz do you have thoughts about how to properly solve this?

@farnabaz farnabaz marked this pull request as ready for review October 19, 2023 12:38
Copy link
Collaborator

@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 @nobkd


I've checked in Firefox and copy seems fine now. As for empty lines just added an empty line placeholder which we keep it in compiler

@farnabaz farnabaz merged commit 3c395bb into nuxt-modules:main Oct 19, 2023
2 checks passed
@nobkd nobkd deleted the fix/line-breaks branch October 19, 2023 13:54
@nobkd
Copy link
Contributor Author

nobkd commented Oct 19, 2023

I don't think, that newlines should be added to inline codes

The code where the newline is added to a text node seems to need a check, if the code is a code block or an inline code.

(Also not sure, if an empty line placeholder gets added to empty inline codes.)


Maybe it does not even change anything, when copying, as the newline is not in a pre. But I would have to check.


EDIT:

It does not make sense, that the inline code contains line breaks too, but I've checked, and it does not seem to influence the copied text, as inline codes aren't inside pres.

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.

Copying code blocks from markdown removes new lines on Firefox
2 participants