davideisinger.com

My personal website
Log | Files | Refs | README

commit 16856010180348b21200b4ec0e3a7aca931de133
parent 0821a50f92be4fcca1e0205b99df18dc889f6d51
Author: David Eisinger <[email protected]>
Date:   Tue,  8 Sep 2026 23:21:05 -0400

Improve gemini links

Diffstat:
Mthemes/v2/layouts/partials/gemtext.gmi | 54+++++++++++++++++++++++++++++++++++-------------------
1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/themes/v2/layouts/partials/gemtext.gmi b/themes/v2/layouts/partials/gemtext.gmi @@ -1,27 +1,43 @@ {{- $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) -}} +{{- /* Promote standalone list links, with an optional attribution link. */ -}} +{{- $references := dict -}} +{{- $refPattern := `(?m)^\[(\d+)\]: (.+)$` -}} +{{- range findRE $refPattern $content -}} + {{- $references = merge $references (dict (replaceRE $refPattern `$1` .) (replaceRE $refPattern `$2` .)) -}} +{{- end -}} +{{- $promoted := dict -}} +{{- $listPattern := `(?m)^[*-] \[([^\]]+)\]\[(\d+)\](?:[ ]+\(\[via\]\[(\d+)\]\))?[ ]*$` -}} +{{- $lines := slice -}} +{{- range split $content "\n" -}} + {{- $line := . -}} + {{- if findRE $listPattern . -}} + {{- $text := replaceRE $listPattern `$1` . -}} + {{- $refNum := replaceRE $listPattern `$2` . -}} + {{- $viaNum := replaceRE $listPattern `$3` . -}} + {{- $url := index $references $refNum -}} + {{- $viaURL := index $references $viaNum -}} + {{- if and $url (or (eq $viaNum "") $viaURL) -}} + {{- $newLine := printf "=> %s %s" $url $text -}} + {{- $promoted = merge $promoted (dict $refNum true) -}} + {{- with $viaURL -}} + {{- $label := (urls.Parse .).Host | default . -}} + {{- $newLine = printf "%s\n=> %s via %s" $newLine . $label -}} + {{- $promoted = merge $promoted (dict $viaNum true) -}} + {{- end -}} + {{- $line = $newLine -}} + {{- end -}} + {{- end -}} + {{- $lines = $lines | append $line -}} +{{- end -}} - {{- /* Step 3: Replace original list item with => link format */ -}} - {{- $newLine := printf "=> %s %s" $url $text -}} - {{- $content = replace $content $fullMatch $newLine -}} +{{- $content = delimit $lines "\n" -}} - {{- /* Step 4: Remove the reference definition */ -}} - {{- $content = replace $content (index $refMatch 0) "" -}} +{{- /* Keep definitions that are still referenced elsewhere in the prose. */ -}} +{{- range $refNum, $_ := $promoted -}} + {{- if not (findRE (printf `\[[^\]]+\]\[%s\]` $refNum) $content) -}} + {{- $content = replaceRE (printf `(?m)^\[%s\]: .+$` $refNum) "" $content -}} {{- end -}} {{- end -}}