From 2991c30e20ee5422e81c1ab9f4795e977c362508 Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Tue, 16 Jul 2024 13:15:04 +0100 Subject: [PATCH] Crypto-generated shortlink - Every page now has a shortlink - Shortlinks can be manually overridden with the shortlink frontmatter param - A single "index.redirects" file is created at build time - A "redirects" tool splits that one file into multiple `_redirects` files, as would be needed on a site like byjp.fyi --- config.toml | 12 +++++++++++- layouts/_default/index.redirects | 5 +++++ layouts/partials/shortlink.txt | 6 ++++++ todo.md | 8 ++++++++ tools/redirects/split.bash | 16 ++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 layouts/_default/index.redirects create mode 100644 layouts/partials/shortlink.txt create mode 100755 tools/redirects/split.bash diff --git a/config.toml b/config.toml index 7adde8b3..1586baeb 100644 --- a/config.toml +++ b/config.toml @@ -189,6 +189,8 @@ date = ["date", "publishDate", "lastmod", ":git"] suffixes = ['opml.xml'] [mediaTypes.'text/gemini'] suffixes = ['gmi'] + [mediaTypes.'text/plain+redirects'] + suffixes = ['redirects'] [outputFormats] [outputFormats.opml] mediaType = 'text/x-opml' @@ -215,9 +217,17 @@ date = ["date", "publishDate", "lastmod", ":git"] permalinkable = false noUgly = false notAlternative = true + [outputFormats.redirects] + name = 'Redirects File' + isPlainText = true + isHTML = false + mediaType = 'text/plain+redirects' + permalinkable = false + noUgly = false + notAlternative = true [outputs] - home = ["html", "rss", "gemini"] + home = ["html", "rss", "gemini", "redirects"] page = ["html", "gemini"] rss = ["rss"] section = ["html", "rss", "gemini"] diff --git a/layouts/_default/index.redirects b/layouts/_default/index.redirects new file mode 100644 index 00000000..55b8502c --- /dev/null +++ b/layouts/_default/index.redirects @@ -0,0 +1,5 @@ +{{ range .Site.RegularPages }} + {{- if .Permalink -}} + {{- printf "/%s %s 301\n" (partial "shortlink.txt" .) .Permalink -}} + {{- end -}} +{{- end -}} diff --git a/layouts/partials/shortlink.txt b/layouts/partials/shortlink.txt new file mode 100644 index 00000000..4720e591 --- /dev/null +++ b/layouts/partials/shortlink.txt @@ -0,0 +1,6 @@ +{{- $shortlink := .Params.shortlink -}} +{{- if not $shortlink -}} + {{/* TODO: Could convert to base36 to reduce size by 2 chars */}} + {{- $shortlink = printf "b/%s" (substr (.RelPermalink | crypto.MD5 ) 0 10) -}} +{{- end -}} +{{- $shortlink -}} \ No newline at end of file diff --git a/todo.md b/todo.md index 28396d5d..78b64f65 100644 --- a/todo.md +++ b/todo.md @@ -29,6 +29,14 @@ Things I might work on within my personal blog's software. - [ ] Remove lychee failing links - [ ] Import Facebook posts +- [ ] Shortlinks + - [x] All posts have an auto-generated unqiue shortlink + - [x] Allow for customisation of shortlinks + - [x] Create suitable `_redirects` files + - [ ] Import pre-existing shortlinks + - [ ] Add shortlink as `` on all relevant pages + - [ ] Upload new redirects to byjp.fyi repo + - [ ] Add additional logic to _combine_ the redirects files as needed ### Done ✓ diff --git a/tools/redirects/split.bash b/tools/redirects/split.bash new file mode 100755 index 00000000..a0b85034 --- /dev/null +++ b/tools/redirects/split.bash @@ -0,0 +1,16 @@ +#!/bin/bash + +rm **/_redirects + +while IFS=$'\n' read -r line +do + # Extract the absolute file path + outdir=$(dirname $(echo "$line" | cut -d " " -f1) | cut -d "/" -f2) + + if [[ -n "$outdir" ]]; then + [[ ! -d $outdir ]] && mkdir -p $outdir + echo $line | cut -c $((${#outdir}+2))- >> "./$outdir/_redirects" + else + echo $line >> "_redirects" + fi +done < "${1:-/dev/stdin}"