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

Function optimizeComponentTree

packages/extension/mcp/tools/code/text/render.ts:204–250  ·  view source on GitHub ↗
(node: DevComponent | string, classProp: string)

Source from the content-addressed store, hash-verified

202}
203
204function optimizeComponentTree(node: DevComponent | string, classProp: string) {
205 if (typeof node === 'string') return
206
207 if (node.children) {
208 for (const child of node.children) {
209 optimizeComponentTree(child, classProp)
210 }
211 }
212
213 const UNWRAP_WHITELIST = new Set([
214 'li',
215 'p',
216 'h1',
217 'h2',
218 'h3',
219 'h4',
220 'h5',
221 'h6',
222 'strong',
223 'em',
224 'u',
225 's',
226 'code',
227 'a',
228 'span'
229 ])
230
231 if (UNWRAP_WHITELIST.has(node.name) && node.children && node.children.length === 1) {
232 const child = node.children[0]
233 if (typeof child !== 'string' && child.name === 'span') {
234 const childProps = child.props || {}
235 const extraChildProps = Object.keys(childProps).filter((key) => key !== classProp)
236 if (extraChildProps.length === 0) {
237 const parentClass = String(node.props?.[classProp] || '')
238 const childClass = String(childProps?.[classProp] || '')
239 const mergedClass = joinClassNames([parentClass, childClass])
240
241 if (mergedClass) {
242 if (!node.props) node.props = {}
243 node.props[classProp] = mergedClass
244 }
245
246 node.children = child.children
247 }
248 }
249 }
250}
251
252function buildInlineTree(
253 runs: TextRun[],

Callers 1

renderTextSegmentsFunction · 0.85

Calls 2

joinClassNamesFunction · 0.90
hasMethod · 0.80

Tested by

no test coverage detected