From 27a8e454ef199ab2536ba8adffd60f1682a7b489 Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sun, 22 May 2022 10:15:00 +0100 Subject: [PATCH] Limit to single newline between links. Fixes #1 --- src/ArticleParser.php | 25 +++++++++++++++++++------ tests/ArticleParserTest.php | 1 - 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/ArticleParser.php b/src/ArticleParser.php index f10dcb2..e83e52f 100644 --- a/src/ArticleParser.php +++ b/src/ArticleParser.php @@ -17,17 +17,24 @@ class ArticleParser extends Parser { $in_frontmatter = true; + // we only update this for significant nodes + $previous_node = null; + $walker = $this->document->walker(); while ($event = $walker->next()) { $entering = $event->isEntering(); $node = $event->getNode(); - if ($entering && $node instanceof ListBlock) { + if (!$entering) { + continue; + } + + if ($node instanceof ListBlock) { $in_frontmatter = false; continue; } - if ($entering && $node instanceof Text) { + if ($node instanceof Text) { $text = $node->getLiteral(); if ($in_frontmatter) { @@ -39,24 +46,30 @@ class ArticleParser extends Parser { } } - $gemtext[] = $text; + $leader = $previous_node ? "\n" : ""; + $gemtext[] = $leader . $text; + + $previous_node = $node; continue; } - if ($entering && $node instanceof Link) { + if ($node instanceof Link) { $label = $walker->next()->getNode(); if (!$label instanceof Text) { throw new \Exception('Expected next node in Link to be Text, got ' . get_class($label)); } - $gemtext[] = sprintf('=> %s %s', $node->getUrl(), $label->getLiteral()); + $leader = ($previous_node && !$previous_node instanceof Link) ? "\n" : ""; + $gemtext[] = $leader . sprintf("=> %s %s", $node->getUrl(), $label->getLiteral()); + + $previous_node = $node; continue; } } - return implode("\n\n", $gemtext); + return implode("\n", $gemtext); } public function getProperties(): array { diff --git a/tests/ArticleParserTest.php b/tests/ArticleParserTest.php index adfb8f7..55252dd 100644 --- a/tests/ArticleParserTest.php +++ b/tests/ArticleParserTest.php @@ -57,7 +57,6 @@ class ArticleParserTest extends TestCase { } public function testSequentialLinks() { - $this->markTestSkipped('not yet implemented'); $this->setArticleText("\n- [foo](bar)\n- [bar](foo)"); $this->parser->load(vfsStream::url('root/article.md')); $this->assertSame("=> bar foo\n=> foo bar", $this->parser->convert());