Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const parseComponentJSON = (
input: Record,
params: ParseComponentJSONParams = {}
): ComponentUIDL => {
const safeInput = params.noClone ? input : UIDLUtils.cloneObject(input)
const node = safeInput.node as Record
const result: ComponentUIDL = {
...((safeInput as unknown) as ComponentUIDL),
}
// other parsers for other sections of the component here
result.node = parseComponentNode(node) as UIDLElementNode
return result
}
throw new Error(contentValidationResult.errorMsg)
}
const { components = {}, root } = uidl
// Based on the routing roles, separate pages into distict UIDLs with their own file names and paths
const pageUIDLs = createPageUIDLs(uidl, this.strategy)
// Set the filename and folder path for each component based on the strategy
prepareComponentOutputOptions(components, this.strategy)
// Set the local dependency paths based on the relative paths between files
resolveLocalDependencies(pageUIDLs, components, this.strategy)
// Initialize output folder and other reusable structures
const rootFolder = UIDLUtils.cloneObject(template || DEFAULT_TEMPLATE)
let collectedDependencies: Record = {}
// If static prefix is not specified, compute it from the path, but if the string is empty it should work
const assetsPrefix =
typeof this.strategy.static.prefix === 'string'
? this.strategy.static.prefix
: '/' + this.getAssetsPath().join('/')
const options: GeneratorOptions = {
assetsPrefix,
projectRouteDefinition: root.stateDefinitions.route,
mapping,
skipValidation: true,
}
// Handling pages
for (const pageUIDL of pageUIDLs) {
export const resolveChildren = (mappedChildren: UIDLNode[], originalChildren: UIDLNode[] = []) => {
let newChildren = UIDLUtils.cloneObject(mappedChildren)
let placeholderFound = false
newChildren.forEach((childNode) => {
UIDLUtils.traverseNodes(childNode, (node, parentNode) => {
if (!isPlaceholderNode(node)) {
return // we're only interested in placeholder nodes
}
if (parentNode !== null) {
if (parentNode.type === 'element') {
// children nodes can only be added to type 'element'
// filter out the placeholder node and add the original children instead
parentNode.content.children = replacePlaceholderNode(
parentNode.content.children,
originalChildren
)
})
}
if (mappedElement.children) {
originalElement.children = resolveChildren(mappedElement.children, originalElement.children)
// Solves an edge case for next.js by passing the styles from the tag to the <a> tag
const anchorChild = originalElement.children.find(
(child) => child.type === 'element' && child.content.elementType === 'a'
) as UIDLElementNode
// only do it if there's a child </a><a> tag and the original element is a navlink
const shouldPassStylesToAnchor =
originalElement.style && originalElementType === 'navlink' && anchorChild
if (shouldPassStylesToAnchor) {
anchorChild.content.style = UIDLUtils.cloneObject(originalElement.style)
originalElement.style = {}
}
}
}
</a>
public resolveUIDL(input: ComponentUIDL, options: GeneratorOptions = {}) {
const mapping = utils.mergeMappings(this.mapping, options.mapping)
const newOptions = {
...options,
mapping,
}
const uidl = UIDLUtils.cloneObject(input)
UIDLUtils.setFriendlyOutputOptions(uidl)
utils.checkForIllegalNames(uidl, mapping)
utils.resolveNode(uidl.node, newOptions)
const nodesLookup = {}
utils.createNodesLookup(uidl.node, nodesLookup)
utils.generateUniqueKeys(uidl.node, nodesLookup)
utils.ensureDataSourceUniqueness(uidl.node)
// There might be urls that need to be prefixed in the metaTags of the component
utils.resolveMetaTags(uidl, options)
public resolveElement(element: UIDLElement, options: GeneratorOptions = {}) {
const mapping = utils.mergeMappings(this.mapping, options.mapping)
const newOptions = {
...options,
mapping,
}
const returnElement = UIDLUtils.cloneObject(element)
utils.resolveElement(returnElement, newOptions)
return returnElement
}
}
export const parseProjectJSON = (
input: Record,
params: ParseProjectJSONParams = {}
): ProjectUIDL => {
const safeInput = params.noClone ? input : UIDLUtils.cloneObject(input)
const root = safeInput.root as Record
const result = {
...((safeInput as unknown) as ProjectUIDL),
}
result.root = parseComponentJSON(root, { noClone: true })
if (result.components) {
result.components = Object.keys(result.components).reduce(
(parsedComponnets: Record, key) => {
parsedComponnets[key] = parseComponentJSON((result.components[key] as unknown) as Record<
string,
unknown
>)
return parsedComponnets
},