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
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
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.