diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2020-02-17 19:59:15 +0000 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2020-02-17 19:59:15 +0000 |
| commit | 498913806182905cc0c14bd12a61f9af26fa16b4 (patch) | |
| tree | 0c36a6df72900c184e900b274bad14aa5114fc5d /listeners | |
| parent | 12b052b1f480c5a95acd1477e28ce76f84ff7932 (diff) | |
Switch over to Jigsaw
Diffstat (limited to 'listeners')
| -rw-r--r-- | listeners/GenerateIndex.php | 22 | ||||
| -rw-r--r-- | listeners/GenerateSitemap.php | 43 |
2 files changed, 65 insertions, 0 deletions
diff --git a/listeners/GenerateIndex.php b/listeners/GenerateIndex.php new file mode 100644 index 0000000..80dd481 --- /dev/null +++ b/listeners/GenerateIndex.php @@ -0,0 +1,22 @@ +<?php + +namespace App\Listeners; + +use TightenCo\Jigsaw\Jigsaw; + +class GenerateIndex +{ + public function handle(Jigsaw $jigsaw) + { + $data = collect($jigsaw->getCollection('posts')->map(function ($page) use ($jigsaw) { + return [ + 'title' => $page->title, + 'categories' => $page->categories, + 'link' => rightTrimPath($jigsaw->getConfig('baseUrl')) . $page->getPath(), + 'snippet' => $page->getExcerpt(), + ]; + })->values()); + + file_put_contents($jigsaw->getDestinationPath() . '/index.json', json_encode($data)); + } +} diff --git a/listeners/GenerateSitemap.php b/listeners/GenerateSitemap.php new file mode 100644 index 0000000..6a439a6 --- /dev/null +++ b/listeners/GenerateSitemap.php @@ -0,0 +1,43 @@ +<?php + +namespace App\Listeners; + +use samdark\sitemap\Sitemap; +use TightenCo\Jigsaw\Jigsaw; +use Illuminate\Support\Str; + +class GenerateSitemap +{ + protected $exclude = [ + '/assets/*', + '*/favicon.ico', + '*/404*' + ]; + + public function handle(Jigsaw $jigsaw) + { + $baseUrl = $jigsaw->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); + } +} |
