Skip to content

Commit

Permalink
Create nestedTagsSupport.js
Browse files Browse the repository at this point in the history
  • Loading branch information
PatoFlamejanteTV authored Nov 23, 2024
1 parent 3bf6cc4 commit 9f582d2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions compiler/WIP/nestedTagsSupport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function enhancedHtsToHtml(htsCode) {
const nestedTagPattern = /{([^}]*)}/g;
const universalTagPattern = /(\w+)\(\s*"([^"]+)"\s*\)|(\w+)\[([^\]]+)\]\(\)/g;

function processTags(code) {
return code.replace(universalTagPattern, (match, tag1, content1, tag2, attributes) => {
if (tag1) {
return `<${tag1}>${processTags(content1)}</${tag1}>`;
} else if (tag2) {
const [attributeName, attributeValue] = attributes.split('=');
if (attributeName && attributeValue) {
return `<${tag2} ${attributeName}="${attributeValue}">${attributeValue}</${tag2}>`;
} else {
console.warn(`Invalid attribute syntax: ${attributes}`);
return match;
}
}
return match;
});
}

return htsCode.replace(nestedTagPattern, (match, nestedCode) => {
return processTags(nestedCode);
}).replace(universalTagPattern, (match, tag1, content1, tag2, attributes) => {
if (tag1) {
return `<${tag1}>${content1}</${tag1}>`;
} else if (tag2) {
const [attributeName, attributeValue] = attributes.split('=');
if (attributeName && attributeValue) {
return `<${tag2} ${attributeName}="${attributeValue}">${attributeValue}</${tag2}>`;
} else {
console.warn(`Invalid attribute syntax: ${attributes}`);
return match;
}
}
return match;
});
}

// Usage
console.log(enhancedHtsToHtml('div("Hello, {span(\"World!\")}")'));
console.log(enhancedHtsToHtml('img[src="logo.png"]()'));
console.log(enhancedHtsToHtml('a[invalid]()'));

0 comments on commit 9f582d2

Please sign in to comment.