Skip to content

Commit 9dac599

Browse files
authored
Merge pull request #3 from kingkongdevs/issue-variable-replacement
Issue variable replacement
2 parents 17ab5bf + e363995 commit 9dac599

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,36 @@ function viteHTMLIncludes(options = {}) {
7575
let loopLocals = { ...locals, [item]: currentItem, [index]: currentIndex };
7676
let clonedNode = parse(eachNode.innerHTML);
7777
processTemplate(clonedNode, loopLocals);
78+
replaceVariables(clonedNode, loopLocals);
7879
nodesToReplace.push(...clonedNode.childNodes);
7980
});
8081

8182
eachNode.replaceWith(...nodesToReplace);
8283
});
8384
}
8485

86+
function replaceVariables(fragment, locals) {
87+
const variableRegex = /\{\{\s*(\w+)\s*\}\}/g; // Regex to match {{variableName}}, {{ variableName }}
88+
fragment.querySelectorAll('*').forEach(node => {
89+
// Replace variables in text content
90+
if (node.nodeType === 3 || node.nodeType === 1) { // Node is a text node
91+
node.innerHTML = node.innerHTML.replace(variableRegex, (match, variableName) => {
92+
return locals[variableName] !== undefined ? locals[variableName] : match;
93+
});
94+
}
95+
// Replace variables in element's attributes if node is an element
96+
if (node.nodeType === 1) {
97+
Object.keys(node.attributes).forEach(attr => {
98+
let attrValue = node.getAttribute(attr);
99+
let replacedAttrValue = attrValue.replace(variableRegex, (match, variableName) => {
100+
return locals[variableName] !== undefined ? locals[variableName] : match;
101+
});
102+
node.setAttribute(attr, replacedAttrValue);
103+
});
104+
}
105+
});
106+
}
107+
85108
function ensureClosedIncludeTags(html) {
86109
// This regex finds <include> tags and ensures they are self-closing or properly closed
87110
const regex = /<include(.*?)>(?!(<\/include>))/g;
@@ -100,7 +123,6 @@ function viteHTMLIncludes(options = {}) {
100123
processConditionals(fragment, locals);
101124
processSwitchCases(fragment, locals);
102125
processEachLoops(fragment, locals);
103-
// Implement other template processing functions here if needed
104126
}
105127

106128
return {
@@ -126,7 +148,6 @@ function viteHTMLIncludes(options = {}) {
126148
console.error(`Error parsing locals JSON: ${localsString}`, e);
127149
}
128150
}
129-
130151
if (!src) return;
131152

132153
const filePath = resolve(config.root + componentsPath + src);
@@ -138,7 +159,11 @@ function viteHTMLIncludes(options = {}) {
138159
// Process the entire template
139160
processTemplate(fragment, locals);
140161

162+
if(Object.keys(locals).length != 0) {
163+
replaceVariables(fragment, locals); //Replace the variables
164+
}
141165
node.replaceWith(fragment);
166+
142167
} catch (e) {
143168
console.error(`Error including file: ${filePath}`, e);
144169
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kingkongdevs/vite-plugin-html-includes",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Plugin for Vite that replaces include tags with corresponding HTML components",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)