import MarkdownIt from "markdown-it"; import pkg from "js-beautify"; import type { ComponentMetadata } from "../../../shared/metadata"; import { getComponentDisplayName } from "./componentUtils"; const { html } = pkg; export function formatComponentWithSlots( block: any, indentLevel: number = 0, componentMetadata?: Map, nestedBlockProperties?: Set ): string { const componentPath = block._component; const componentName = getComponentDisplayName(componentPath); const props = { ...block }; const indent = " ".repeat(indentLevel); delete props._component; const componentSlug = componentPath .replace(/^blocks\//, "") .replace(/^elements\//, "") .replace(/^forms\//, "") .replace(/^navigation\//, "") .replace(/^typography\//, "") .replace(/^wrappers\//, ""); let metadata = componentMetadata?.get(componentSlug); if (!metadata) { metadata = componentMetadata?.get(componentPath); } const supportsSlots = metadata?.supportsSlots ?? false; const isTextComponent = componentPath.includes("heading") || componentPath.includes("text") || componentPath.includes("simple-text") || componentPath.includes("list-item") || componentPath.includes("definition-list-item") || componentPath.includes("testimonial") || componentPath.includes("button") || componentPath.includes("submit"); const textContent = isTextComponent ? props.text : null; if (textContent) { delete props.text; } if (supportsSlots) { if (nestedBlockProperties) { for (const prop of nestedBlockProperties) { if (props[prop] !== undefined) { delete props[prop]; } } } delete props.contentSections; delete props.navBlocks; delete props.formBlocks; delete props.firstColumnContentSections; delete props.secondColumnContentSections; delete props.buttonSections; delete props.slides; } else if (componentPath.includes("split")) { delete props.firstColumnContentSections; delete props.secondColumnContentSections; } else if (componentPath.includes("form")) { delete props.formBlocks; } // If formBlocks exists, we'll handle it as slot content, so remove it from props if (block.formBlocks) { delete props.formBlocks; } // Don't delete items for content-selector as it uses the prop internally if (!componentPath.includes("content-selector")) { delete props.items; } if (!componentPath.includes("choice-group") && !componentPath.includes("segments")) { delete props.options; } let propsString = Object.entries(props) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } else if (Array.isArray(value)) { const formattedArray = JSON.stringify(value, null, 2) .split("\n") .map((line, index) => (index === 0 ? line : `${indent} ${line}`)) .join("\n"); return `${key}={\n${indent} ${formattedArray}\n${indent}}`; } else if (typeof value === "object" && value !== null) { return `${key}={${JSON.stringify(value)}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const items = block.items; const nestedBlocks = block.contentSections || block.navBlocks || block.formBlocks || block.buttonSections; if (nestedBlocks && supportsSlots) { propsString = Object.entries(props) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } else if (Array.isArray(value)) { const formattedArray = JSON.stringify(value, null, 2) .split("\n") .map((line, index) => (index === 0 ? line : `${indent} ${line}`)) .join("\n"); return `${key}={\n${indent} ${formattedArray}\n${indent}}`; } else if (typeof value === "object" && value !== null) { return `${key}={${JSON.stringify(value)}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const blocksArray = Array.isArray(nestedBlocks) ? nestedBlocks : [nestedBlocks]; const nestedContent = blocksArray .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 1, componentMetadata, nestedBlockProperties ) ) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${nestedContent} ${indent}`; } else if (block.formBlocks) { // Handle formBlocks as slot content - render as Form with child components const formAction = block.formAction || "./"; const formBlocksArray = Array.isArray(block.formBlocks) ? block.formBlocks : [block.formBlocks]; const FormComponentName = getComponentDisplayName("building-blocks/forms/form"); const formChildren = formBlocksArray .map((formBlock) => formatComponentWithSlots( formBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${indent} <${FormComponentName} action="${formAction}"> ${formChildren} ${indent} ${indent}`; } else if ( componentPath.includes("split") && (block.firstColumnContentSections || block.secondColumnContentSections) ) { const firstContent = block.firstColumnContentSections ? (Array.isArray(block.firstColumnContentSections) ? block.firstColumnContentSections : [block.firstColumnContentSections] ) .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n") : ""; const secondContent = block.secondColumnContentSections ? (Array.isArray(block.secondColumnContentSections) ? block.secondColumnContentSections : [block.secondColumnContentSections] ) .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n") : ""; return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${ firstContent ? `${indent} ${firstContent} ${indent} ` : "" }${firstContent && secondContent ? "\n" : ""}${ secondContent ? `${indent} ${secondContent} ${indent} ` : "" } ${indent}`; } else if (items && componentPath.includes("list")) { // Handle list items as slot content const itemsArray = Array.isArray(items) ? items : [items]; const isDefinitionList = componentPath.includes("definition-list"); const itemComponentName = isDefinitionList ? "DefinitionListItem" : "ListItem"; const itemsContent = itemsArray .map((item) => { const itemProps = { ...item }; delete itemProps.text; // Remove text from props since it goes in the slot if (isDefinitionList) { delete itemProps.title; // Remove title from props for definition lists } const itemPropsString = Object.entries(itemProps) .sort(([a], [b]) => a.localeCompare(b)) // Sort attributes alphabetically .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const itemText = item.text ? new MarkdownIt() .renderInline(item.text) .trim() .replace(/</g, "<") .replace(/>/g, ">") : ""; if (isDefinitionList) { // For definition lists, use title prop and text in slot const titleProp = item.title ? ` title="${item.title}"` : ""; return `${indent} <${itemComponentName}${titleProp}${itemPropsString ? ` ${itemPropsString}` : ""}>${itemText}`; } else { return `${indent} <${itemComponentName}${itemPropsString ? ` ${itemPropsString}` : ""}>${itemText}`; } }) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${itemsContent} ${indent}`; } else if (items && componentPath.includes("grid")) { // Handle grid items as slot content const itemsArray = Array.isArray(items) ? items : [items]; const itemComponentName = "GridItem"; const itemsContent = itemsArray .map((item) => { const itemProps = { ...item }; delete itemProps.contentSections; // Remove contentSections from props since it goes in the slot const itemPropsString = Object.entries(itemProps) .sort(([a], [b]) => a.localeCompare(b)) // Sort attributes alphabetically .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const itemContent = item.contentSections ? (Array.isArray(item.contentSections) ? item.contentSections : [item.contentSections]) .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n") : ""; return `${indent} <${itemComponentName}${itemPropsString ? ` ${itemPropsString}` : ""}> ${itemContent} ${indent} `; }) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${itemsContent} ${indent}`; } else if (items && componentPath.includes("accordion")) { // Handle accordion items as slot content const itemsArray = Array.isArray(items) ? items : [items]; const itemComponentName = "AccordionItem"; const itemsContent = itemsArray .map((item) => { const itemProps = { ...item }; delete itemProps.contentSections; // Remove contentSections from props since it goes in the slot const itemPropsString = Object.entries(itemProps) .sort(([a], [b]) => a.localeCompare(b)) // Sort attributes alphabetically .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const itemContent = item.contentSections ? (Array.isArray(item.contentSections) ? item.contentSections : [item.contentSections]) .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n") : ""; return `${indent} <${itemComponentName}${itemPropsString ? ` ${itemPropsString}` : ""}> ${itemContent} ${indent} `; }) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${itemsContent} ${indent}`; } else if (items && componentPath.includes("content-selector")) { // Handle content selector items as slot content const itemsArray = Array.isArray(items) ? items : [items]; const itemComponentName = "ContentSelectorPanel"; const containerProps = { ...props }; delete containerProps.items; const containerPropsString = Object.entries(containerProps) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } else if (Array.isArray(value)) { const formattedArray = JSON.stringify(value, null, 2) .split("\n") .map((line, index) => (index === 0 ? line : `${indent} ${line}`)) .join("\n"); return `${key}={\n${indent} ${formattedArray}\n${indent}}`; } else if (typeof value === "object" && value !== null) { return `${key}={${JSON.stringify(value)}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const itemsContent = itemsArray .map((item) => { const itemProps = { ...item }; delete itemProps._component; delete itemProps.contentSections; const itemPropsString = Object.entries(itemProps) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const itemContent = item.contentSections ? (Array.isArray(item.contentSections) ? item.contentSections : [item.contentSections]) .map((nestedBlock) => formatComponentWithSlots( nestedBlock, indentLevel + 2, componentMetadata, nestedBlockProperties ) ) .join("\n") : ""; return `${indent} <${itemComponentName}${itemPropsString ? ` ${itemPropsString}` : ""}> ${itemContent} ${indent} `; }) .join("\n"); return `${indent}<${componentName}${containerPropsString ? ` ${containerPropsString}` : ""}> ${itemsContent} ${indent}`; } else if (block.slides && componentPath.includes("carousel")) { // Handle carousel slides as slot content const slidesArray = Array.isArray(block.slides) ? block.slides : [block.slides]; const slideComponentName = "CarouselSlide"; const slidesContent = slidesArray .map((slide) => { const slideProps = { ...slide }; delete slideProps.contentSections; // Remove contentSections from props since it goes in the slot const slidePropsString = Object.entries(slideProps) .sort(([a], [b]) => a.localeCompare(b)) // Sort attributes alphabetically .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); const slideContent = slide.contentSections ? (Array.isArray(slide.contentSections) ? slide.contentSections : [slide.contentSections]) .map((nestedBlock) => formatComponentWithSlots(nestedBlock, indentLevel + 2, componentMetadata) ) .join("\n") : ""; return `${indent} <${slideComponentName}${slidePropsString ? ` ${slidePropsString}` : ""}> ${slideContent} ${indent} `; }) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${slidesContent} ${indent}`; } else if (block.options && componentPath.includes("select")) { // Handle select options as slot content const optionsArray = Array.isArray(block.options) ? block.options : [block.options]; const optionComponentName = "SelectOption"; const optionsContent = optionsArray .map((option) => { const optionProps = { ...option }; const optionPropsString = Object.entries(optionProps) .sort(([a], [b]) => a.localeCompare(b)) // Sort attributes alphabetically .map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } else if (typeof value === "boolean") { return value ? key : ""; } else if (typeof value === "number") { return `${key}={${value}}`; } return `${key}="${String(value)}"`; }) .filter(Boolean) .join(" "); return `${indent} <${optionComponentName}${optionPropsString ? ` ${optionPropsString}` : ""} />`; }) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${optionsContent} ${indent}`; } else if (textContent) { let htmlContent = textContent; if ( componentPath.includes("text") || componentPath.includes("simple-text") || componentPath.includes("heading") || componentPath.includes("list-item") || componentPath.includes("definition-list-item") || componentPath.includes("testimonial") || componentPath.includes("button") || componentPath.includes("submit") ) { // For text component, use full markdown render if ( componentPath.includes("text") && !componentPath.includes("heading") && !componentPath.includes("simple-text") ) { htmlContent = new MarkdownIt().render(textContent).trim(); } else { // For simple-text and other text components, use inline markdown htmlContent = new MarkdownIt({ html: true }).renderInline(textContent).trim(); } } if ( componentPath.includes("text") && !componentPath.includes("simple-text") && htmlContent.includes("<") ) { const formattedHtml = html(htmlContent, { indent_size: 2, indent_char: " ", max_preserve_newlines: 1, preserve_newlines: true, keep_array_indentation: false, break_chained_methods: false, indent_scripts: "normal", brace_style: "collapse", space_before_conditional: true, unescape_strings: false, jslint_happy: false, end_with_newline: false, wrap_line_length: 0, indent_inner_html: true, comma_first: false, e4x: false, indent_empty_lines: false, }); // Add proper indentation to each line const indentedLines = formattedHtml .split("\n") .map((line) => `${indent} ${line}`) .join("\n"); return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${indentedLines} ${indent}`; } else { return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""}> ${indent} ${htmlContent} ${indent}`; } } else { // Handle multi-line props formatting if (propsString && propsString.includes("\n")) { return `${indent}<${componentName}\n${propsString}\n${indent}/>`; } else { return `${indent}<${componentName}${propsString ? ` ${propsString}` : ""} />`; } } }