From 2666d6ed67b2f7253274ec417869ff26e0af90ca Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Fri, 16 May 2025 13:03:55 -0700 Subject: [PATCH] feat(reading): clean and refactor routing for books -> reading to separate paths --- api/book-import.php | 2 +- config/filters/media.js | 2 +- package-lock.json | 4 +-- package.json | 2 +- server/utils/init.php | 1 + server/utils/routing.php | 8 +++++ src/assets/styles/index.css | 2 +- .../styles/pages/{books.css => reading.css} | 0 src/includes/fetchers/artist.php.liquid | 10 ++---- src/includes/fetchers/book.php.liquid | 33 +++---------------- src/includes/fetchers/genre.php.liquid | 10 ++---- src/includes/fetchers/movie.php.liquid | 10 ++---- src/includes/fetchers/show.php.liquid | 10 ++---- src/includes/fetchers/tags.php.liquid | 12 ++----- src/includes/metadata/index.liquid | 2 +- src/meta/htaccess.liquid | 16 ++++++--- src/pages/dynamic/book.php.liquid | 4 +-- src/pages/media/books/index.html | 2 +- src/pages/media/books/year.html | 6 ++-- src/pages/static/search.html | 2 +- src/pages/static/stats.html | 2 +- 21 files changed, 51 insertions(+), 89 deletions(-) create mode 100644 server/utils/routing.php rename src/assets/styles/pages/{books.css => reading.css} (100%) diff --git a/api/book-import.php b/api/book-import.php index da1acad..1b5a286 100644 --- a/api/book-import.php +++ b/api/book-import.php @@ -90,7 +90,7 @@ class BookImportHandler extends ApiHandler "author" => $author, "description" => $description, "read_status" => "want to read", - "slug" => "/books/" . $isbn, + "slug" => "/reading/books/" . $isbn, ]; $this->makeRequest("POST", "books", ["json" => $bookPayload]); diff --git a/config/filters/media.js b/config/filters/media.js index a8ee3ae..489d057 100644 --- a/config/filters/media.js +++ b/config/filters/media.js @@ -8,7 +8,7 @@ export default { .sort((a, b) => b.value - a.value) .map( (year, index) => - `${year.value}${ + `${year.value}${ index < years.length - 1 ? " • " : "" }` ) diff --git a/package-lock.json b/package-lock.json index 3f75eae..fab5c1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "5.1.6", + "version": "6.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "5.1.6", + "version": "6.0.0", "license": "MIT", "dependencies": { "html-minifier-terser": "7.2.0", diff --git a/package.json b/package.json index 580dae9..f7d450a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "5.1.6", + "version": "6.0.0", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": { diff --git a/server/utils/init.php b/server/utils/init.php index 4894fa5..b00f9c9 100644 --- a/server/utils/init.php +++ b/server/utils/init.php @@ -2,6 +2,7 @@ require_once "icons.php"; require_once "media.php"; require_once "paginator.php"; + require_once "routing.php"; require_once "strings.php"; require_once "tags.php"; ?> diff --git a/server/utils/routing.php b/server/utils/routing.php new file mode 100644 index 0000000..433fd42 --- /dev/null +++ b/server/utils/routing.php @@ -0,0 +1,8 @@ + diff --git a/src/assets/styles/index.css b/src/assets/styles/index.css index bfe0ae3..a283ea8 100644 --- a/src/assets/styles/index.css +++ b/src/assets/styles/index.css @@ -11,11 +11,11 @@ @import url("./base/index.css") layer(base); /* page styles */ -@import url("./pages/books.css") layer(page); @import url("./pages/contact.css") layer(page); @import url("./pages/links.css") layer(page); @import url("./pages/media.css") layer(page); @import url("./pages/music.css") layer(page); +@import url("./pages/reading.css") layer(page); @import url("./pages/watching.css") layer(page); @import url("./pages/webrings.css") layer(page); diff --git a/src/assets/styles/pages/books.css b/src/assets/styles/pages/reading.css similarity index 100% rename from src/assets/styles/pages/books.css rename to src/assets/styles/pages/reading.css diff --git a/src/includes/fetchers/artist.php.liquid b/src/includes/fetchers/artist.php.liquid index 8b96440..0a2c8f6 100644 --- a/src/includes/fetchers/artist.php.liquid +++ b/src/includes/fetchers/artist.php.liquid @@ -9,18 +9,12 @@ $requestUri = $_SERVER["REQUEST_URI"]; $url = trim(parse_url($requestUri, PHP_URL_PATH), "/"); - if (strpos($url, "music/artists/") !== 0) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (strpos($url, "music/artists/") !== 0) redirectTo404(); $fetcher = new ArtistFetcher(); $artist = $fetcher->fetch($url); - if (!$artist) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!$artist) redirectTo404(); $artist["description"] = parseMarkdown($artist["description"]); $pageTitle = htmlspecialchars("Artists • " . $artist["name"], ENT_QUOTES, "UTF-8"); diff --git a/src/includes/fetchers/book.php.liquid b/src/includes/fetchers/book.php.liquid index e72c94e..c524f4a 100644 --- a/src/includes/fetchers/book.php.liquid +++ b/src/includes/fetchers/book.php.liquid @@ -1,7 +1,7 @@ fetch($url); - if (!$book) { - echo file_get_contents(__DIR__ . "/../404/index.html"); - exit(); - } + if (!$book) redirectTo404(); $book["description"] = parseMarkdown($book["description"]); $pageTitle = htmlspecialchars("Books • {$book["title"]} by {$book["author"]}", ENT_QUOTES, "UTF-8"); diff --git a/src/includes/fetchers/genre.php.liquid b/src/includes/fetchers/genre.php.liquid index 8816c65..24d3506 100644 --- a/src/includes/fetchers/genre.php.liquid +++ b/src/includes/fetchers/genre.php.liquid @@ -9,18 +9,12 @@ $requestUri = $_SERVER["REQUEST_URI"]; $url = trim(parse_url($requestUri, PHP_URL_PATH), "/"); - if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) redirectTo404(); $fetcher = new GenreFetcher(); $genre = $fetcher->fetch($url); - if (!$genre) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!$genre) redirectTo404(); $pageTitle = htmlspecialchars("Genres • " . $genre["name"], ENT_QUOTES, "UTF-8"); $pageDescription = truncateText( diff --git a/src/includes/fetchers/movie.php.liquid b/src/includes/fetchers/movie.php.liquid index 53a3812..d30f3e0 100644 --- a/src/includes/fetchers/movie.php.liquid +++ b/src/includes/fetchers/movie.php.liquid @@ -9,18 +9,12 @@ $requestUri = $_SERVER["REQUEST_URI"]; $url = trim(parse_url($requestUri, PHP_URL_PATH), "/"); - if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) redirectTo404(); $fetcher = new MovieFetcher(); $movie = $fetcher->fetch($url); - if (!$movie) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!$movie) redirectTo404(); $movie["description"] = parseMarkdown($movie["description"]); $pageTitle = htmlspecialchars("Movies • " . $movie["title"], ENT_QUOTES, "UTF-8"); diff --git a/src/includes/fetchers/show.php.liquid b/src/includes/fetchers/show.php.liquid index 2e38f1b..3d78915 100644 --- a/src/includes/fetchers/show.php.liquid +++ b/src/includes/fetchers/show.php.liquid @@ -9,18 +9,12 @@ $requestUri = $_SERVER["REQUEST_URI"]; $url = trim(parse_url($requestUri, PHP_URL_PATH), "/"); - if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) redirectTo404(); $fetcher = new ShowFetcher(); $show = $fetcher->fetch($url); - if (!$show) { - echo file_get_contents(__DIR__ . "/../../404/index.html"); - exit(); - } + if (!$show) redirectTo404(); $pageTitle = htmlspecialchars("Show • " . $show["title"], ENT_QUOTES, "UTF-8"); $pageDescription = truncateText( diff --git a/src/includes/fetchers/tags.php.liquid b/src/includes/fetchers/tags.php.liquid index 2919d02..5895d8b 100644 --- a/src/includes/fetchers/tags.php.liquid +++ b/src/includes/fetchers/tags.php.liquid @@ -14,10 +14,7 @@ exit(); } - if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) { - echo file_get_contents(__DIR__ . "/../404/index.html"); - exit(); - } + if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) redirectTo404(); if (isset($matches[2]) && (int)$matches[2] === 1) { header("Location: /tags/{$matches[1]}", true, 301); @@ -26,10 +23,7 @@ $tag = strtolower(urldecode($matches[1])); - if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) { - echo file_get_contents(__DIR__ . "/../404/index.html"); - exit(); - } + if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) redirectTo404(); $page = isset($matches[2]) ? max(1, (int)$matches[2]) : 1; $pageSize = 20; @@ -37,7 +31,7 @@ $tagged = $fetcher->fetch($tag, $page, $pageSize); if (!$tagged || count($tagged) === 0) { - echo file_get_contents(__DIR__ . '/../404/index.html'); + header("Location: /404/", true, 302); exit(); } diff --git a/src/includes/metadata/index.liquid b/src/includes/metadata/index.liquid index 16adf15..5768c6b 100644 --- a/src/includes/metadata/index.liquid +++ b/src/includes/metadata/index.liquid @@ -51,7 +51,7 @@ {%- when 'books' -%} {%- assign overviewBook = books.all | filterBooksByStatus: 'started' | reverse | first %} {%- assign ogImage = ogImageBaseUrl | append: overviewBook.image -%} - {%- when 'books-year' -%} + {%- when 'reading-year' -%} {%- assign pageTitle = 'Books' | append: ' • ' | append: year.value | append: ' • ' | append: globals.site_name -%} {%- capture pageDescription -%} Here's what I read in {{ year.value }}. diff --git a/src/meta/htaccess.liquid b/src/meta/htaccess.liquid index 3ede2c5..9364522 100644 --- a/src/meta/htaccess.liquid +++ b/src/meta/htaccess.liquid @@ -30,10 +30,18 @@ ErrorDocument 500 /500/index.html ## artists RewriteRule ^music/artists/([^/]+)/?$ music/artists/index.php [L] -## books -RewriteRule ^books/([^/]+)/?$ books/index.php [L] -RewriteRule ^books/years/(\d{4})/?$ books/years/index.html [L] -RewriteRule ^books/?$ books/books.html [L] +## reading + +### redirects from books +RedirectMatch 301 ^/books/years/(\d{4})/?$ /reading/years/$1 +RedirectMatch 301 ^/books/years/?$ /reading/years +RedirectMatch 301 ^/books/((97[89][-]?\d{1,5}[-]?\d{1,7}[-]?\d{1,7}[-]?[\dXx]))/?$ /reading/books/$1 +RedirectMatch 301 ^/books/?$ /reading + +### routing +RewriteRule ^reading/?$ reading/index.html [L] +RewriteRule ^reading/years/(\d{4})/?$ reading/years/$1.html [L] +RewriteRule ^reading/books/([\dXx-]+)/?$ reading/books/index.php [L] ## movies RewriteRule ^watching/movies/([^/]+)/?$ watching/movies/index.php [L] diff --git a/src/pages/dynamic/book.php.liquid b/src/pages/dynamic/book.php.liquid index 9b36598..01bb133 100644 --- a/src/pages/dynamic/book.php.liquid +++ b/src/pages/dynamic/book.php.liquid @@ -1,9 +1,9 @@ --- -permalink: /books/index.php +permalink: /reading/books/index.php type: dynamic schema: book --- -{% tablericon "arrow-left" %} Back to books +{% tablericon "arrow-left" %} Back to reading
{% tablericon "arrow-left" %} Back to books +{% tablericon "arrow-left" %} Back to reading

{{ year.value }} • Books

{% if yearString == currentYearString %}

I've finished {{ bookData.size }} book{% unless bookData.size == 1 %}s{% endunless %} this year.{%- if favoriteBooks %} Among my favorites are {{ favoriteBooks }}.{%- endif -%}

diff --git a/src/pages/static/search.html b/src/pages/static/search.html index d930be0..d6b3eee 100644 --- a/src/pages/static/search.html +++ b/src/pages/static/search.html @@ -5,7 +5,7 @@ description: Search through posts and other content on my site. ---

Search

-

You can find posts, links, artists, genres, movies, shows and books via the field below (though it only surfaces movies and shows I've watched and books I've written something about). You can also browse my tags list.

+

You can find posts, links, artists, genres, movies, shows and books via the field below (though it only surfaces movies and shows I've watched and books I've written something about). You can also browse my tags list.

{% render "blocks/top-tags.liquid" label:"Top tags" tags:topTags.tags diff --git a/src/pages/static/stats.html b/src/pages/static/stats.html index ee4a985..4d7d528 100644 --- a/src/pages/static/stats.html +++ b/src/pages/static/stats.html @@ -5,7 +5,7 @@ description: Some basic stats about my activity on this site. updated: "now" ---

Stats

-

I share the music I listen to, concerts I attend, shows and movies I watch, books I read, posts I write, and links I enjoy on this site. I have some basic counts of each below.

+

I share the music I listen to, concerts I attend, shows and movies I watch, books I read, posts I write, and links I enjoy on this site. I have some basic counts of each below.


I've listened to {{ stats.listen_count }} {{ stats.listen_count | pluralize: "track" }} by {{ stats.artist_count }} {{ stats.artist_count | pluralize: "artist" }} across {{ stats.genre_count }} {{ stats.genre_count | pluralize: "genre" }}.

I've been to {{ stats.concert_count }} {{ stats.concert_count | pluralize: "concert" }} at {{ stats.venue_count }} {{ stats.venue_count | pluralize: "venue" }}.