Compare commits
3 Commits
99f5d1e660
...
ee5ed3e075
Author | SHA1 | Date | |
---|---|---|---|
ee5ed3e075 | |||
ee4bd41d9c | |||
832bc08dc7 |
@ -6,6 +6,9 @@ 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\FencedCode;
|
||||||
|
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;
|
||||||
|
|
||||||
@ -22,6 +25,7 @@ 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
|
||||||
|
|
||||||
|
$blockquote = 0;
|
||||||
$block_level = 0;
|
$block_level = 0;
|
||||||
$list_item = 0;
|
$list_item = 0;
|
||||||
|
|
||||||
@ -41,6 +45,8 @@ class ArticleParser extends Parser {
|
|||||||
if ($node instanceof ListBlock) {
|
if ($node instanceof ListBlock) {
|
||||||
$block_level -= 1;
|
$block_level -= 1;
|
||||||
$list_item = 0;
|
$list_item = 0;
|
||||||
|
} elseif ($node instanceof BlockQuote) {
|
||||||
|
$blockquote -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@ -52,6 +58,37 @@ class ArticleParser extends Parser {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($node instanceof BlockQuote) {
|
||||||
|
$blockquote += 1;
|
||||||
|
$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 FencedCode) {
|
||||||
|
$leader = $previous_node ? "\n" : "";
|
||||||
|
|
||||||
|
$gemtext[] = $leader . "```";
|
||||||
|
$gemtext[] = $node->getLiteral() . '```'; // allow trailing slash from literal
|
||||||
|
|
||||||
|
$previous_node = $node;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($node instanceof Text) {
|
if ($node instanceof Text) {
|
||||||
$text = $node->getLiteral();
|
$text = $node->getLiteral();
|
||||||
|
|
||||||
@ -69,6 +106,8 @@ class ArticleParser extends Parser {
|
|||||||
|
|
||||||
$leader = $list_item === 1 ? "\n" : "";
|
$leader = $list_item === 1 ? "\n" : "";
|
||||||
$gemtext[] = $leader . '* ' . $text;
|
$gemtext[] = $leader . '* ' . $text;
|
||||||
|
} elseif ($blockquote) {
|
||||||
|
$gemtext[] = '> ' . $text;
|
||||||
} else {
|
} else {
|
||||||
$leader = $previous_node ? "\n" : "";
|
$leader = $previous_node ? "\n" : "";
|
||||||
$gemtext[] = $leader . $text;
|
$gemtext[] = $leader . $text;
|
||||||
|
@ -101,4 +101,22 @@ class ArticleParserTest extends TestCase {
|
|||||||
$this->parser->load(vfsStream::url('root/article.md'));
|
$this->parser->load(vfsStream::url('root/article.md'));
|
||||||
$this->assertSame("para\n\n* item 1\n* item 2\n\nout", $this->parser->convert());
|
$this->assertSame("para\n\n* item 1\n* item 2\n\nout", $this->parser->convert());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testBlockquote() {
|
||||||
|
$this->setArticleText("- before\n- > this is a quote\n it spans 2 lines\n- more");
|
||||||
|
$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());
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPreformatted() {
|
||||||
|
$this->setArticleText("- before\n- ```\n one\n two\n ```\n- after");
|
||||||
|
$this->parser->load(vfsStream::url('root/article.md'));
|
||||||
|
$this->assertSame("before\n\n```\none\ntwo\n```\n\nafter", $this->parser->convert());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user