diff --git a/src/ArticleParser.php b/src/ArticleParser.php index 2587a5b..c7411b8 100644 --- a/src/ArticleParser.php +++ b/src/ArticleParser.php @@ -7,6 +7,7 @@ namespace LogseqGem; use League\CommonMark\Node\Inline\Text; use League\CommonMark\Extension\CommonMark\Node\Inline\Link; use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote; +use League\CommonMark\Extension\CommonMark\Node\Block\Heading; use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; @@ -61,6 +62,22 @@ class ArticleParser extends Parser { $gemtext[] = ''; } + if ($node instanceof Heading) { + $next = $walker->next(); + $text = $next->getNode(); + + if (!$text instanceof Text) { + throw new \Exception('Expected Heading to contain a Text node, got ' . get_class($text)); + } + + $leader = $previous_node ? "\n" : ""; + $gemtext[] = $leader . str_repeat('#', $node->getLevel()) . ' ' . $text->getLiteral(); + + $previous_node = $node; + + continue; + } + if ($node instanceof Text) { $text = $node->getLiteral(); diff --git a/tests/ArticleParserTest.php b/tests/ArticleParserTest.php index bf3b999..09ca982 100644 --- a/tests/ArticleParserTest.php +++ b/tests/ArticleParserTest.php @@ -107,4 +107,10 @@ class ArticleParserTest extends TestCase { $this->parser->load(vfsStream::url('root/article.md')); $this->assertSame("before\n\n> this is a quote\n> it spans 2 lines\n\nmore", $this->parser->convert()); } + + public function testHeadings() { + $this->setArticleText("- before\n- # h1\n- ## h2\n- ### h3\n- after"); + $this->parser->load(vfsStream::url('root/article.md')); + $this->assertSame("before\n\n# h1\n\n## h2\n\n### h3\n\nafter", $this->parser->convert()); + } }