I'm trying to create a script that uses some template files and then replaces the contents of the file with some data asked to the user.
I've been using the {{word}}
pattern to be able to replace specific words, but I'd like to be able to remove also whole sections of the file depending on the user input data.
I'd like to be able to write into the template file something like this:
Some content that I always want.
{{<mySection>}}
Some conditional content.
More conditional content.
{{</mySection>}}
More content that I always want.
And be able to remove the whole content of <mySection>
(that included) or not depending on what the user has specified.
Right now, I'm using fs-extra
to do the pattern replacement:
const templateContent = await fs.readFile(
`${templatePath}/TemplateContent.txt`,
{ encoding: 'utf-8' },
);
const outputContent = templateContent.replace(new RegExp(`{{${word}}}`, 'g'), value);
Which would be the best method as per simplicity and performance, taking into account that there can be multiple of these different sections in the same file?
I've thought of getting the index of the beginning of the section and the end, and doing some kind of substring or splitting in the string and joining it later, but I don't know if that would be not very performance-wise keeping in mind that there will be multiple sections and I'll have to do that operation multiple times.
Thank you!