www.byjp.me/layouts/partials/markdown.gmi
2024-05-07 15:35:43 +01:00

47 lines
No EOL
2 KiB
Text

{{ $sections := split (replaceRE ` ` " " .) "\n#" -}}
{{- $first := true -}}
{{- range $sections -}}
{{- $content := . -}}
{{- if not $first -}}
{{- $content = printf "\n#%s" $content -}}
{{- end -}}
{{- $first = false -}}
{{/* Swap out unsupported list types */}}
{{- $content := $content | replaceRE `\n- (.+?)` "\n* $1" -}}
{{- $content := $content | replaceRE `\n(\d+)\. (.+?)` "\n* $2" -}}
{{/* Remove footnote references */}}
{{- $content := $content | replaceRE `\[\^(.+?)\]:?` "" -}}
{{/* Swap out forced newlines */}}
{{- $content := $content | replaceRE `<br/??>` "\n" -}}
{{- $content := $content | replaceRE `\\\n` "\n" -}}
{{/* Horizontal rules */}}
{{- $content := $content | replaceRE "\n+---\n+" "\n\n" -}}
{{/* Images */}}
{{- $content := $content | replaceRE `\n\n!\[.*?]\((.+?)\)` "\n\n=> $1 Linked image" -}}
{{/* Links */}}
{{- $content := $content | replaceRE `<a .*?href="(.+?)".*?>(.+?)</a>` "[$2]($1)" -}}
{{- $content := $content | replaceRE `\sgemini://(\S*)` " [gemini://$1](gemini://$1)" -}}
{{- $refs := findRE `\[[^]]+?\]\([^#][^)]+?\)` $content -}}
{{/* Remove lines that are only hashtags */}}
{{- $content := $content | replaceRE `\n(\[[^]]+?\]\(/tags/[^)]*?\) *)+\n` "" -}}
{{/* Give hashtags a hash */}}
{{- $content := $content | replaceRE `\[([^]]+?)\]\(/tags/[^)]*?\)` "#$1" -}}
{{/* Give other links a subscript indicator */}}
{{- $content := $content | replaceRE `\[([^]]+?)\]\(([^)]+?)\)` "$1⁺" -}}
{{- $content | safeHTML -}}
{{- if gt (len $refs) 0 -}}
{{- "\n" -}}
{{- range ($refs | uniq) -}}
{{- $parts := split . "](" -}}
{{- $title := substr (index $parts 0) 1 | strings.FirstUpper -}}
{{- $link := partial "noindex.gmi" (substr (index $parts 1) 0 (sub (len (index $parts 1)) 1)) -}}
{{- if hasPrefix $link "/twitter/#" }}{{ continue }}{{ end -}}
{{- if hasPrefix $link "/tags/" }}{{ $title = printf "🔗 #%s" $title }}{{ end -}}
=> {{ printf "%s %s\n" $link $title -}}
{{- end -}}
{{- end -}}
{{- end }}