From 3748c4e96d4a5d0c570f6983e047d2a943500b16 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Sat, 13 Nov 2021 18:15:52 -0600 Subject: [PATCH 1/2] feature: add priority and lastmod to sitemap --- content/public/content.md | 1 + src/Content/FrontMatter.php | 40 +++++++++++++------ src/Documents/Sitemap.php | 32 ++++++++++++++- src/PageComponents/DateBlock.php | 2 +- tests/SitemapTest.php | 2 +- tests/test-content/content/public/content.md | 1 + .../content/public/published-sub/content.md | 2 + 7 files changed, 64 insertions(+), 16 deletions(-) diff --git a/content/public/content.md b/content/public/content.md index 120e2d5e..2e68b02c 100644 --- a/content/public/content.md +++ b/content/public/content.md @@ -1,5 +1,6 @@ --- title: Josh Bruce's personal site +created: 20210101 header_quote: Welcome to the rabbit-hole. copyright: Joshua C. Bruce copyright_year: 2004 diff --git a/src/Content/FrontMatter.php b/src/Content/FrontMatter.php index dd0eaf95..352bd461 100644 --- a/src/Content/FrontMatter.php +++ b/src/Content/FrontMatter.php @@ -4,6 +4,9 @@ namespace JoshBruce\Site\Content; +use DateTime; +// use Carbon\Carbon; + class FrontMatter { /** @@ -45,26 +48,37 @@ public function data(): array return []; } - public function created(): int|false + public function created(string $format = ''): string|int|false { - if ($this->hasMember('created')) { - return $this->frontMatter['created']; - } - return false; + return $this->dateField('created', $format); } - public function moved(): int|false + public function moved(string $format = ''): string|int|false { - if ($this->hasMember('moved')) { - return $this->frontMatter['moved']; - } - return false; + return $this->dateField('moved', $format); } - public function updated(): int|false + public function updated(string $format = ''): string|int|false { - if ($this->hasMember('updated')) { - return $this->frontMatter['updated']; + return $this->dateField('updated', $format); + } + + private function dateField( + string $key, + string $format = '' + ): string|int|false { + if ($this->hasMember($key)) { + $date = $this->frontMatter[$key]; + if (strlen($format) === 0) { + return $date; + + } + + $date = DateTime::createFromFormat('Ymd', strval($date)); + if ($date) { + return $date->format($format); + + } } return false; } diff --git a/src/Documents/Sitemap.php b/src/Documents/Sitemap.php index d40e7e07..08fe9bea 100644 --- a/src/Documents/Sitemap.php +++ b/src/Documents/Sitemap.php @@ -26,10 +26,40 @@ public static function create(FileSystemInterface $fileSystem): string $urls = []; foreach ($files as $file) { $path = strval(str_replace('/content.md', '', $file->path())); + $frontMatter = Markdown::for($file, $fileSystem)->frontMatter(); + $date = ''; + if ($frontMatter->hasMember('updated')) { + $date = $frontMatter->updated('Y-m-d'); + + } elseif ($frontMatter->hasMember('created')) { + $date = $frontMatter->created('Y-m-d'); + + } + + if (strlen($date) === 0) { + continue; + } + + $priority = 0.5; + if ( + str_starts_with( + $file->path(false), + '/finances/building-wealth-paycheck-to-paycheck/' + ) + ) { + $priority = 0.0; + + } elseif (str_starts_with($file->path(false), '/finances')) { + $priority = 0.1; + + } + $urls[$path] = Element::url( Element::loc( File::at($path, $fileSystem)->canonicalUrl() - ) + ), + Element::lastmod($date), + Element::priority($priority) ); } ksort($urls); diff --git a/src/PageComponents/DateBlock.php b/src/PageComponents/DateBlock.php index f414ab71..64f68c23 100644 --- a/src/PageComponents/DateBlock.php +++ b/src/PageComponents/DateBlock.php @@ -47,7 +47,7 @@ public static function create(FrontMatter $frontMatter): string private static function timestamp( string $label, - int|false $date = false, + string|int|false $date = false, string $schemaProp = '' ): Element|string { if (! $date) { diff --git a/tests/SitemapTest.php b/tests/SitemapTest.php index 225e72f4..8a5f9d0c 100644 --- a/tests/SitemapTest.php +++ b/tests/SitemapTest.php @@ -44,7 +44,7 @@ $xml->body() )->toBe(<< - https://joshbruce.comhttps://joshbruce.com/published-subhttps://joshbruce.com/published-sub/published-sub-sub + https://joshbruce.com2021-11-03https://joshbruce.com/published-sub2021-11-13https://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 dd6d1e76..922973cb 100644 --- a/tests/test-content/content/public/content.md +++ b/tests/test-content/content/public/content.md @@ -1,6 +1,7 @@ --- 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. +created: 2021113 --- # Heading in the way diff --git a/tests/test-content/content/public/published-sub/content.md b/tests/test-content/content/public/published-sub/content.md index 502805a7..3a044e08 100644 --- a/tests/test-content/content/public/published-sub/content.md +++ b/tests/test-content/content/public/published-sub/content.md @@ -1,5 +1,7 @@ --- title: Sub-folder content title +created: 20211001 +updated: 20211113 --- # Heading in the way From 54ae36ad90dafde2c36d57a09344dd7023625e43 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Sat, 13 Nov 2021 18:18:59 -0600 Subject: [PATCH 2/2] fix: stan and tests --- src/Documents/Sitemap.php | 2 +- tests/SitemapTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Documents/Sitemap.php b/src/Documents/Sitemap.php index 08fe9bea..861f690d 100644 --- a/src/Documents/Sitemap.php +++ b/src/Documents/Sitemap.php @@ -36,7 +36,7 @@ public static function create(FileSystemInterface $fileSystem): string } - if (strlen($date) === 0) { + if (is_string($date) and strlen($date) === 0) { continue; } diff --git a/tests/SitemapTest.php b/tests/SitemapTest.php index 8a5f9d0c..f397afa6 100644 --- a/tests/SitemapTest.php +++ b/tests/SitemapTest.php @@ -44,7 +44,7 @@ $xml->body() )->toBe(<< - https://joshbruce.com2021-11-03https://joshbruce.com/published-sub2021-11-13https://joshbruce.com/published-sub/published-sub-sub + https://joshbruce.com2021-11-030.5https://joshbruce.com/published-sub2021-11-130.5 xml ); })->group('request', 'response', 'sitemap');