Format Gemini link list

This commit is contained in:
David Eisinger
2025-02-16 14:47:02 -05:00
parent 364f39a761
commit 31087c4349

View File

@@ -1,23 +1,57 @@
{{ $content := .content -}} {{- $content := .content -}}
{{ $page := .page -}} {{- $page := .page -}}
{{ $content = $content | replaceRE `<!--more-->\n+` "" -}}
{{ $content = $content | replaceRE `\[([^\]]+)\]\[(\d+)\]` "$1[$2]" -}} {{- /* Convert link lists */ -}}
{{ $content = $content | replaceRE `(?m)^\[(\d+)\]: (.*)$` "=> $2 [$1] $2" -}} {{- $listPattern := `(?m)^\* \[([^\]]+)\]\[(\d+)\]$` -}}
{{ $content = $content | replaceRE `\[([^\]]+)\]\([^\)]+\)` "$1" -}} {{- range findRE $listPattern $content }}
{{ $content = $content | replaceRE `(?m)^ >` ">" -}} {{- /* Extract text and reference number */ -}}
{{ $content = $content | replaceRE `(?m)^ \*` "* ⇢" -}} {{- $fullMatch := . -}}
{{ $content = $content | replaceRE `(?m)^- ` "* " -}} {{- $text := replaceRE $listPattern `$1` . -}}
{{ $content = $content | replaceRE `(?m)^[\*-]{3}$` "✱ ✱ ✱" -}} {{- $refNum := replaceRE $listPattern `$2` . -}}
{{ $content = $content | replaceRE `=> (/.+\.pdf)` (printf "=> %s$1" site.BaseURL) -}}
{{ $audioRE := `<audio controls src="(.*/([^"]+))"></audio>` -}} {{- /* Step 2: Find the corresponding reference link */ -}}
{{ range findRE $audioRE $content -}} {{- $refPattern := printf `(?m)^\[%s\]: (.+)$` $refNum -}}
{{ $url := . | replaceRE $audioRE "$1" | replaceRE ` ` "%20" -}} {{- $refMatch := findRE $refPattern $content -}}
{{ $title := . | replaceRE $audioRE "$2" -}}
{{ $content = replace $content . (printf "=> %s %s" $url (substr $title 0 -4)) -}} {{- if gt (len $refMatch) 0 }}
{{ end -}} {{- $url := replaceRE $refPattern `$1` (index $refMatch 0) -}}
{{ range findRE `{{<(dither|thumbnail) [^/]+/>}}` $content -}}
{{ $rendered := $page.RenderString . -}} {{- /* Step 3: Replace original list item with => link format */ -}}
{{ $url := $rendered | replaceRE `(?s).*src="([^"]+)".*` "=> $1" -}} {{- $newLine := printf "=> %s %s" $url $text -}}
{{ $content = replace $content . $url -}} {{- $content = replace $content $fullMatch $newLine -}}
{{ end -}}
{{ $content | safeHTML -}} {{- /* Step 4: Remove the reference definition */ -}}
{{- $content = replace $content (index $refMatch 0) "" -}}
{{- end -}}
{{- end -}}
{{- /* Convert links */ -}}
{{- $content = $content | replaceRE `\[([^\]]+)\]\[(\d+)\]` "$1[$2]" -}}
{{- $content = $content | replaceRE `(?m)^\[(\d+)\]: (.*)$` "=> $2 [$1] $2" -}}
{{- $content = $content | replaceRE `\[([^\]]+)\]\([^\)]+\)` "$1" -}}
{{- /* Replace audio tags */ -}}
{{- $audioRE := `<audio controls src="(.*/([^"]+))"></audio>` -}}
{{- range findRE $audioRE $content -}}
{{- $url := . | replaceRE $audioRE "$1" | replaceRE ` ` "%20" -}}
{{- $title := . | replaceRE $audioRE "$2" -}}
{{- $content = replace $content . (printf "=> %s %s" $url (substr $title 0 -4)) -}}
{{- end -}}
{{- /* Photo processing */ -}}
{{- range findRE `{{<(dither|thumbnail) [^/]+/>}}` $content -}}
{{- $rendered := $page.RenderString . -}}
{{- $url := $rendered | replaceRE `(?s).*src="([^"]+)".*` "=> $1" -}}
{{- $content = replace $content . $url -}}
{{- end -}}
{{- /* Misc. markup fixes */ -}}
{{- $content = $content | replaceRE `<!--more-->\n+` "" -}}
{{- $content = $content | replaceRE `(?m)^ >` ">" -}}
{{- $content = $content | replaceRE `(?m)^ \*` "* ⇢" -}}
{{- $content = $content | replaceRE `(?m)^- ` "* " -}}
{{- $content = $content | replaceRE `(?m)^[\*-]{3}$` "✱ ✱ ✱" -}}
{{- $content = $content | replaceRE `=> (/.+\.pdf)` (printf "=> %s$1" site.BaseURL) -}}
{{- $content = $content | replaceRE `(?m)\n{3,}` "\n\n" }}
{{- $content | safeHTML -}}