-
Notifications
You must be signed in to change notification settings - Fork 63
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
base: master
Are you sure you want to change the base?
Conversation
|
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 |
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. |
I get: if ('serviceWorker' in navigator) { addEventListener('load', function () { navigator.serviceWorker.register('./path/to/service-worker.js'); });} |
From a quick test it seems that Svelte files are fine, but JS files are mostly broken. |
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); | ||
|
There was a problem hiding this comment.
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:
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
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.