bd-contributions icon indicating copy to clipboard operation
bd-contributions copied to clipboard

Missing whitespace when copying codeblock contents

Open alreadypro opened this issue 2 years ago • 6 comments

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 avatar Mar 16 '22 20:03 alreadypro

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

Setloth avatar Mar 25 '22 22:03 Setloth

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

justanothercell avatar Apr 08 '22 14:04 justanothercell

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?

alreadypro avatar Apr 12 '22 17:04 alreadypro

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

justanothercell avatar Apr 12 '22 17:04 justanothercell

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.

Setloth avatar Apr 16 '22 05:04 Setloth

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.

CandiceJoy avatar May 19 '22 04:05 CandiceJoy