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

Make sure there are newlines between lines when copying code snippets #282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mimbrown
Copy link

@mimbrown mimbrown commented May 3, 2024

I noticed while using the sveltekit site that copying the codeblocks didn't include a lot of necessary newline characters. This is a simple fix that seems to fix the problem I was facing, but it might need to be more sophisticated.

Copy link

changeset-bot bot commented May 3, 2024

⚠️ No Changeset found

Latest commit: fcc0876

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@PuruVJ
Copy link
Collaborator

PuruVJ commented May 10, 2024

Can you specify the exact code snippets which give the problem? I tried on my end and this fix just adds 2 lines more, so there's maybe a more granular solution

@mimbrown
Copy link
Author

Sure, try copying the first snippet (and probably the others too) on the service worker sveltekit page. Could be a problem with when the HTML is generated instead of when it's traversed, I don't know.

@mimbrown
Copy link
Author

I get:

if ('serviceWorker' in navigator) {	addEventListener('load', function () {		navigator.serviceWorker.register('./path/to/service-worker.js');	});}

@dummdidumm
Copy link
Member

From a quick test it seems that Svelte files are fine, but JS files are mostly broken.

Comment on lines 22 to +35
for (const node of block.querySelector('code')?.childNodes ?? []) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (!(/** @type {HTMLElement} */ (node).classList.contains('deleted'))) {
code += node.textContent;
code += node.textContent + '\n';
}
} else {
code += node.textContent;
code += node.textContent + '\n';
}
}

if (!code) continue;

code = code.slice(0, -1);

Copy link

Choose a reason for hiding this comment

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

I think you can avoid the \n's and use innerText instead:

Suggested change
for (const node of block.querySelector('code')?.childNodes ?? []) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (!(/** @type {HTMLElement} */ (node).classList.contains('deleted'))) {
code += node.textContent;
code += node.textContent + '\n';
}
} else {
code += node.textContent;
code += node.textContent + '\n';
}
}
if (!code) continue;
code = code.slice(0, -1);
for (const node of block.querySelector('code')?.childNodes ?? []) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (!(/** @type {HTMLElement} */ (node).classList.contains('deleted'))) {
code += node.innerText;
}
} else {
code += node.innerText;
}
}
if (!code) continue;

Here's the blame when it got changed from innerText to textContent: 4898bfd

I have a similar behavior on another project of mine and innerText preserves the new lines and spaces:
https://github.com/fmaclen/hollama/blob/main/src/routes/%5Bid%5D/Article.svelte#L41

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.

4 participants