diff --git a/composer.json b/composer.json index 6ccb565..40b0f3b 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "league/commonmark": "^2.3" }, "require-dev": { - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "mikey179/vfsstream": "^1.6" } } diff --git a/composer.lock b/composer.lock index 97409ef..0b30bce 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cc27d276519ce69d017e43bec647a98a", + "content-hash": "848176d1d39c31f3ecc3bbea8204ac22", "packages": [ { "name": "dflydev/dot-access-data", @@ -688,6 +688,57 @@ ], "time": "2022-03-03T08:28:38+00:00" }, + { + "name": "mikey179/vfsstream", + "version": "v1.6.10", + "source": { + "type": "git", + "url": "https://github.com/bovigo/vfsStream.git", + "reference": "250c0825537d501e327df879fb3d4cd751933b85" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/bovigo/vfsStream/zipball/250c0825537d501e327df879fb3d4cd751933b85", + "reference": "250c0825537d501e327df879fb3d4cd751933b85", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.5|^5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6.x-dev" + } + }, + "autoload": { + "psr-0": { + "org\\bovigo\\vfs\\": "src/main/php" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Frank Kleine", + "homepage": "http://frankkleine.de/", + "role": "Developer" + } + ], + "description": "Virtual file system to mock the real file system in unit tests.", + "homepage": "http://vfs.bovigo.org/", + "support": { + "issues": "https://github.com/bovigo/vfsStream/issues", + "source": "https://github.com/bovigo/vfsStream/tree/master", + "wiki": "https://github.com/bovigo/vfsStream/wiki" + }, + "time": "2021-09-25T08:05:01+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.11.0", diff --git a/src/ArticleParser.php b/src/ArticleParser.php new file mode 100644 index 0000000..d9755f8 --- /dev/null +++ b/src/ArticleParser.php @@ -0,0 +1,8 @@ +document->iterator() as $node) { + if (!$node instanceof Text) { + continue; + } + + $text = $node->getLiteral(); + + if (substr($text, 0, 2) !== '[[' && substr($text, -2) !== ']]') { + continue; + } + + $titles[] = substr($text, 2, -2); + } + + return $titles; + } +} diff --git a/src/Parser.php b/src/Parser.php index 13c1ac5..e7a9b22 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace LogseqGem; +use League\CommonMark\Node\Block\Document; + use League\CommonMark\Parser\MarkdownParser; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; @@ -11,13 +13,17 @@ use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; abstract class Parser { private MarkdownParser $parser; + protected Document $document; + public function __construct() { $environment = new Environment(); $environment->addExtension(new CommonMarkCoreExtension()); $this->parser = new MarkdownParser($environment); } - public function parse(string $input) { - return $this->parser->parse($input); + public function load(string $path) { + $markdown = file_get_contents($path); + $this->document = $this->parser->parse($markdown); + return $this->document; } } diff --git a/tests/GemlogParserTest.php b/tests/GemlogParserTest.php new file mode 100644 index 0000000..7f141b6 --- /dev/null +++ b/tests/GemlogParserTest.php @@ -0,0 +1,29 @@ +root = vfsStream::setup(); + } + + public function testLoad() { + vfsStream::newFile('Gemlog.md') + ->withContent("- [[Test link]]\n- [[Link 2]]\n-") + ->at($this->root); + $parser = new GemlogParser; + $parser->load(vfsStream::url('root/Gemlog.md')); + $this->assertSame(['Test link', 'Link 2'], $parser->getTitles()); + } +} diff --git a/tests/ParserTest.php b/tests/ParserTest.php index cdc13c8..e1b2b2a 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -5,14 +5,26 @@ declare(strict_types=1); namespace LogseqGem\Tests; use PHPUnit\Framework\TestCase; -use LogseqGem\Parser; +use org\bovigo\vfs\vfsStream; +use org\bovigo\vfs\vfsStreamDirectory; use League\CommonMark\Node\Block\Document; +use LogseqGem\Parser; + class ParserTest extends TestCase { + private vfsStreamDirectory $root; + + public function setUp(): void { + $this->root = vfsStream::setup(); + } + public function testParse() { + vfsStream::newFile('test.md') + ->withContent("this is a test **markdown** file") + ->at($this->root); $parser = $this->getMockForAbstractClass(Parser::class); - $document = $parser->parse('**foo**'); + $document = $parser->load(vfsStream::url('root/test.md')); $this->assertInstanceOf(Document::class, $document); } }