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
This commit is contained in:
JP Hastings-Spital 2024-07-16 13:15:04 +01:00
parent ad28ed4778
commit 2991c30e20
5 changed files with 46 additions and 1 deletions

View file

@ -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"]

View file

@ -0,0 +1,5 @@
{{ range .Site.RegularPages }}
{{- if .Permalink -}}
{{- printf "/%s %s 301\n" (partial "shortlink.txt" .) .Permalink -}}
{{- end -}}
{{- end -}}

View file

@ -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 -}}

View file

@ -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 `<link rel="shortlink">` on all relevant pages
- [ ] Upload new redirects to byjp.fyi repo
- [ ] Add additional logic to _combine_ the redirects files as needed
### Done ✓

16
tools/redirects/split.bash Executable file
View file

@ -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}"