Files
davideisinger.com/themes/v2/layouts/partials/gemtext.gmi
2025-02-19 00:41:56 -05:00

60 lines
2.5 KiB
Gemtext

{{- $content := .content -}}
{{- $page := .page -}}
{{- /* Convert link lists */ -}}
{{- $listPattern := `(?m)^\* \[([^\]]+)\]\[(\d+)\]$` -}}
{{- range findRE $listPattern $content }}
{{- /* Extract text and reference number */ -}}
{{- $fullMatch := . -}}
{{- $text := replaceRE $listPattern `$1` . -}}
{{- $refNum := replaceRE $listPattern `$2` . -}}
{{- /* Step 2: Find the corresponding reference link */ -}}
{{- $refPattern := printf `(?m)^\[%s\]: (.+)$` $refNum -}}
{{- $refMatch := findRE $refPattern $content -}}
{{- if gt (len $refMatch) 0 }}
{{- $url := replaceRE $refPattern `$1` (index $refMatch 0) -}}
{{- /* Step 3: Replace original list item with => link format */ -}}
{{- $newLine := printf "=> %s %s" $url $text -}}
{{- $content = replace $content $fullMatch $newLine -}}
{{- /* Step 4: Remove the reference definition */ -}}
{{- $content = replace $content (index $refMatch 0) "" -}}
{{- end -}}
{{- end -}}
{{- /* Convert links */ -}}
{{- $content = $content | replaceRE `([^\w])\[([^\]]+)\]\[(\d+)\]` "$1$2[$3]" -}}
{{- $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 `=> /(elsewhere[^ ])` (printf "=> %s$1" site.BaseURL) -}}
{{- $content = $content | replaceRE `(?m)^(\* [^\n]+)\n\n ` "$1 " -}}
{{- $content = $content | replaceRE `(?m)\n{3,}` "\n\n" }}
{{- $content | safeHTML -}}