From efb30ac1727426d23d4a2ca405889f41e5fabc1f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 17:10:07 +0200 Subject: [PATCH 01/21] Extract complex constructor logic to helper method --- packages/framework/src/Actions/SourceFileParser.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 17f7fa102f0..1885afbe16b 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -28,10 +28,14 @@ class SourceFileParser public function __construct(string $pageClass, string $slug) { $this->validateExistence($pageClass, $slug); - $this->slug = $slug; - $this->page = $pageClass === BladePage::class + $this->page = $this->constructBaseModel($pageClass); + } + + protected function constructBaseModel(string $pageClass): BladePage|AbstractMarkdownPage + { + return $pageClass === BladePage::class ? $this->parseBladePage() : $this->parseMarkdownPage($pageClass); } @@ -63,4 +67,5 @@ public function get(): PageContract { return $this->page; } + } From 564a2de34065b4583760d6d1c38c814483831285 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 17:28:36 +0200 Subject: [PATCH 02/21] Find the page title in parser --- .../src/Actions/SourceFileParser.php | 35 ++++++++++++++++++- .../tests/Feature/SourceFileParserTest.php | 23 ++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 1885afbe16b..aa200d81e38 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -5,6 +5,7 @@ use Hyde\Framework\Concerns\ValidatesExistence; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Contracts\PageContract; +use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Modules\Markdown\MarkdownFileParser; @@ -31,6 +32,7 @@ public function __construct(string $pageClass, string $slug) $this->slug = $slug; $this->page = $this->constructBaseModel($pageClass); + $this->constructDynamicData(); } protected function constructBaseModel(string $pageClass): BladePage|AbstractMarkdownPage @@ -58,11 +60,42 @@ protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage return new $pageClass( matter: $matter, body: $body, - title: FindsTitleForDocument::get($this->slug, $matter, $body), slug: $this->slug ); } + protected function constructDynamicData(): void + { + $this->page->title = $this->findTitleForPage(); + } + + protected function findTitleForPage(): string + { + if ($this->page instanceof BladePage) { + return Hyde::makeTitle($this->slug); + } + + if ($this->page->matter('title')) { + return $this->page->matter('title'); + } + + return $this->findTitleTagInMarkdown() ?: Hyde::makeTitle($this->slug); + } + + /** Attempt to find the title based on the first H1 tag. */ + protected function findTitleTagInMarkdown(): string|false + { + $lines = explode("\n", $this->page->markdown()); + + foreach ($lines as $line) { + if (str_starts_with($line, '# ')) { + return trim(substr($line, 2), ' '); + } + } + + return false; + } + public function get(): PageContract { return $this->page; diff --git a/packages/framework/tests/Feature/SourceFileParserTest.php b/packages/framework/tests/Feature/SourceFileParserTest.php index 3b6befb5ab1..588392138c8 100644 --- a/packages/framework/tests/Feature/SourceFileParserTest.php +++ b/packages/framework/tests/Feature/SourceFileParserTest.php @@ -59,4 +59,27 @@ public function test_documentation_page_parser() $this->assertEquals('# Foo Bar', $page->body); $this->assertEquals('Foo Bar Baz', $page->title); } + + public function test_dynamic_data_constructor_can_find_title_from_front_matter() + { + $this->markdown('_pages/foo.md', '# Foo Bar', ['title' => 'My Title']); + $page = MarkdownPage::parse('foo'); + $this->assertEquals('My Title', $page->title); + } + + public function test_dynamic_data_constructor_can_find_title_from_h1_tag() + { + $this->markdown('_pages/foo.md', '# Foo Bar'); + $page = MarkdownPage::parse('foo'); + + $this->assertEquals('Foo Bar', $page->title); + } + + public function test_dynamic_data_constructor_can_find_title_from_slug() + { + $this->markdown('_pages/foo-bar.md'); + $page = MarkdownPage::parse('foo-bar'); + + $this->assertEquals('Foo Bar', $page->title); + } } From 97c14a0cdbb990a40ef833c17f68abc14a8ddad2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 3 Aug 2022 15:29:45 +0000 Subject: [PATCH 03/21] Apply fixes from StyleCI --- packages/framework/src/Actions/SourceFileParser.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index aa200d81e38..df3805adbe2 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -100,5 +100,4 @@ public function get(): PageContract { return $this->page; } - } From b9d9a51cf1073fbe1f4b6f5c04a6ecf355140283 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 17:32:53 +0200 Subject: [PATCH 04/21] Delete FindsTitleForDocument.php --- .../src/Actions/FindsTitleForDocument.php | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 packages/framework/src/Actions/FindsTitleForDocument.php diff --git a/packages/framework/src/Actions/FindsTitleForDocument.php b/packages/framework/src/Actions/FindsTitleForDocument.php deleted file mode 100644 index aec60605aef..00000000000 --- a/packages/framework/src/Actions/FindsTitleForDocument.php +++ /dev/null @@ -1,35 +0,0 @@ - Date: Wed, 3 Aug 2022 17:54:37 +0200 Subject: [PATCH 05/21] Use arrayable method --- packages/framework/src/Actions/SourceFileParser.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index df3805adbe2..2b0bdfb9767 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -85,9 +85,7 @@ protected function findTitleForPage(): string /** Attempt to find the title based on the first H1 tag. */ protected function findTitleTagInMarkdown(): string|false { - $lines = explode("\n", $this->page->markdown()); - - foreach ($lines as $line) { + foreach ($this->page->markdown()->toArray() as $line) { if (str_starts_with($line, '# ')) { return trim(substr($line, 2), ' '); } From e94e225838d09cc9fd6f39e3cc6b21f2bd345b97 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 17:58:29 +0200 Subject: [PATCH 06/21] Remove trait HasDynamicTitle --- RELEASE_NOTES.md | 1 + .../src/Concerns/HasDynamicTitle.php | 45 ------------------- .../src/Contracts/AbstractMarkdownPage.php | 5 --- .../framework/src/Services/RssFeedService.php | 2 +- .../tests/Feature/AbstractPageTest.php | 6 --- .../tests/Unit/HasDynamicTitleTest.php | 38 ---------------- 6 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 packages/framework/tests/Unit/HasDynamicTitleTest.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 63a9035a0e4..6e737d21350 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -21,6 +21,7 @@ This update refactors the internal page source model parsing. This will likely n ### Removed - The PageParserContract interface, and all of its implementations have been removed - Removed `$localPath` property from DocumentationPage class, see above +- Removed trait HasDynamicTitle ### Fixed - for any bug fixes. diff --git a/packages/framework/src/Concerns/HasDynamicTitle.php b/packages/framework/src/Concerns/HasDynamicTitle.php index ed576ddb5b6..92c80524c46 100644 --- a/packages/framework/src/Concerns/HasDynamicTitle.php +++ b/packages/framework/src/Concerns/HasDynamicTitle.php @@ -3,48 +3,3 @@ namespace Hyde\Framework\Concerns; use Hyde\Framework\Hyde; - -/** - * Find and get the title to use for a Markdown Document. - * - * First check the front matter for a title. If one is not found, - * it searches the Markdown for a level one heading. Falls back to - * generating a title from the slug if no other title could be found. - * - * @deprecated Move to action to generate when constructing a page. - */ -trait HasDynamicTitle -{ - public function constructDynamicTitle(): void - { - if (! isset($this->title) || $this->title === '') { - $this->title = $this->findTitleForDocument(); - } - } - - public function findTitleForDocument(): string - { - if (isset($this->matter['title'])) { - return $this->matter['title']; - } - - return $this->findTitleTagInMarkdown($this->body) - ?: Hyde::makeTitle($this->slug); - } - - /** - * Attempt to find the title based on the first H1 tag. - */ - protected function findTitleTagInMarkdown(string $stream): string|false - { - $lines = explode("\n", $stream); - - foreach ($lines as $line) { - if (str_starts_with($line, '# ')) { - return trim(substr($line, 2), ' '); - } - } - - return false; - } -} diff --git a/packages/framework/src/Contracts/AbstractMarkdownPage.php b/packages/framework/src/Contracts/AbstractMarkdownPage.php index 12cf1419e41..4511637c400 100644 --- a/packages/framework/src/Contracts/AbstractMarkdownPage.php +++ b/packages/framework/src/Contracts/AbstractMarkdownPage.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Contracts; -use Hyde\Framework\Concerns\HasDynamicTitle; use Hyde\Framework\Facades\Markdown; use Hyde\Framework\Models\MarkdownDocument; @@ -22,8 +21,6 @@ */ abstract class AbstractMarkdownPage extends AbstractPage { - use HasDynamicTitle; - public MarkdownDocument $markdown; public array $matter; @@ -41,8 +38,6 @@ public function __construct(array $matter = [], string $body = '', string $title $this->slug = $slug; $this->markdown = $markdownDocument ?? new MarkdownDocument($matter, $body); - - $this->constructDynamicTitle(); } public function markdown(): MarkdownDocument diff --git a/packages/framework/src/Services/RssFeedService.php b/packages/framework/src/Services/RssFeedService.php index f3716a45c1c..6c6a151d832 100644 --- a/packages/framework/src/Services/RssFeedService.php +++ b/packages/framework/src/Services/RssFeedService.php @@ -51,7 +51,7 @@ public function getXML(): string|false protected function addItem(MarkdownPost $post): void { $item = $this->feed->channel->addChild('item'); - $item->addChild('title', $post->findTitleForDocument()); + $item->addChild('title', $post->title); $item->addChild('link', $post->getCanonicalLink()); $item->addChild('guid', $post->getCanonicalLink()); $item->addChild('description', $post->getPostDescription()); diff --git a/packages/framework/tests/Feature/AbstractPageTest.php b/packages/framework/tests/Feature/AbstractPageTest.php index 53c98deb568..54a54a7bf2b 100644 --- a/packages/framework/tests/Feature/AbstractPageTest.php +++ b/packages/framework/tests/Feature/AbstractPageTest.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Testing\Feature; -use Hyde\Framework\Concerns\HasDynamicTitle; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Contracts\AbstractPage; use Hyde\Framework\Contracts\PageContract; @@ -237,11 +236,6 @@ public function test_abstract_markdown_page_implements_page_contract() $this->assertInstanceOf(PageContract::class, new class extends AbstractMarkdownPage {}); } - public function test_abstract_markdown_page_uses_has_dynamic_title_trait() - { - $this->assertContains(HasDynamicTitle::class, class_uses_recursive(AbstractMarkdownPage::class)); - } - public function test_abstract_markdown_page_has_markdown_document_property() { $this->assertClassHasAttribute('markdown', AbstractMarkdownPage::class); diff --git a/packages/framework/tests/Unit/HasDynamicTitleTest.php b/packages/framework/tests/Unit/HasDynamicTitleTest.php deleted file mode 100644 index 476e182d2d2..00000000000 --- a/packages/framework/tests/Unit/HasDynamicTitleTest.php +++ /dev/null @@ -1,38 +0,0 @@ - 'My Title', - ], body: ''); - - $this->assertEquals('My Title', $document->findTitleForDocument()); - } - - public function test_can_find_title_from_h1_tag() - { - $document = new MarkdownPage([], body: '# My Title'); - - $this->assertEquals('My Title', $document->findTitleForDocument()); - } - - public function test_can_find_title_from_slug() - { - $document = new MarkdownPage([], body: '', slug: 'my-title'); - $this->assertEquals('My Title', $document->findTitleForDocument()); - } -} From f9715f4c138de88730dc4f6e44c39fa30215c515 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 3 Aug 2022 16:40:37 +0000 Subject: [PATCH 07/21] Apply fixes from StyleCI --- packages/framework/src/Concerns/HasDynamicTitle.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/src/Concerns/HasDynamicTitle.php b/packages/framework/src/Concerns/HasDynamicTitle.php index 92c80524c46..0914d9870ab 100644 --- a/packages/framework/src/Concerns/HasDynamicTitle.php +++ b/packages/framework/src/Concerns/HasDynamicTitle.php @@ -1,5 +1,3 @@ Date: Wed, 3 Aug 2022 19:51:39 +0200 Subject: [PATCH 08/21] Add backwards compatibility to assign title from constructor frontmatter --- packages/framework/src/Contracts/AbstractMarkdownPage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Contracts/AbstractMarkdownPage.php b/packages/framework/src/Contracts/AbstractMarkdownPage.php index 4511637c400..970eced2ece 100644 --- a/packages/framework/src/Contracts/AbstractMarkdownPage.php +++ b/packages/framework/src/Contracts/AbstractMarkdownPage.php @@ -30,11 +30,11 @@ abstract class AbstractMarkdownPage extends AbstractPage public static string $fileExtension = '.md'; - public function __construct(array $matter = [], string $body = '', string $title = '', string $slug = '', ?MarkdownDocument $markdownDocument = null) + public function __construct(array $matter = [], string $body = '', ?string $title = null, string $slug = '', ?MarkdownDocument $markdownDocument = null) { $this->matter = $matter; $this->body = $body; - $this->title = $title; + $this->title = $title ?? $matter['title'] ?? ''; $this->slug = $slug; $this->markdown = $markdownDocument ?? new MarkdownDocument($matter, $body); From 9e7803c92ec88e59294fceb5a93581a6d7886de5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 19:47:42 +0200 Subject: [PATCH 09/21] Parser now infers title automatically --- RELEASE_NOTES.md | 1 + packages/framework/tests/Feature/AbstractPageTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 455459519bf..13d9bf912fc 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -13,6 +13,7 @@ This update refactors the internal page source model parsing. This will likely n - Blog post front matter no longer includes merged slug - MarkdownDocument now implements the `Arrayable` interface - Markdown page models no longer includes the slug merged into the front matter +- All Markdown page models now have the title property inferred when parsing - internal: The DocumentationPage slug now behaves like other pages, and the basename is produced at runtime, see below - internal: Refactor search index generator to use route system diff --git a/packages/framework/tests/Feature/AbstractPageTest.php b/packages/framework/tests/Feature/AbstractPageTest.php index 54a54a7bf2b..6dbc988149e 100644 --- a/packages/framework/tests/Feature/AbstractPageTest.php +++ b/packages/framework/tests/Feature/AbstractPageTest.php @@ -85,7 +85,7 @@ public function test_all_returns_collection_of_all_source_files_parsed_into_the_ { Hyde::touch(('_pages/foo.md')); $this->assertEquals( - collect([new MarkdownPage([], '', '', 'foo')]), + collect([new MarkdownPage([], '', 'Foo', 'foo')]), MarkdownPage::all() ); unlink(Hyde::path('_pages/foo.md')); From 90436f3a5ce1a659a9822617f9774947cc798ce7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:01:42 +0200 Subject: [PATCH 10/21] Use the already parsed title --- .../src/Actions/GeneratesDocumentationSearchIndexFile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Actions/GeneratesDocumentationSearchIndexFile.php b/packages/framework/src/Actions/GeneratesDocumentationSearchIndexFile.php index dea289f46b6..a1b9f234b58 100644 --- a/packages/framework/src/Actions/GeneratesDocumentationSearchIndexFile.php +++ b/packages/framework/src/Actions/GeneratesDocumentationSearchIndexFile.php @@ -63,7 +63,7 @@ public function generatePageObject(DocumentationPage $page): object { return (object) [ 'slug' => $page->slug, - 'title' => trim($page->findTitleForDocument()), + 'title' => $page->title, 'content' => trim($this->getSearchContentForDocument($page)), 'destination' => $this->getDestinationForSlug($page->slug), ]; From 18215298480955a79c1df0ece69f2fbae5d52808 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:06:31 +0200 Subject: [PATCH 11/21] Update history --- tests/Benchmarks/SearchIndexBenchmark.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Benchmarks/SearchIndexBenchmark.php b/tests/Benchmarks/SearchIndexBenchmark.php index b52eb82e11e..73e8624de20 100644 --- a/tests/Benchmarks/SearchIndexBenchmark.php +++ b/tests/Benchmarks/SearchIndexBenchmark.php @@ -7,7 +7,7 @@ class SearchIndexBenchmark extends BenchCase { /** - * Results history: + * Results history: (with xdebug) * #5555b676b 'avg_iteration_time': '26.75ms' * #2d73931ad 'avg_iteration_time': '19.41ms'. */ @@ -22,7 +22,7 @@ public function testBuildSearchIndexCommandNoFiles() } /** - * Results history: + * Results history: (with xdebug) * #5555b676b 'avg_iteration_time': '120.45ms' * #2d73931ad 'avg_iteration_time': '105.91ms'. */ From db63158d55daa309444a315ad52c069e312dc59d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:12:59 +0200 Subject: [PATCH 12/21] Discover documentation page category in parser --- .../src/Actions/SourceFileParser.php | 14 ++++++++++ .../src/Models/Pages/DocumentationPage.php | 11 -------- .../tests/Feature/SourceFileParserTest.php | 23 ++++++++++++++++ .../tests/Unit/DocumentationPageTest.php | 26 ------------------- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 2b0bdfb9767..6f4bb41bcab 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -7,7 +7,9 @@ use Hyde\Framework\Contracts\PageContract; use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\BladePage; +use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Modules\Markdown\MarkdownFileParser; +use Illuminate\Support\Str; /** * Parses a source file and returns a new page model instance for it. @@ -67,6 +69,9 @@ protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage protected function constructDynamicData(): void { $this->page->title = $this->findTitleForPage(); + if ($this->page instanceof DocumentationPage) { + $this->page->category = $this->getDocumentationPageCategory(); + } } protected function findTitleForPage(): string @@ -94,6 +99,15 @@ protected function findTitleTagInMarkdown(): string|false return false; } + protected function getDocumentationPageCategory(): ?string + { + if (str_contains($this->slug, '/')) { + return Str::before($this->slug, '/'); + } + + return $this->page->matter['category'] ?? null; + } + public function get(): PageContract { return $this->page; diff --git a/packages/framework/src/Models/Pages/DocumentationPage.php b/packages/framework/src/Models/Pages/DocumentationPage.php index dab8dc2b64f..6396122a1b3 100644 --- a/packages/framework/src/Models/Pages/DocumentationPage.php +++ b/packages/framework/src/Models/Pages/DocumentationPage.php @@ -6,7 +6,6 @@ use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Contracts\RouteContract; use Hyde\Framework\Models\Route; -use Illuminate\Support\Str; class DocumentationPage extends AbstractMarkdownPage { @@ -24,16 +23,6 @@ class DocumentationPage extends AbstractMarkdownPage public function __construct(array $matter = [], string $body = '', string $title = '', string $slug = '') { parent::__construct($matter, $body, $title, $slug); - $this->category = $this->getDocumentationPageCategory(); - } - - protected function getDocumentationPageCategory(): ?string - { - if (str_contains($this->slug, '/')) { - return Str::before($this->slug, '/'); - } - - return $this->matter['category'] ?? null; } /** @inheritDoc */ diff --git a/packages/framework/tests/Feature/SourceFileParserTest.php b/packages/framework/tests/Feature/SourceFileParserTest.php index 588392138c8..73ca10c8ac7 100644 --- a/packages/framework/tests/Feature/SourceFileParserTest.php +++ b/packages/framework/tests/Feature/SourceFileParserTest.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Actions\SourceFileParser; +use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\BladePage; use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; @@ -82,4 +83,26 @@ public function test_dynamic_data_constructor_can_find_title_from_slug() $this->assertEquals('Foo Bar', $page->title); } + + + public function test_documentation_page_parser_can_get_category_from_front_matter() + { + $this->markdown('_docs/foo.md', '# Foo Bar', ['category' => 'foo']); + + $page = DocumentationPage::parse('foo'); + $this->assertEquals('foo', $page->category); + } + + public function test_documentation_page_parser_can_get_category_automatically_from_nested_page() + { + mkdir(Hyde::path('_docs/foo')); + touch(Hyde::path('_docs/foo/bar.md')); + + /** @var DocumentationPage $page */ + $page = DocumentationPage::parse('foo/bar'); + $this->assertEquals('foo', $page->category); + + unlink(Hyde::path('_docs/foo/bar.md')); + rmdir(Hyde::path('_docs/foo')); + } } diff --git a/packages/framework/tests/Unit/DocumentationPageTest.php b/packages/framework/tests/Unit/DocumentationPageTest.php index d99fea315fb..2d75a40b5c5 100644 --- a/packages/framework/tests/Unit/DocumentationPageTest.php +++ b/packages/framework/tests/Unit/DocumentationPageTest.php @@ -129,30 +129,4 @@ public function test_compiled_pages_originating_in_subdirectories_get_output_to_ $page = (new DocumentationPage([], '', '', 'foo/bar')); $this->assertEquals('docs/bar.html', $page->getOutputPath()); } - - public function test_documentation_page_parser_can_get_category_from_front_matter() - { - $this->markdown('_docs/foo.md', '# Foo Bar', ['category' => 'foo']); - - $parser = new SourceFileParser(DocumentationPage::class, 'foo'); - - /** @var DocumentationPage $page */ - $page = $parser->get(); - $this->assertEquals('foo', $page->category); - } - - public function test_documentation_page_parser_can_get_category_automatically_from_nested_page() - { - mkdir(Hyde::path('_docs/foo')); - touch(Hyde::path('_docs/foo/bar.md')); - - $parser = new SourceFileParser(DocumentationPage::class, 'foo/bar'); - - /** @var DocumentationPage $page */ - $page = $parser->get(); - $this->assertEquals('foo', $page->category); - - unlink(Hyde::path('_docs/foo/bar.md')); - rmdir(Hyde::path('_docs/foo')); - } } From 1d456d33bdb139ca15b527239f620445a9ad44e5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:14:00 +0200 Subject: [PATCH 13/21] Replace null coalesce with helper method call --- packages/framework/src/Actions/SourceFileParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 6f4bb41bcab..5fd863c57cb 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -105,7 +105,7 @@ protected function getDocumentationPageCategory(): ?string return Str::before($this->slug, '/'); } - return $this->page->matter['category'] ?? null; + return $this->page->matter('category'); } public function get(): PageContract From 2614965801bedaaed16972c5412d4cd75b365ae3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:14:29 +0200 Subject: [PATCH 14/21] Simplify 'if' with ternary operator --- packages/framework/src/Actions/SourceFileParser.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 5fd863c57cb..77fbeea58d6 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -101,11 +101,9 @@ protected function findTitleTagInMarkdown(): string|false protected function getDocumentationPageCategory(): ?string { - if (str_contains($this->slug, '/')) { - return Str::before($this->slug, '/'); - } - - return $this->page->matter('category'); + return str_contains($this->slug, '/') + ? Str::before($this->slug, '/') + : $this->page->matter('category'); } public function get(): PageContract From cc15a16df6f3f547d2db27a5cde735834e3ece8f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:15:29 +0200 Subject: [PATCH 15/21] Document code behaviour --- packages/framework/src/Actions/SourceFileParser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 77fbeea58d6..1e82838563c 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -101,6 +101,7 @@ protected function findTitleTagInMarkdown(): string|false protected function getDocumentationPageCategory(): ?string { + // If the documentation page is in a subdirectory, we use that as the category name return str_contains($this->slug, '/') ? Str::before($this->slug, '/') : $this->page->matter('category'); From ad8a66cc98cfe97dfd79d2451169c555e702ae3d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:17:53 +0200 Subject: [PATCH 16/21] Update code documentation --- packages/framework/src/Actions/SourceFileParser.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 1e82838563c..66e54488d99 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -84,12 +84,12 @@ protected function findTitleForPage(): string return $this->page->matter('title'); } - return $this->findTitleTagInMarkdown() ?: Hyde::makeTitle($this->slug); + return $this->findTitleInMarkdown() ?: Hyde::makeTitle($this->slug); } - /** Attempt to find the title based on the first H1 tag. */ - protected function findTitleTagInMarkdown(): string|false + protected function findTitleInMarkdown(): string|false { + // Attempt to find the title based on the first H1 tag. foreach ($this->page->markdown()->toArray() as $line) { if (str_starts_with($line, '# ')) { return trim(substr($line, 2), ' '); @@ -101,9 +101,11 @@ protected function findTitleTagInMarkdown(): string|false protected function getDocumentationPageCategory(): ?string { - // If the documentation page is in a subdirectory, we use that as the category name + // If the documentation page is in a subdirectory return str_contains($this->slug, '/') + // Then we can use that as the category name ? Str::before($this->slug, '/') + // Otherwise, we search in front matter, falling back to null : $this->page->matter('category'); } From 4dce275b5ddd7ef625805dfd5083f9831a5ab6ce Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:19:19 +0200 Subject: [PATCH 17/21] Add some spacing --- packages/framework/src/Actions/SourceFileParser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 66e54488d99..6a201107b5c 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -69,6 +69,7 @@ protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage protected function constructDynamicData(): void { $this->page->title = $this->findTitleForPage(); + if ($this->page instanceof DocumentationPage) { $this->page->category = $this->getDocumentationPageCategory(); } From f3d6a0734278dd23ca543c79d17f040c84e8ccad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:21:55 +0200 Subject: [PATCH 18/21] Format comments --- packages/framework/src/Actions/SourceFileParser.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 6a201107b5c..636439f75fb 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -102,11 +102,12 @@ protected function findTitleInMarkdown(): string|false protected function getDocumentationPageCategory(): ?string { - // If the documentation page is in a subdirectory + // If the documentation page is in a subdirectory, + // then we can use that as the category name. + // Otherwise, we look in the front matter. + return str_contains($this->slug, '/') - // Then we can use that as the category name ? Str::before($this->slug, '/') - // Otherwise, we search in front matter, falling back to null : $this->page->matter('category'); } From b715bbb845da76abaac403ec1c98e1629cb31729 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:22:52 +0200 Subject: [PATCH 19/21] Replace comment with more descriptive method name --- packages/framework/src/Actions/SourceFileParser.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 636439f75fb..82fa380dad9 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -85,12 +85,11 @@ protected function findTitleForPage(): string return $this->page->matter('title'); } - return $this->findTitleInMarkdown() ?: Hyde::makeTitle($this->slug); + return $this->findTitleFromMarkdownHeadings() ?: Hyde::makeTitle($this->slug); } - protected function findTitleInMarkdown(): string|false + protected function findTitleFromMarkdownHeadings(): string|false { - // Attempt to find the title based on the first H1 tag. foreach ($this->page->markdown()->toArray() as $line) { if (str_starts_with($line, '# ')) { return trim(substr($line, 2), ' '); From e377f2aa0a0cd7c060af4c1f2f1e73fa2c5d834e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 3 Aug 2022 20:23:21 +0200 Subject: [PATCH 20/21] Replace false return value with null --- packages/framework/src/Actions/SourceFileParser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php index 82fa380dad9..e601db5bc94 100644 --- a/packages/framework/src/Actions/SourceFileParser.php +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -85,10 +85,10 @@ protected function findTitleForPage(): string return $this->page->matter('title'); } - return $this->findTitleFromMarkdownHeadings() ?: Hyde::makeTitle($this->slug); + return $this->findTitleFromMarkdownHeadings() ?? Hyde::makeTitle($this->slug); } - protected function findTitleFromMarkdownHeadings(): string|false + protected function findTitleFromMarkdownHeadings(): ?string { foreach ($this->page->markdown()->toArray() as $line) { if (str_starts_with($line, '# ')) { @@ -96,7 +96,7 @@ protected function findTitleFromMarkdownHeadings(): string|false } } - return false; + return null; } protected function getDocumentationPageCategory(): ?string From 41ff3ef060c85ddf87c85faef3fd9202be43cace Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 3 Aug 2022 18:23:51 +0000 Subject: [PATCH 21/21] Apply fixes from StyleCI --- packages/framework/tests/Feature/SourceFileParserTest.php | 1 - packages/framework/tests/Unit/DocumentationPageTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/framework/tests/Feature/SourceFileParserTest.php b/packages/framework/tests/Feature/SourceFileParserTest.php index 73ca10c8ac7..e23710b24ef 100644 --- a/packages/framework/tests/Feature/SourceFileParserTest.php +++ b/packages/framework/tests/Feature/SourceFileParserTest.php @@ -84,7 +84,6 @@ public function test_dynamic_data_constructor_can_find_title_from_slug() $this->assertEquals('Foo Bar', $page->title); } - public function test_documentation_page_parser_can_get_category_from_front_matter() { $this->markdown('_docs/foo.md', '# Foo Bar', ['category' => 'foo']); diff --git a/packages/framework/tests/Unit/DocumentationPageTest.php b/packages/framework/tests/Unit/DocumentationPageTest.php index 2d75a40b5c5..b13fef69fe8 100644 --- a/packages/framework/tests/Unit/DocumentationPageTest.php +++ b/packages/framework/tests/Unit/DocumentationPageTest.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Testing\Unit; -use Hyde\Framework\Actions\SourceFileParser; use Hyde\Framework\Hyde; use Hyde\Framework\HydeServiceProvider; use Hyde\Framework\Models\Pages\DocumentationPage;