Files
wehub-resource-sync c48612c494
CI / E2E Tests (push) Has been cancelled
CI / Lint, Typecheck & Unit Tests (push) Has been cancelled
Docs Build / Build docs site (push) Has been cancelled
Publish @openmaic packages / Build, validate & publish (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:23 +08:00

47 lines
1.3 KiB
JavaScript

import * as txml from 'txml/dist/txml.mjs'
let cust_attr_order = 0
export function simplifyLostLess(children, parentAttributes = {}) {
const out = {}
if (!children.length) return out
if (children.length === 1 && typeof children[0] === 'string') {
return Object.keys(parentAttributes).length ? {
attrs: { order: cust_attr_order++, ...parentAttributes },
value: children[0],
} : children[0]
}
for (const child of children) {
if (typeof child !== 'object') return
if (child.tagName === '?xml') continue
if (!out[child.tagName]) out[child.tagName] = []
const kids = simplifyLostLess(child.children || [], child.attributes)
if (typeof kids === 'object') {
if (!kids.attrs) kids.attrs = { order: cust_attr_order++ }
else kids.attrs.order = cust_attr_order++
}
if (Object.keys(child.attributes || {}).length) {
kids.attrs = { ...kids.attrs, ...child.attributes }
}
out[child.tagName].push(kids)
}
for (const child in out) {
if (out[child].length === 1) out[child] = out[child][0]
}
return out
}
export async function readXmlFile(zip, filename) {
try {
const data = await zip.file(filename).async('string')
return simplifyLostLess(txml.parse(data))
}
catch {
return null
}
}