Compare commits

...

2 Commits

3 changed files with 26 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<phpunit bootstrap="vendor/autoload.php"> <phpunit bootstrap="vendor/autoload.php">
<testsuites> <testsuites>
<testsuite name="parser"> <testsuite name="logseq2gemtext">
<file>tests/ParserTest.php</file> <directory>tests</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>

View File

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

View File

@ -95,4 +95,10 @@ class ArticleParserTest extends TestCase {
$this->parser->load(vfsStream::url('root/article.md')); $this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("=> bar foo\n\n=> foo bar", $this->parser->convert()); $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());
}
} }