Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
)
  • Loading branch information
coderaiser committed May 14, 2024
1 parent 98414f9 commit 9f5b4cf
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,42 @@ const parse = (input, options = {}) => {
push({ type: 'text', value });
}

flattenBlocks(stack)
markImbalancedBraces(ast);
push({ type: 'eos' });

return ast;
};

module.exports = parse;

function markImbalancedBraces({nodes}) {
// Mark imbalanced braces and brackets as invalid
for (const node of nodes) {
if (node.nodes || node.invalid)
continue;

if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';

node.invalid = true;
delete node.parent;
}
}

function flattenBlocks(stack) {
let block;
do {
block = stack.pop();

if (block.type !== 'root') {
block.nodes.forEach(node => {
if (!node.nodes) {
if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';
node.invalid = true;
}
});
if (block.type === 'root')
continue;

// get the location of the block on parent.nodes (block's siblings)
let parent = stack[stack.length - 1];
let index = parent.nodes.indexOf(block);
// replace the (invalid) block with it's nodes
parent.nodes.splice(index, 1, ...block.nodes);
}
// get the location of the block on parent.nodes (block's siblings)

This comment has been minimized.

Copy link
@carlos2martinize

carlos2martinize Sep 11, 2024

what would the number ident be for margin

let parent = stack.at(-1);
let index = parent.nodes.indexOf(block);
// replace the (invalid) block with its nodes
parent.nodes.splice(index, 1, ...block.nodes);
} while (stack.length > 0);

push({ type: 'eos' });
return ast;
};

module.exports = parse;
}

0 comments on commit 9f5b4cf

Please sign in to comment.