From 8ee2f1b17cb2b95454e6686d9fe32dbcadcdb9dd Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Sun, 8 Mar 2020 02:19:19 +0000 Subject: Add sitemap --- bootstrap.php | 2 ++ config.php | 2 +- listeners/GenerateSitemap.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 listeners/GenerateSitemap.php diff --git a/bootstrap.php b/bootstrap.php index 7d6e3c4..cdefc5b 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -13,3 +13,5 @@ * // Your code here * }); */ + +$events->afterBuild(App\Listeners\GenerateSitemap::class); diff --git a/config.php b/config.php index 3f46230..495952e 100644 --- a/config.php +++ b/config.php @@ -3,7 +3,7 @@ use Illuminate\Support\Str; return [ - 'baseUrl' => '', + 'baseUrl' => 'http://localhost:3000', 'production' => false, 'siteName' => 'davidtsadler.com', 'siteDescription' => 'My little bit of the Internet', diff --git a/listeners/GenerateSitemap.php b/listeners/GenerateSitemap.php new file mode 100644 index 0000000..b7f471e --- /dev/null +++ b/listeners/GenerateSitemap.php @@ -0,0 +1,43 @@ +getConfig('baseUrl'); + + if (! $baseUrl) { + echo("\nTo generate a sitemap.xml file, please specify a 'baseUrl' in config.php.\n\n"); + + return; + } + + $sitemap = new Sitemap($jigsaw->getDestinationPath() . '/sitemap.xml'); + + collect($jigsaw->getOutputPaths()) + ->reject(function ($path) { + return $this->isExcluded($path); + })->each(function ($path) use ($baseUrl, $sitemap) { + $sitemap->addItem(rtrim($baseUrl, '/') . $path, time(), Sitemap::DAILY); + }); + + $sitemap->write(); + } + + public function isExcluded($path) + { + return Str::is($this->exclude, $path); + } +} -- cgit v1.2.3-13-gbd6f