Created
December 25, 2020 22:12
-
-
Save evan-brass/652daa90aa6cac97872d09cdde472226 to your computer and use it in GitHub Desktop.
Step 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_content(cursor) { | |
let run = true; | |
while (run && input.length > 0) { | |
// Parse an open tag | |
const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => { | |
const new_tag = { tag, attributes: {}, children: [] }; | |
cursor.children.push(new_tag); | |
parse_content(new_tag); | |
}) || | |
// Parse close tag | |
pull(/^<\/([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => { | |
if (cursor.tag !== tag) { | |
throw new Error("Unmatched close tag"); | |
} | |
run = false; | |
}); | |
if (!success) { | |
throw new Error("Parse error"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment