Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1x 32x 32x 32x 32x 2x 2x 2x 2x 2x 5x 3x 3x 2x 2x 2x 3x 2x 2x 32x | import { DOMParser } from 'xmldom'
import { Template } from '../elements'
export const resolveTemplateRefs = (
xml: string,
templates: Record<string, Template> = {}
): string => {
const parser = new DOMParser()
const dom = parser.parseFromString(xml, 'text/xml')
// replace template nodes with their actual content
const useTemplateNodes = Array.from(dom.getElementsByTagName('UseTemplate'))
useTemplateNodes.forEach((node) => {
const templateName = node.getAttribute('ref')
Iif (!templateName) return
const template = templates[templateName]
Iif (!template) return
// get all the attributes
const attrs = Array.from(node.attributes).reduce((acc, attr) => {
if (attr.name === 'ref') return acc
acc[attr.name] = attr.value
return acc
}, {} as Record<string, string>)
const templateText = template.Text
// replace the variables in the template text
// todo: update type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let text = (templateText as any)._ ?? templateText
Object.entries(attrs).forEach(([key, value]) => {
text = text.replace(new RegExp(`{${key}}`, 'g'), value)
})
const textNode = dom.createTextNode(text)
node.parentNode?.replaceChild(textNode, node)
})
return dom.toString()
}
|