Action that will replace one Return with two

Is there an action that, in a draft, will replace every instance where there is one RETURN with two? That would be very helpful when I paste text that lacks spacing between paragraphs.

Howard

Replacing newline characters with two newlines is easy, but I’m guessing you don’t want to further spread paragraphs that already have a blank line (or two or three) between them. My suggestion is to try an action with one script step:

draft.content = draft.content.replace(/(\n+)?\n/g,
	function($0, $1){
		return $1 ? $0 : "\n\n";
	}
);
draft.update();

Single newlines will be doubled, but bunches of newlines will remain untouched. Running it on this

Hello
How are you?

I am fine
And you?

will turn it into this

Hello

How are you?

I am fine

And you?

You can install it from here: https://actions.getdrafts.com/a/133

2 Likes

Your action does exactly what I needed. Thank you very much.

I just realized that if your draft ends with a single newline, this action will add another to the end. If that annoys you, put this before the last line of the script:

draft.content = draft.content.replace(/\n\n$/, "\n");

I love this action – it was one I was going to ask about as well. I’m still learning regex and javascript (Drafts is a good way to do this!)

How do I do it to switch, and turn two new lines into one? I tried a couple of variations on this but I must be mixing something up with the regex.

Can you help?

EDIT (since I’m limited in replying, being new and all): Mostly just to take two lines to one. When writing fiction, you don’t want extra lines between paragraphs. Drafts forces me to write that way, so I want to remove them when I move text around.

So if there’s multiples, every group of 2 down by 1? Or even, just anything more than 2 reduce to 1.

Thanks!

Two consecutive newlines into one is easy, but what do you want to do with three consecutive newlines? Or four? Should all instances of more than one newline be squeezed down to one?

Here is an example Condense Multiple Line Feeds action, which I think does what you want, changing any occurrences of multiple line feeds in the text to one. The script:

let text = editor.getText();
text = text.replace(/\n+/g, "\n");
editor.setText(text);

Fantastic! That does exactly what I want!

Thank you very much!