template-file
Advanced tools
Comparing version
@@ -34,8 +34,20 @@ "use strict"; | ||
exports.renderGlob = renderGlob; | ||
const tagRegEx = /\{\{\s*(.*?)\s*\}\}/g; | ||
const sectionRegEx = /\{\{\s*(?:#(.*?))\s*\}\}\n*([\s\S]*?)\s*\{\{\s*\/\1\s*\}\}/g; | ||
const combinedRegEx = new RegExp(`${sectionRegEx.source}|${tagRegEx.source}`, 'g'); | ||
function getTemplateRegEx() { | ||
const anything = '([\\s\\S]*?)'; | ||
const optionalNewLines = '\\n*'; | ||
const optionalWhitespace = '\\s*'; | ||
const spaceNotNewLines = `[ \t]*`; | ||
const tagStart = `{{${optionalWhitespace}`; | ||
const tagEnd = `${optionalWhitespace}}}`; | ||
const sectionStart = `${spaceNotNewLines}${tagStart}(?:#(.*?))${tagEnd}${optionalNewLines}`; | ||
const sectionEnd = `${optionalWhitespace}${tagStart}\\/\\1${tagEnd}`; | ||
const repeatingSectionTag = `${sectionStart}${anything}${sectionEnd}`; | ||
const replacementTag = `${tagStart}(.*?)${tagEnd}`; | ||
const combinedRegEx = new RegExp(`${repeatingSectionTag}|${replacementTag}`, 'g'); | ||
return combinedRegEx; | ||
} | ||
function render(template, data) { | ||
return template.replace(combinedRegEx, (_match, sectionTag, sectionContents, basicTag) => { | ||
// Tag is for an array section | ||
const templateRegEx = getTemplateRegEx(); | ||
return template.replace(templateRegEx, (_match, sectionTag, sectionContents, replacementTag) => { | ||
// Tag is for a repeating section | ||
if (sectionTag !== undefined) { | ||
@@ -49,3 +61,3 @@ const replacements = deep_1.get(sectionTag, data); | ||
} | ||
const replacement = deep_1.get(basicTag, data); | ||
const replacement = deep_1.get(replacementTag, data); | ||
// If a template variable is found but nothing is supplied to fill it, remove it | ||
@@ -52,0 +64,0 @@ if (replacement === null || replacement === undefined) { |
{ | ||
"name": "template-file", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "description": "🔀 Replace {{ variables }} in all your files", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84483
1.22%172
7.5%