Add support for Gemtext lists. Fixes #3

This commit is contained in:
Annika Backstrom 2022-05-24 21:56:15 +01:00
parent 1de5e568ef
commit 577ab07e34
2 changed files with 24 additions and 2 deletions

View File

@ -22,6 +22,9 @@ class ArticleParser extends Parser {
$last_node = null; //updated every time
$previous_node = null; // we only update this for significant nodes
$block_level = 0;
$list_item = 0;
$walker = $this->document->walker();
while ($event = $walker->next()) {
$entering = $event->isEntering();
@ -35,10 +38,16 @@ class ArticleParser extends Parser {
}
if (!$entering) {
if ($node instanceof ListBlock) {
$block_level -= 1;
$list_item = 0;
}
continue;
}
if ($node instanceof ListBlock) {
$block_level += 1;
$in_frontmatter = false;
continue;
}
@ -55,8 +64,15 @@ class ArticleParser extends Parser {
}
}
if ($block_level > 1) {
$list_item += 1;
$leader = $list_item === 1 ? "\n" : "";
$gemtext[] = $leader . '* ' . $text;
} else {
$leader = $previous_node ? "\n" : "";
$gemtext[] = $leader . $text;
}
$previous_node = $node;

View File

@ -95,4 +95,10 @@ class ArticleParserTest extends TestCase {
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("=> bar foo\n\n=> foo bar", $this->parser->convert());
}
public function testList() {
$this->setArticleText("- para\n\t- item 1\n\t- item 2\n- out");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("para\n\n* item 1\n* item 2\n\nout", $this->parser->convert());
}
}