From 92a61694197f5b754209f556941b7e40009d6b7f Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Tue, 16 Jul 2024 23:33:23 +0100 Subject: [PATCH] No auto generated shortlinks :( MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Can't autogenerate shortlinks, as there are too many for byjp.fyi to handle! The file can't be larger than 64kb in the one file. Keep the process for manually shortlinked files — even if it's now a little complex! --- layouts/_default/index.redirects | 7 ++++--- layouts/partials/shortlink.txt | 31 +------------------------------ 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/layouts/_default/index.redirects b/layouts/_default/index.redirects index 55b8502c..5840cc1a 100644 --- a/layouts/_default/index.redirects +++ b/layouts/_default/index.redirects @@ -1,5 +1,6 @@ -{{ range .Site.RegularPages }} - {{- if .Permalink -}} - {{- printf "/%s %s 301\n" (partial "shortlink.txt" .) .Permalink -}} +{{- range .Site.RegularPages -}} + {{- $shortlink := partial "shortlink.txt" . -}} + {{- if and .Permalink $shortlink -}} + {{- printf "/%s %s 301\n" $shortlink .Permalink -}} {{- end -}} {{- end -}} diff --git a/layouts/partials/shortlink.txt b/layouts/partials/shortlink.txt index af6664a1..9e14463c 100644 --- a/layouts/partials/shortlink.txt +++ b/layouts/partials/shortlink.txt @@ -1,30 +1 @@ -{{- $shortlink := .Params.shortlink -}} -{{- $chars := split "123456789bcdfghjkmnpqrstuvwxyz" "" -}}{{/* Omit vowels and confusing characters to prevent accidental spelling of words */}} -{{- $base := len $chars -}} -{{- $size := 8 -}}{{/* 8 chars with 30 possibilities means 0.0091% chance of collision with 10k posts; I currently have 3,213 */}} -{{- if and (not $shortlink) .IsPage -}}{{/* Don't make dynamic shortlinks for list taxonomy/list/non-regular pages */}} - {{- $hex := .RelPermalink | crypto.MD5 -}} - - {{- $idx := 0 -}} - {{- $n := 0 -}} - {{- $enc := slice -}} - - {{- range (split $hex "") | collections.Reverse -}} - {{- $dec := int (printf "0x%s" .) -}} - {{- $n = add $dec (mul $n 16) -}} - - {{- range seq 2 -}} - {{- if le $n $base -}}{{ break }}{{- end -}} - - {{- $amt := (mod $n $base) -}} - {{- $char := index $chars $amt }} - {{- $enc = $enc | append $char -}} - {{- $n = div $n $base -}} - {{- end -}} - - {{- if ge (len $enc) $size -}}{{- break -}}{{- end -}} - {{- end -}} - - {{- $shortlink = printf "b/%s" (delimit ($enc | collections.Reverse) "") -}} -{{- end -}} -{{- $shortlink -}} \ No newline at end of file +{{- .Params.shortlink | default "" -}}