Skip to content

Commit 867cffd

Browse files
committed
Handle root being a single item
1 parent 925bcf6 commit 867cffd

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/lib/loadOutline.ts

+23-19
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ async function loadOutline(url_str: string):Promise<OpmlData> {
139139
retVal.title = xmlTitle || "";
140140

141141
const outlineStart = Date.now();
142-
processNode(retVal, retVal.root, xml_data.opml.body.outline as OpmlOutline[]);
142+
const rawRoot = xml_data.opml.body.outline;
143+
const treeRoot = Array.isArray(rawRoot) ? rawRoot : [rawRoot];
144+
processNode(retVal, retVal.root, treeRoot as OpmlOutline[]);
143145

144146
retVal.messages.push(`Entries: ${retVal.count}`);
145147
retVal.messages.push(`Outline parsing complete in ${Date.now() - outlineStart}ms.`);
@@ -153,28 +155,30 @@ async function loadOutline(url_str: string):Promise<OpmlData> {
153155

154156
function processNode(retVal: OpmlData, parent: TreeItem, data: OpmlOutline[]) {
155157

156-
//console.log(data);
157-
158-
for (const item of data) {
159-
retVal.count++;
160-
161-
const newItem: TreeItem = {
162-
id: `ti-${retVal.count}`,
163-
label: item.title || item.text || "",
164-
htmlUrl: purifyUrl(item.htmlUrl) || purifyUrl(item.url),
165-
xmlUrl: purifyUrl(item.xmlUrl),
166-
children: [],
167-
};
168-
parent.children.push(newItem);
169-
170-
if (item.outline) {
171-
const subtree: OpmlOutline[] = Array.isArray(item.outline) ? item.outline : [item.outline];
172-
processNode(retVal, newItem, subtree);
158+
try {
159+
for (const item of data) {
160+
retVal.count++;
161+
162+
const newItem: TreeItem = {
163+
id: `ti-${retVal.count}`,
164+
label: item.title || item.text || "",
165+
htmlUrl: purifyUrl(item.htmlUrl) || purifyUrl(item.url),
166+
xmlUrl: purifyUrl(item.xmlUrl),
167+
children: [],
168+
};
169+
parent.children.push(newItem);
170+
171+
if (item.outline) {
172+
const subtree: OpmlOutline[] = Array.isArray(item.outline) ? item.outline : [item.outline];
173+
processNode(retVal, newItem, subtree);
174+
}
173175
}
176+
} catch (err: unknown) {
177+
retVal.errorCount++;
178+
retVal.messages.push("Error processing outline node: " + errorMessage(err));
174179
}
175180
}
176181

177-
178182
export {
179183
loadOutline,
180184
}

0 commit comments

Comments
 (0)