From 0e431d1916abba4b97d4a62adf1398035c934f61 Mon Sep 17 00:00:00 2001 From: Neil Seligmann Date: Fri, 2 Aug 2024 22:07:14 -0300 Subject: [PATCH] feat: added support for "Group" tag in "Info.xml" --- src/definitions/Metadata.ts | 32 +++++++++++++++++++++++++++++--- tests/parse-node.test.ts | 8 ++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/definitions/Metadata.ts b/src/definitions/Metadata.ts index e4c3783..fef7fbd 100644 --- a/src/definitions/Metadata.ts +++ b/src/definitions/Metadata.ts @@ -4,7 +4,7 @@ import { DefaultFomodDocumentConfig, FomodDocumentConfig } from "./lib/FomodDocu import { ElementObjectMap, XmlRepresentation } from "./lib/XmlRepresentation"; export interface FomodInfoData { - [key: string]: string|undefined; + [key: string]: string|string[]|undefined; /** MO2 reads and displays this field in the installer dialog. Vortex reads but does not use it. */ Name?: string; @@ -21,6 +21,9 @@ export interface FomodInfoData { /** MO2 reads, displays, and potentially stores this field. There are no restrictions on its value. */ Version?: string; + /** An array of strings (categories) */ + Groups?: string[]; + } export const DefaultInfoSchema = 'https://fomod.bellcube.dev/schemas/Info.xsd'; @@ -54,7 +57,17 @@ export class FomodInfo extends XmlRepresentation { if (value === undefined) continue; const child = document.createElement(key); - child.textContent = value; + + if (Array.isArray(value)) { + for (const subChildValue of value) { + // Append "element" tag to child, with the value + const subChild = document.createElement('element'); + subChild.textContent = subChildValue; + child.appendChild(subChild); + } + } else { + child.textContent = value; + } element.appendChild(child); } @@ -80,7 +93,20 @@ export class FomodInfo extends XmlRepresentation { for (const child of element.children) { if (child.textContent === null) continue; - data[child.tagName] = child.textContent; + + if (child.children.length > 0) { + const filteredChildren: string[] = []; + + for (const subchild of child.children) { + if (typeof subchild.textContent === 'string') { + filteredChildren.push(subchild.textContent); + } + } + + data[child.tagName] = filteredChildren; + } else { + data[child.tagName] = child.textContent; + } } const obj = new FomodInfo(data); diff --git a/tests/parse-node.test.ts b/tests/parse-node.test.ts index c8d5494..e3d6a81 100644 --- a/tests/parse-node.test.ts +++ b/tests/parse-node.test.ts @@ -162,6 +162,10 @@ describe('Info.xml Got Parsed', () => { 1 https://example.com 1.0.0 + + category 1 + testGroup2 + `, {contentType: 'text/xml'}).window.document; const infoObj = parseInfoDoc(infoDoc); @@ -189,6 +193,10 @@ describe('Info.xml Got Parsed', () => { expect(infoObj.data.Version).toBe('1.0.0'); }); + test('Groups Is Correct', () => { + expect(infoObj.data.Groups).toStrictEqual(['category 1', 'testGroup2']); + }); + describe('Converted Back Into Element Properly', () => { const newElement = infoObj.asElement(new JSDOM('', {contentType: 'text/xml'}).window.document);