Drafts: find/replace text action. Help

I tried to create an action that would convert the “original text” below to the “target text” below (or if at all possible to the very last version of it below)

Is this possible? Is it too complex? (I have very little javascript skills and definitely not enough for this one…).

Any help is well appreciated:

Original text structure (ignore the ‘ before each line)

‘ # Title

‘ ## Heading 1

‘ * Text text text
‘ * More text
‘ * Even more text

‘ ## Heading 2

‘ ## - Line 1

‘ * Line 1 details

‘ ## - Line2

‘ * Line 2 details

‘ ## - Line 3

‘ * Line 3 details

‘ ## Heading 3

Target (after running action):

Line 1

Line 1 details

Line2

Line 2 details

Line 3

Line 3 details

Or even better this one below :grinning:

Line 1. Line 1 details

Line2. Line 2 details

Line 3. Line 3 details

Thanks!!

I assume that you want to keep only those parts of the text that consist of a 2nd level heading like “Linex” where x ia a digit (or a number?) Is the first such heading always “Line 1”? Or is there any other way to recognise the start of the block of text you’re interested in?

What I’d try to do:

  • parse the lines of text into a JavaScript array `let arr = text.split(’\n’);
  • walk trough this array until I find “## Line 1” popping all the other lines in the process
  • add a dot to the end of all entries starting with "## "
  • remove the leading "## " and "* " from all following entries
  • when I find the next 2nd level heading that doesn’t match the pattern “## Linex” (BTW: Is the space between “Line” and the digit required or not?), pop the rest of the array
  • finally do an arr.join('\n')
1 Like

Thanks!!

Line x is always

- Linex: variable text

Everything is fixed apart from the “variable text”

Thanks for the hints but my js skills are not that good :grinning:

I’ ve found a find/replace action and tried to adjust, but it was of no good :grinning: :man_facepalming:

There are some discrepancies and some things that are not 100% clear from your previous posts, so I’ve made a number of assumptions along the way.

e.g.

  • There are spaces in “line” headings after ‘line’ sometimes but not every time.
  • Will there ever be other headings between “line” heading sections?
  • Do you exclusively use hashes and asterisks or do you ever use other valid markup for headings and bullets?
  • Are there always new lines between everything except lists in non-“line” heading sections?
  • Would there ever be multiple bullets under a “line” heading?
  • Would a “line” heading ever be the last section?

However, based on a big handful of assumptions, this script…

function transformAFC()
{
	const SECTION_HEADER = "## - Line";
	const SECTION_HEADER_MATCH = /^## - /;
	const SECTION_CONTENT_START = "* ";
	const SECTION_CONTENT_MATCH = /^\* /;
	const OTHER_HEADER_START = "#";
	const SECTION_SEPARATOR = "\n\n"

	let bCapture = false;
	let strSection;
	let astrOutput = [];

	draft.lines.forEach(function(strLine)
	{
		//Start capturing from the section heading
		if (strLine.startsWith(SECTION_HEADER))
		{
			//If a capture section was the last section, make sure it is added to the array
			if (bCapture) astrOutput.push(strSection);

			bCapture = true;
			strSection = strLine.replace(SECTION_HEADER_MATCH, "") + ". ";
		}
		//Capture the section line
		else if (strLine.startsWith(SECTION_CONTENT_START) && bCapture) strSection += strLine.replace(SECTION_CONTENT_MATCH, "");
		//Found a new header that isn't a section header, so stop capturing and store the section for later
		else if(strLine.startsWith(OTHER_HEADER_START) && bCapture)
		{
			astrOutput.push(strSection);
			bCapture = false;
		}
		//Do nothing, this isn't a line to be processed
		else bCapture = bCapture;
	});

	//If a capture section was the final section, make sure it is added to the array
	if (bCapture) astrOutput.push(strSection);

	//Build and return the output
	return astrOutput.join(SECTION_SEPARATOR);
}

//Take a backup, then change the draft content
draft.saveVersion();
draft.content = transformAFC();
draft.update();

… should transform content like this …

# Title

## Heading 1

* Text text text
* More text
* Even more text

## Heading 2

## - Line 1

* Line 1 details

## - Line2

* Line 2 details

## - Line 3

* Line 3 details

## Heading 3

* Line 4 details

… to content like this …

Line 1. Line 1 details

Line2. Line 2 details

Line 3. Line 3 details

If it isn’t quite right, hopefully it gives you enough of a starting point to resolve the rest. You may even be able to simplify it based on the variety of the structure of the initial draft content.

1 Like

Thanks @sylumer !!!

Did a quick tweak in the regex and it works fine.

Have noticed now that the source text has 2 spaces that I haven’t noticed before between line x and details. So after running the action I get:

Line x[space][space].[space]details

Would this be an easy fix? Otherwise, leave it.

And I really appreciate your help.

Untested, but changing

strSection = strLine.replace(SECTION_HEADER_MATCH, "") + ". ";

to include a trim

strSection = strLine.replace(SECTION_HEADER_MATCH, "").trim() + ". ";

should remove any whitespace from the end (and start) of that content.

1 Like

That’s working great @sylumer . Many thanks!!!