Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Missing whitespace when copying codeblock contents #29

Open
alreadypro opened this issue Mar 16, 2022 · 6 comments
Open

Missing whitespace when copying codeblock contents #29

alreadypro opened this issue Mar 16, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@alreadypro
Copy link

alreadypro commented Mar 16, 2022

Description

New lines & (some) tabs are not copied to clipboard when a codeblock's content is copied.

This could be fixed multiple ways, but a favorable solution would be to add a "copy code" button on the codeblock.

Reproduction Steps

  1. Select all of the code in a codeblock
  2. Ctrl + C to copy it
  3. Ctrl + V in your favorite editor
  4. ???
  5. where are my newlines & (some) tabs?????
  • Plugin Name: BetterCodeblocks
  • Discord Release: Stable
  • Platform: Windows 11
@alreadypro alreadypro added the bug Something isn't working label Mar 16, 2022
@Setloth
Copy link

Setloth commented Mar 25, 2022

image
image
also fitting with this, tabs just vanish, might be related

@justanothercell
Copy link

justanothercell commented Apr 8, 2022

this is temporarily fixable by editing the source. replace this in line 316:

// comment this out:
// return !min ? content : content.replace(new RegExp(`^ {${min}}`, "gm"), '');
// put this there:
return content

idk whether thatbreaks things elsewhere, but for now its better than the two missing whitespaces

@alreadypro
Copy link
Author

this is temporarily fixable by editing the source. replace this in line 316:

// comment this out:
// return !min ? content : content.replace(new RegExp(`^ {${min}}`, "gm"), '');
// put this there:
return content
```idk whether thatbreaks things elsewhere, but for now its better than the two missing whitespaces

perhaps you should make a PR?

@justanothercell
Copy link

nah, im just removing functionality cheaply without knowing what else it might break, there has to be a reason why thats there, and im just basically skipping it. until i know why that is there in the first place that would be a bad idea

@Setloth
Copy link

Setloth commented Apr 16, 2022

image
me running some tests console.log'ing the info. the first CONTENT is the variable as it exists in the parameter CONTENT 2 is after the first regex edit, the MIN is the value of min and then END is what it returns

This is an explanation of the first regex:
image

Second:
image
However, the line this regex is on also will reduce the content as an array by the regex into a number by using Math.min() on the values based on the array. Basically, this will compare each value (which represents the index of the returned regex match in the string) to the length of the array, and return the lower set of the numbers. Starting with Infinity, it will then add these numbers together for the sum, and set the min variable to it

Then, the last regex, uses the min variable and makes a regex
image
Then, on this line it will check first if min is 0, if it is, it returns the content variable as before, if it isn't, then it will replace content based on the regex above with a blank string ''

Essentially, the tl;dr, being the only reason that this (stupid) method exists, is to cut out the first line of indents in a code block. AKA, its basically useless. It is very possible that when making the plugin, there was some much that added extra spacing as to mess up the formatting, but currently there is no such issue and it makes no sense to include this. However, I am not the developer for this plugin so I'll leave that up to the developer.

Also, In my copy of the code I am setting the method to:

dedent(content) {
	return content;
}```

As the first regex will remove any tab characters, and that is also pointless.

@CandiceJoy
Copy link

This is fixed in the latest commit. I've submitted a pullrequest to make the latest commit load shiki correctly which fixes both bugs for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants