diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..4a7892a Binary files /dev/null and b/.DS_Store differ diff --git a/dist/.DS_Store b/dist/.DS_Store new file mode 100644 index 0000000..6a0373e Binary files /dev/null and b/dist/.DS_Store differ diff --git a/dist/utils/Parser.js b/dist/utils/Parser.js index 8fc19f5..329adc5 100644 --- a/dist/utils/Parser.js +++ b/dist/utils/Parser.js @@ -1,45 +1,60 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.parse = void 0; -const parse = (input) => { - if (input.length > 0) { - let arr = input - .map((x, index) => (x.includes("\t") ? null : index)) - .filter((x) => x !== null); - return arr - .map((item, index) => { - return index + 1 < arr.length - ? [item, arr[index + 1]] - : [item, input.length]; - }) - .filter((x) => x !== undefined) - .map((range) => { - if (range) { - if (range[0] === range[1]) { - return { - name: input[range[0]], - children: [], - }; - } - else { - return { - name: input[range[0]], - children: (0, exports.parse)(input - .slice(range[0] + 1, range[1]) - .map((x) => x.substring(1))), - }; - } +function removeCircularReferences(node) { + const visitedNodes = new Set(); + + function removeCircular(node) { + if (visitedNodes.has(node)) { + // Circular reference found, remove it + return null; + } + + visitedNodes.add(node); + + node.children = node.children.map(child => removeCircular(child)).filter(Boolean); + + return node; + } + + return removeCircular(node); +} + +function parse(lines) { + let tree = null; + let currentParent = null; + let currentLevel = -1; + const stack = []; + + lines.forEach(line => { + const level = line.search(/\S|$/); // Find the first non-whitespace character + const content = line.trim(); + const node = { content: content, children: [] }; + + if (!tree) { + tree = node; + currentParent = tree; + } else if (level > currentLevel) { + stack.push({ level: currentLevel, node: currentParent }); + currentParent = currentParent.children[currentParent.children.length - 1] || currentParent; + currentLevel = level; + } else if (level < currentLevel) { + while (stack.length > 0 && stack[stack.length - 1].level >= level) { + stack.pop(); } - else { - return { - name: "error", - children: [], - }; + if (stack.length > 0) { + currentParent = stack[stack.length - 1].node; + currentLevel = stack[stack.length - 1].level; } - }); - } - else { - return []; - } -}; -exports.parse = parse; + } + + if (content !== "") { + currentParent.children.push(node); + stack.push({ level: level, node: node }); + } + }); + + // Remove circular references + tree = removeCircularReferences(tree); + + return tree; +} + +module.exports = { parse }; diff --git a/dist/utils/Renderer.js b/dist/utils/Renderer.js index 99ec436..6da857e 100644 --- a/dist/utils/Renderer.js +++ b/dist/utils/Renderer.js @@ -1,173 +1,191 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.render = void 0; -const render = (data) => ` - -
- -