@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
44 lines (42 loc) • 1.59 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region src/transpiler/html/getHTMLCustomComponents.ts
const parseAttributes = (attributesString) => {
const attributes = {};
if (!attributesString?.trim()) return attributes;
[...attributesString.matchAll(/([a-zA-Z0-9-:_@]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+))?/g)].forEach((match) => {
const attrName = match[1];
attributes[attrName] = "string";
});
return attributes;
};
/**
* Extracts component names from an HTML string.
* - Standard HTML tags are set to `true`.
* - Custom components are parsed to extract their attributes/props.
*/
const getHTMLCustomComponents = (content) => {
if (typeof content !== "string") throw new Error("content must be a string");
const matches = [...content.matchAll(/<(\/)?([a-zA-Z0-9.-]+)\s*([\s\S]*?)(\/?)>/g)];
const components = {};
matches.forEach((match) => {
const isClosing = !!match[1];
const tagName = match[2];
const attributesString = match[3];
const isSelfClosing = !!match[4];
if (/^[a-z][a-z0-9]*$/.test(tagName)) {
components[tagName] = true;
return;
}
if (!components[tagName]) components[tagName] = {};
if (components[tagName] === true) return;
if (isClosing) return;
const attributes = parseAttributes(attributesString);
const componentDef = components[tagName];
Object.assign(componentDef, attributes);
if (!isSelfClosing) componentDef.children = "string";
});
return components;
};
//#endregion
exports.getHTMLCustomComponents = getHTMLCustomComponents;
//# sourceMappingURL=getHTMLCustomComponents.cjs.map