I’m trying to replace code in this format
{{>
default-hero
color="red"
title="Foo"
subheading="Bar"
background="about-us-hero-desktop.jpg"
squarePartial="company/square.hbs"
squareBaseline=true
}}
with this
{% set color = "red" %}
{% set heroTitle = "Foo" %}
{% set subheading = "Bar" %}
{% set background = "about-us-hero-desktop.jpg" %}
{% set squarePartial = "company/square.njk" %}
{% set squareBaseline = true %}
{% include "default-hero.njk" %}
This would run recursively against many files. Is this possible? I started writing code like this
oldContent = newContent;
regex = /{{>s*([a-zA-Z0-9-_/]+)?s*([a-zA-Z0-9-_/.=:"'s]+)s*}}/gi;
replaceVal = '{% include $1.njk $2 %}';
newContent = oldContent.replace(regex, replaceVal);
but it only works partially.
2
Answers
I figured it out. Thanks for everyone's help though.
Not sure this can achieve entirely from
Regex
only. As far as my understanding you need the stringreplace
in javascript for this to some extent.Refer the code below: