Add support for Gemtext headings. Fixes #2

This commit is contained in:
Annika Backstrom 2022-05-24 22:39:32 +01:00
parent 832bc08dc7
commit ee4bd41d9c
2 changed files with 23 additions and 0 deletions

View File

@ -7,6 +7,7 @@ namespace LogseqGem;
use League\CommonMark\Node\Inline\Text; use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link; use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote; use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
@ -61,6 +62,22 @@ class ArticleParser extends Parser {
$gemtext[] = ''; $gemtext[] = '';
} }
if ($node instanceof Heading) {
$next = $walker->next();
$text = $next->getNode();
if (!$text instanceof Text) {
throw new \Exception('Expected Heading to contain a Text node, got ' . get_class($text));
}
$leader = $previous_node ? "\n" : "";
$gemtext[] = $leader . str_repeat('#', $node->getLevel()) . ' ' . $text->getLiteral();
$previous_node = $node;
continue;
}
if ($node instanceof Text) { if ($node instanceof Text) {
$text = $node->getLiteral(); $text = $node->getLiteral();

View File

@ -107,4 +107,10 @@ class ArticleParserTest extends TestCase {
$this->parser->load(vfsStream::url('root/article.md')); $this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("before\n\n> this is a quote\n> it spans 2 lines\n\nmore", $this->parser->convert()); $this->assertSame("before\n\n> this is a quote\n> it spans 2 lines\n\nmore", $this->parser->convert());
} }
public function testHeadings() {
$this->setArticleText("- before\n- # h1\n- ## h2\n- ### h3\n- after");
$this->parser->load(vfsStream::url('root/article.md'));
$this->assertSame("before\n\n# h1\n\n## h2\n\n### h3\n\nafter", $this->parser->convert());
}
} }