Limit to single newline between links. Fixes #1

This commit is contained in:
Annika Backstrom 2022-05-22 10:15:00 +01:00
parent cb0e7654b6
commit 27a8e454ef
2 changed files with 19 additions and 7 deletions

View File

@ -17,17 +17,24 @@ class ArticleParser extends Parser {
$in_frontmatter = true; $in_frontmatter = true;
// we only update this for significant nodes
$previous_node = null;
$walker = $this->document->walker(); $walker = $this->document->walker();
while ($event = $walker->next()) { while ($event = $walker->next()) {
$entering = $event->isEntering(); $entering = $event->isEntering();
$node = $event->getNode(); $node = $event->getNode();
if ($entering && $node instanceof ListBlock) { if (!$entering) {
continue;
}
if ($node instanceof ListBlock) {
$in_frontmatter = false; $in_frontmatter = false;
continue; continue;
} }
if ($entering && $node instanceof Text) { if ($node instanceof Text) {
$text = $node->getLiteral(); $text = $node->getLiteral();
if ($in_frontmatter) { if ($in_frontmatter) {
@ -39,24 +46,30 @@ class ArticleParser extends Parser {
} }
} }
$gemtext[] = $text; $leader = $previous_node ? "\n" : "";
$gemtext[] = $leader . $text;
$previous_node = $node;
continue; continue;
} }
if ($entering && $node instanceof Link) { if ($node instanceof Link) {
$label = $walker->next()->getNode(); $label = $walker->next()->getNode();
if (!$label instanceof Text) { if (!$label instanceof Text) {
throw new \Exception('Expected next node in Link to be Text, got ' . get_class($label)); 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; continue;
} }
} }
return implode("\n\n", $gemtext); return implode("\n", $gemtext);
} }
public function getProperties(): array { public function getProperties(): array {

View File

@ -57,7 +57,6 @@ class ArticleParserTest extends TestCase {
} }
public function testSequentialLinks() { public function testSequentialLinks() {
$this->markTestSkipped('not yet implemented');
$this->setArticleText("\n- [foo](bar)\n- [bar](foo)"); $this->setArticleText("\n- [foo](bar)\n- [bar](foo)");
$this->parser->load(vfsStream::url('root/article.md')); $this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("=> bar foo\n=> foo bar", $this->parser->convert()); $this->assertSame("=> bar foo\n=> foo bar", $this->parser->convert());