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.