Add paragraph and link support to ArticleParser

This commit is contained in:
Annika Backstrom 2022-05-21 18:44:11 +01:00
parent 2c7901d5a9
commit e3e27708ef
2 changed files with 30 additions and 1 deletions

View File

@ -4,5 +4,35 @@ declare(strict_types=1);
namespace LogseqGem;
use League\CommonMark\Node\NodeWalker;
use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
class ArticleParser extends Parser {
public function convert() {
$gemtext = [];
$link = 0;
$walker = $this->document->walker();
while ($event = $walker->next()) {
$entering = $event->isEntering();
$node = $event->getNode();
if ($entering && $node instanceof Text) {
$gemtext[] = $node->getLiteral();
}
if ($entering && $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());
}
}
return implode("\n\n", $gemtext);
}
}

View File

@ -8,7 +8,6 @@ use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use League\CommonMark\Node\Block\Document;
use LogseqGem\GemlogParser;
class GemlogParserTest extends TestCase {