stkb / Rewrap

Rewrap extension for VSCode and Visual Studio
https://marketplace.visualstudio.com/items/stkb.rewrap
Other
517 stars 65 forks source link

Rewrap does not work for code within markdown code block #83

Open DanStevens opened 6 years ago

DanStevens commented 6 years ago

The Rewrap Comment / Text command doesn't appear to work within a code block for markdown files.

For example, suppose the rewrap.reformat setting has a value of 30 and I have a file named test.md as follows:

This paragraph will wrap. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

```
This paragraph will not wrap. Fusce in nibh dignissim, vestibulum odio a, lacinia quam. Sed mattis dictum elit at sollicitudin. Curabitur non tortor vehicula elit efficitur vestibulum non ut turpis. 
```

After running the Rewrap Comment / Text command, the result is:

This paragraph will wrap.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.

```
This paragraph will not wrap. Fusce in nibh dignissim, vestibulum odio a, lacinia quam.
```

The first paragraph has been rewrapped, but the second hasn't. The same happens if instead I indent the second paragraphs instead of using triple backticks:

This paragraph will wrap. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    This paragraph will not wrap. Fusce in nibh dignissim, vestibulum odio a, lacinia quam.

This doesn't affect plain text files - if I create a file named test.txt with the same content as test.md, the result of the wrap is:

This paragraph will wrap.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.

``` This paragraph will not
wrap. Fusce in nibh dignissim,
vestibulum odio a, lacinia
quam. ```

Is this by designed? If so, can I setting be created to allow control over this behaviour?

stkb commented 6 years ago

Hi,

That content within a code block (fenced (```), or indented) is by design. For the same reason that code within source files isn't rewrapped: code is something you normally don't want messed with.

However, I could add a feature that comments within code blocks in markdown files are wrapped. I'm guessing you have something like this?

Hendrerit adolescens vim ad, qualisque delicatissimi ad sit. 

``` js
// Eos et tractatos intellegam, usu omnium sapientem disputationi ea.
function fib(n) {
    return n < 2 ? 1 : (fib(n - 2) + fib(n - 1))
}
```

Here the comment in the code could also be wrapped, as long as you have a language marker after the ``` (in this case js). (This means it won't work with indented code blocks as they have no language marker)

Hendrerit adolescens vim ad, qualisque
delicatissimi ad sit. 

``` js
// Eos et tractatos intellegam, usu 
// omnium sapientem disputationi ea.
function fib(n) {
    return n < 2 ? 1 : (fib(n - 2) + fib(n - 1))
}
```

Note the text above and comment within the code are wrapped, but not the code itself.

Plain text files (.txt) don't use the markdown parser which is why they don't recognise code blocks and you don't see the same behaviour.

DanStevens commented 6 years ago

I see - it makes sense that you may not want rewrap to wrap source code. In my particular case I was wanting to wrap YAML code within a markdown code block, which contained long strings that I wanted to wrap.

If rewrap were to wrap comments within a markdown code block, and a user really did want to wrap source code, they could convert the code to a comment, wrap, then uncomment.

Noah-Silvera commented 4 years ago

I'm using rewwap to write documentation, which has a lot of codeblocks in markdown with comments. I would also like rewrap to be able to wrap the comments in the codeblocks!