(FEAT): Heading parser uses a new inline method.

The only difference between this and the original one is the exit
condition.
This commit is contained in:
Hayden Hargreaves 2025-10-22 12:03:02 -07:00
parent d863b93132
commit e6dc318306
3 changed files with 63 additions and 16 deletions

View File

@ -15,4 +15,8 @@ this is too far`
## **Hello world**
### hello world
### hello *world*
# ***This is both!***
###### This is neither

View File

@ -108,25 +108,15 @@ std::unique_ptr<Node> Parser::ParseHeading() {
ConsumeWhiteSpace();
std::string str;
while (!IsEOF()) {
c = Peek();
// We can stop as soon as we see a new line. Headings are single line blocks
if (c == '\n')
break;
// If a newline, use a space instead
str += c;
Consume();
// This should call parse inline
auto text_nodes = ParseInline();
for (auto &text_node : text_nodes) {
node->AddChild(std::move(text_node));
}
// BUG: Why do we need to check this?
if (str == "")
if (node->IsEmpty())
return nullptr;
auto text_node = std::make_unique<TextNode>(str);
node->AddChild(std::move(text_node));
return node;
}
@ -179,6 +169,54 @@ vector<std::unique_ptr<Node>> Parser::ParseInline() {
return nodes;
}
vector<std::unique_ptr<Node>> Parser::ParseInlineHeading() {
vector<std::unique_ptr<Node>> nodes;
string str;
while (!IsEOF()) {
char c = Peek();
// We can stop as soon as we see a new line. Headings are single line blocks
if (c == '\n')
break;
if (c == '*' && Peek(1) == '*' && Peek(2) == '*') {
PushTextNode(nodes, str);
auto node = ParseBoldItalic();
if (!node->IsEmpty())
nodes.push_back(std::move(node));
continue;
} else if (c == '*' && Peek(1) == '*') {
PushTextNode(nodes, str);
auto node = ParseBold();
if (!node->IsEmpty())
nodes.push_back(std::move(node));
continue;
} else if (c == '*') {
PushTextNode(nodes, str);
auto node = ParseItalic();
if (!node->IsEmpty())
nodes.push_back(std::move(node));
continue;
}
if (c == '`') {
PushTextNode(nodes, str);
auto node = ParseCode();
if (!node->IsEmpty())
nodes.push_back(std::move(node));
continue;
}
// If a newline, use a space instead
str += (c == '\n' ? ' ' : c);
Consume();
}
// Push the last node, if the string is not empty
PushTextNode(nodes, str);
return nodes;
}
std::unique_ptr<Node> Parser::ParseItalic() {
string str;
Consume(1);

View File

@ -117,10 +117,15 @@ private:
// Working input content
string content;
// TODO: Document these methods, no more magic methods :)
std::unique_ptr<Node> ParseParagraph();
std::unique_ptr<Node> ParseHeading();
vector<std::unique_ptr<Node>> ParseInline();
// The only difference is the exit condition
vector<std::unique_ptr<Node>> ParseInlineHeading();
void PushTextNode(vector<std::unique_ptr<Node>> &nodes, string &str);
std::unique_ptr<Node> ParseItalic();