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