From ee4c329b0ccd696bfe41a0d10f4dec0db14a032d Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Thu, 11 Nov 2021 19:00:27 -0600 Subject: [PATCH 1/2] feature: Markdown can generate description Could be from description front matter member or from the content of the post. --- src/Content/FrontMatter.php | 8 ++++ src/Content/Markdown.php | 44 +++++++++++++++++++ tests/FileSystemTest.php | 2 +- tests/MarkdownTest.php | 37 ++++++++++++++++ tests/SitemapTest.php | 2 +- tests/test-content/content/public/content.md | 13 ++++++ .../content/public/published-sub/content.md | 12 +++++ .../published-sub-sub/content.md | 27 ++++++++++++ 8 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 tests/MarkdownTest.php create mode 100644 tests/test-content/content/public/published-sub/published-sub-sub/content.md diff --git a/src/Content/FrontMatter.php b/src/Content/FrontMatter.php index 6b243f9b..dd0eaf95 100644 --- a/src/Content/FrontMatter.php +++ b/src/Content/FrontMatter.php @@ -84,4 +84,12 @@ public function template(): string } return ''; } + + public function description(): string + { + if ($this->hasMember('description')) { + return strval($this->frontMatter['description']); + } + return ''; + } } diff --git a/src/Content/Markdown.php b/src/Content/Markdown.php index 0f2aec36..206f221f 100644 --- a/src/Content/Markdown.php +++ b/src/Content/Markdown.php @@ -143,6 +143,50 @@ public function pageTitle(): string return implode(' | ', $titles); } + public function description(): string + { + if ($this->frontMatter()->hasMember('description')) { + $description = $this->frontMatter()->description(); + + } else { + $body = $this->body(); + $description = preg_filter("/#(.*)\n/", '', $body); + if (is_string($description)) { + $parts = explode("\n", $description); + $parts = array_filter($parts); + $description = implode(' ', $parts); + + } else { + // TODO: Doesn't guarantee meta description content. + // Log?? + $description = ''; + + } + } + + $description = htmlentities(substr($description, 0, 200)); + + $parts = explode('. ', $description); + $description = ''; + foreach ($parts as $part) { + $d = $part; + if (strlen($description) > 0) { + $d = $description . '. ' . $part; + } + + $proposedLength = strlen($d); + if ($proposedLength >= 200) { + $ps = explode('. ', $d); + array_pop($ps); + $description = implode('. ', $ps) . '.'; + break; + } + $description = $d; + } + + return $description; + } + public function file(): File { return $this->file; diff --git a/tests/FileSystemTest.php b/tests/FileSystemTest.php index 392b5cce..adc26c9d 100644 --- a/tests/FileSystemTest.php +++ b/tests/FileSystemTest.php @@ -11,7 +11,7 @@ count( TestFileSystem::init()->publishedContentFinder() ) - )->toBeInt()->toBe(2); + )->toBeInt()->toBe(3); })->group('filesystem'); it('has required folders', function() { diff --git a/tests/MarkdownTest.php b/tests/MarkdownTest.php new file mode 100644 index 00000000..84f4b734 --- /dev/null +++ b/tests/MarkdownTest.php @@ -0,0 +1,37 @@ +publicRoot(); + + // description field + $file = File::at($publicRoot . '/content.md', $fileSystem); + expect( + Markdown::for($file, $fileSystem)->description() + )->toBe( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur "iaculis" sodales turpis a pellentesque's. In ac nibh ex." + ); + + // derived description from content, short + $file = File::at($publicRoot . '/published-sub/content.md', $fileSystem); + expect( + Markdown::for($file, $fileSystem)->description() + )->toBe( + "Short sentence. Something a little bit longer. Third sentence." + ); + + // derived description from content, long + $file = File::at($publicRoot . '/published-sub/published-sub-sub/content.md', $fileSystem); + expect( + Markdown::for($file, $fileSystem)->description() + )->toBe( + "Short sentence. Something a little bit longer. Third sentence. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit." + ); +})->group('markdown'); diff --git a/tests/SitemapTest.php b/tests/SitemapTest.php index 49375aee..225e72f4 100644 --- a/tests/SitemapTest.php +++ b/tests/SitemapTest.php @@ -44,7 +44,7 @@ $xml->body() )->toBe(<< - https://joshbruce.comhttps://joshbruce.com/published-sub + https://joshbruce.comhttps://joshbruce.com/published-subhttps://joshbruce.com/published-sub/published-sub-sub xml ); })->group('request', 'response', 'sitemap'); diff --git a/tests/test-content/content/public/content.md b/tests/test-content/content/public/content.md index 8866c764..dd6d1e76 100644 --- a/tests/test-content/content/public/content.md +++ b/tests/test-content/content/public/content.md @@ -1,3 +1,16 @@ --- title: Test content root +description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur "iaculis" sodales turpis a pellentesque's. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis. --- + +# Heading in the way + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur iaculis sodales turpis a pellentesque. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis. + +Nunc sed felis a lacus convallis porttitor ut eu nulla. Morbi ac neque elit. Cras sit amet hendrerit purus. Nullam in nisi sem. Phasellus sodales condimentum velit, eget tempus urna scelerisque ullamcorper. Curabitur viverra velit sem, vel finibus justo porttitor ultricies. Ut arcu turpis, convallis at efficitur eget, aliquet sit amet dolor. Vestibulum ut sem nec purus imperdiet finibus. Vestibulum auctor tortor a lacinia faucibus. Nulla sagittis fringilla tincidunt. Fusce suscipit et diam et porta. Mauris felis justo, aliquam et neque eget, aliquet venenatis risus. Maecenas erat arcu, semper vitae massa sit amet, facilisis vulputate diam. Praesent lacinia pulvinar vestibulum. Nullam tristique augue magna, vel fermentum mauris malesuada nec. Aenean molestie odio sodales ante molestie pharetra. + +Phasellus sit amet mi vitae turpis tristique luctus. Ut pellentesque mattis nisl, rutrum accumsan mi placerat ac. Vivamus nunc ligula, malesuada non lacinia in, accumsan id sem. Proin tempus ac lacus a pulvinar. Nunc laoreet maximus sem nec scelerisque. Maecenas sem erat, faucibus non arcu sed, accumsan vehicula sapien. Nullam ipsum lorem, bibendum non arcu convallis, fringilla interdum lacus. + +Vivamus elementum elit at lectus dignissim, in commodo sem convallis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus venenatis non ante quis ullamcorper. In facilisis ultricies tempus. Proin mollis sagittis condimentum. Vestibulum ornare pretium pellentesque. In dictum sodales felis ut ornare. + +Fusce nec ullamcorper magna, ac pretium quam. Integer elementum tincidunt ligula eget tristique. Vivamus orci neque, finibus ultricies vulputate eget, rutrum at velit. Vestibulum at nisi ac est vehicula luctus vitae id mauris. Nam tempor egestas justo, a consectetur purus porta non. Curabitur suscipit odio eu enim fringilla, non elementum dui luctus. Sed nec feugiat libero. Mauris luctus nulla ornare, dignissim nulla id, rutrum nunc. diff --git a/tests/test-content/content/public/published-sub/content.md b/tests/test-content/content/public/published-sub/content.md index ef1a82a7..00c2ae8a 100644 --- a/tests/test-content/content/public/published-sub/content.md +++ b/tests/test-content/content/public/published-sub/content.md @@ -1,3 +1,15 @@ --- title: Sub-folder content title --- + +# Heading in the way + +Short sentence. + +## Heading in the way + +Something a little bit longer. + +### Heading the way + +Third sentence. diff --git a/tests/test-content/content/public/published-sub/published-sub-sub/content.md b/tests/test-content/content/public/published-sub/published-sub-sub/content.md new file mode 100644 index 00000000..5653c671 --- /dev/null +++ b/tests/test-content/content/public/published-sub/published-sub-sub/content.md @@ -0,0 +1,27 @@ +--- +title: Sub-folder content title +--- + +# Heading in the way + +Short sentence. + +## Heading in the way + +Something a little bit longer. + +### Heading the way + +Third sentence. + +## Heading in the way + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce felis arcu, molestie nec imperdiet eu, tristique ut elit. Curabitur iaculis sodales turpis a pellentesque. In ac nibh ex. Maecenas ornare in nisi ut commodo. Etiam consequat aliquam erat. Quisque varius mattis risus, consequat viverra eros scelerisque in. Nulla faucibus porta libero a sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis finibus placerat ante faucibus hendrerit. Phasellus condimentum nisi sed velit pretium, at feugiat quam convallis. + +Nunc sed felis a lacus convallis porttitor ut eu nulla. Morbi ac neque elit. Cras sit amet hendrerit purus. Nullam in nisi sem. Phasellus sodales condimentum velit, eget tempus urna scelerisque ullamcorper. Curabitur viverra velit sem, vel finibus justo porttitor ultricies. Ut arcu turpis, convallis at efficitur eget, aliquet sit amet dolor. Vestibulum ut sem nec purus imperdiet finibus. Vestibulum auctor tortor a lacinia faucibus. Nulla sagittis fringilla tincidunt. Fusce suscipit et diam et porta. Mauris felis justo, aliquam et neque eget, aliquet venenatis risus. Maecenas erat arcu, semper vitae massa sit amet, facilisis vulputate diam. Praesent lacinia pulvinar vestibulum. Nullam tristique augue magna, vel fermentum mauris malesuada nec. Aenean molestie odio sodales ante molestie pharetra. + +Phasellus sit amet mi vitae turpis tristique luctus. Ut pellentesque mattis nisl, rutrum accumsan mi placerat ac. Vivamus nunc ligula, malesuada non lacinia in, accumsan id sem. Proin tempus ac lacus a pulvinar. Nunc laoreet maximus sem nec scelerisque. Maecenas sem erat, faucibus non arcu sed, accumsan vehicula sapien. Nullam ipsum lorem, bibendum non arcu convallis, fringilla interdum lacus. + +Vivamus elementum elit at lectus dignissim, in commodo sem convallis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus venenatis non ante quis ullamcorper. In facilisis ultricies tempus. Proin mollis sagittis condimentum. Vestibulum ornare pretium pellentesque. In dictum sodales felis ut ornare. + +Fusce nec ullamcorper magna, ac pretium quam. Integer elementum tincidunt ligula eget tristique. Vivamus orci neque, finibus ultricies vulputate eget, rutrum at velit. Vestibulum at nisi ac est vehicula luctus vitae id mauris. Nam tempor egestas justo, a consectetur purus porta non. Curabitur suscipit odio eu enim fringilla, non elementum dui luctus. Sed nec feugiat libero. Mauris luctus nulla ornare, dignissim nulla id, rutrum nunc. From a04b6dfc0ed781348ebfd0015b084e60ef473d0a Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Thu, 11 Nov 2021 19:10:41 -0600 Subject: [PATCH 2/2] fix: #68 --- src/Content/Markdown.php | 2 +- src/HttpResponse.php | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Content/Markdown.php b/src/Content/Markdown.php index 206f221f..fd479dfd 100644 --- a/src/Content/Markdown.php +++ b/src/Content/Markdown.php @@ -159,7 +159,7 @@ public function description(): string } else { // TODO: Doesn't guarantee meta description content. // Log?? - $description = ''; + $description = $body; } } diff --git a/src/HttpResponse.php b/src/HttpResponse.php index 556898bd..0dd4871f 100644 --- a/src/HttpResponse.php +++ b/src/HttpResponse.php @@ -88,17 +88,19 @@ public function body(): string } - $template = ''; - $pageTitle = ''; - $html = ''; + $template = ''; + $pageTitle = ''; + $html = ''; + $description = ''; if ($localFile->isMarkdown()) { $markdown = Markdown::for( file: $localFile, in: $this->request()->fileSystem() ); - $template = $markdown->frontMatter()->template(); - $pageTitle = $markdown->pageTitle(); - $html = $markdown->html(); + $template = $markdown->frontMatter()->template(); + $pageTitle = $markdown->pageTitle(); + $html = $markdown->html(); + $description = $markdown->description(); } if ($this->request()->isSitemap()) { @@ -112,6 +114,10 @@ public function body(): string 'name viewport', 'content width=device-width,initial-scale=1' ), + Element::meta()->omitEndTag()->props( + 'name description', + 'content ' . $description + ), Element::link()->omitEndTag()->props( 'type image/x-icon', 'rel icon',