Spaces:
Configuration error
Configuration error
File size: 1,936 Bytes
a7060a0 | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 | import type { TaxonomyItem, SelectedItem, TaxonomyPath } from "../components/NewTaxonomy/NewTaxonomy";
export const parseCommentClassificationConfig = (config: string | null): TaxonomyItem[] => {
/**
* Assume that there is a single root Taxonomy element for now, which may have multiple
* TaxonomyItem children, as in this simple example below.
* <Taxonomy name="default">
* <TaxonomyItem value="title">
* <TaxonomyItem value="spelling" />
* <TaxonomyItem value="grammar" />
* </TaxonomyItem>
* <TaxonomyItem value="subtitle">
* <TaxonomyItem value="spelling" />
* <TaxonomyItem value="grammar" />
* </TaxonomyItem>
* </Taxonomy>
*/
if (!config) {
return [];
}
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(config, "application/xml");
const taxonomyItems: TaxonomyItem[] = [];
const parseItems = (node: Element, depth = 0, path: string[] = []): TaxonomyItem => {
const value = node.getAttribute("value") || "";
const newPath = [...path, value];
const children: TaxonomyItem[] = [];
node.querySelectorAll(":scope > TaxonomyItem").forEach((childNode) => {
children.push(parseItems(childNode, depth + 1, newPath));
});
return { label: value, children: children.length ? children : undefined, depth, path: newPath };
};
const taxonomyRoot = xmlDoc.querySelector("Taxonomy");
if (taxonomyRoot) {
taxonomyRoot.querySelectorAll(":scope > TaxonomyItem").forEach((node) => {
taxonomyItems.push(parseItems(node));
});
}
return taxonomyItems;
};
export const taxonomyPathsToSelectedItems = (paths: TaxonomyPath[] | null): SelectedItem[] =>
paths
? paths.map((path) =>
path.map((pathElt: any) => ({
label: pathElt,
value: pathElt,
})),
)
: [];
export const COMMENT_TAXONOMY_OPTIONS = { pathSeparator: "/", showFullPath: true };
|