MCPcopy Create free account
hub / github.com/ecomfe/tempad-dev / stringifyBaseComponent

Function stringifyBaseComponent

packages/extension/utils/component.ts:192–281  ·  view source on GitHub ↗
(
  component: DevComponent,
  stringifyProp: (key: string, value: unknown) => string,
  indentLevel = 0,
  isInline?: (tagName: string) => boolean
)

Source from the content-addressed store, hash-verified

190const INDENT_UNIT = ' '
191
192function stringifyBaseComponent(
193 component: DevComponent,
194 stringifyProp: (key: string, value: unknown) => string,
195 indentLevel = 0,
196 isInline?: (tagName: string) => boolean
197) {
198 const indent = INDENT_UNIT.repeat(indentLevel)
199 const { name, props, children: rawChildren } = component
200
201 if (name === RAW_TAG_NAME) {
202 const content = String(props.content || '')
203 const injectedProps = props.injectedProps as Record<string, string> | undefined
204 let result = content
205 if (injectedProps) {
206 result = mergeAttributes(content, injectedProps)
207 }
208 return indentAll(result, indent)
209 }
210
211 const propItems = Object.entries(props)
212 .filter(([, value]) => value != null)
213 .map((entry) => stringifyProp(...entry))
214 .filter(Boolean)
215
216 const firstItem = propItems[0]
217
218 const propsString =
219 propItems.length === 0
220 ? ''
221 : propItems.length === 1
222 ? firstItem.includes('\n')
223 ? ` ${indentAll(firstItem, indent, true)}`
224 : ` ${firstItem}`
225 : `\n${propItems
226 .map((prop) => `${indentAll(prop, indent + INDENT_UNIT)}`)
227 .join('\n')}\n${indent}`
228
229 const children = rawChildren.filter((child) => child != null)
230
231 // Compact Mode Check
232 const shouldCompact = isInline?.(name) ?? false
233
234 let childrenString: string
235
236 if (shouldCompact) {
237 // Compact Mode: No newlines, no extra indent, join with empty string
238 childrenString = children
239 .map((child): string => {
240 if (typeof child === 'string') {
241 return escapeHTML(child)
242 }
243 return stringifyBaseComponent(child, stringifyProp, 0, isInline)
244 })
245 .join('')
246 } else if (children.length === 0) {
247 // Block Mode with no children
248 childrenString = ''
249 } else {

Callers 2

stringifyVueComponentFunction · 0.85
stringifyJSXComponentFunction · 0.85

Calls 5

indentAllFunction · 0.90
escapeHTMLFunction · 0.90
mergeAttributesFunction · 0.85
isCustomComponentTagFunction · 0.85
hasMethod · 0.80

Tested by

no test coverage detected