Skip to content

Commit

Permalink
Merge pull request #104 from 8fold/update-sitemap
Browse files Browse the repository at this point in the history
feature: add priority and lastmod to sitemap
  • Loading branch information
joshbruce authored Nov 14, 2021
2 parents 657b819 + 54ae36a commit ab95650
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions content/public/content.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 27 additions & 13 deletions src/Content/FrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace JoshBruce\Site\Content;

use DateTime;
// use Carbon\Carbon;

class FrontMatter
{
/**
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 31 additions & 1 deletion src/Documents/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (is_string($date) and 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);
Expand Down
2 changes: 1 addition & 1 deletion src/PageComponents/DateBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
$xml->body()
)->toBe(<<<xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://joshbruce.com</loc></url><url><loc>https://joshbruce.com/published-sub</loc></url><url><loc>https://joshbruce.com/published-sub/published-sub-sub</loc></url></urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://joshbruce.com</loc><lastmod>2021-11-03</lastmod><priority>0.5</priority></url><url><loc>https://joshbruce.com/published-sub</loc><lastmod>2021-11-13</lastmod><priority>0.5</priority></url></urlset>
xml
);
})->group('request', 'response', 'sitemap');
1 change: 1 addition & 0 deletions tests/test-content/content/public/content.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/test-content/content/public/published-sub/content.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Sub-folder content title
created: 20211001
updated: 20211113
---

# Heading in the way
Expand Down

0 comments on commit ab95650

Please sign in to comment.