{{ $content := . }} {{ $footnotesPattern := `(?s)
]*>.*?
` }} {{ $footnotesMatch := findRE $footnotesPattern $content 1 }} {{ if $footnotesMatch }} {{ $footnotesSection := index $footnotesMatch 0 }} {{ $liPattern := `(?s)
  • (.*?)
  • ` }} {{ $footnoteItems := findRESubmatch $liPattern $footnotesSection }} {{ $footnoteMap := dict }} {{ range $footnoteItems }} {{ $id := index . 1 }} {{ $contentRaw := index . 2 }} {{ $cleanContent := replaceRE `(?s)\s*]*>.*?\s*$` "" $contentRaw }} {{ $footnoteMap = merge $footnoteMap (dict $id $cleanContent) }} {{ end }} {{ $contentWithoutFootnotes := replaceRE $footnotesPattern "" $content }} {{ $refPattern := `` }} {{ $refs := findRESubmatch $refPattern $contentWithoutFootnotes }} {{ $result := $contentWithoutFootnotes }} {{ $count := len $refs }} {{ range seq (sub $count 1) -1 0 }} {{ $ref := index $refs . }} {{ $refFull := index $ref 0 }} {{ $refId := index $ref 1 }} {{ $noteContent := index $footnoteMap $refId }} {{ if $noteContent }} {{ $inlineFootnote := printf `
    %s.
    %s
    ` $refId $refId $noteContent }} {{ $blockTags := slice "p" "li" "td" "th" "blockquote" "h1" "h2" "h3" "h4" "h5" "h6" "figcaption" "dt" "dd" }} {{ $found := false }} {{ range $blockTags }} {{ if not $found }} {{ $tag := . }} {{ $pattern := printf `(?s)<%s[^>]*>.*?` $tag $tag }} {{ $matches := findRE $pattern $result }} {{ range $matches }} {{ if not $found }} {{ $block := . }} {{ if strings.Contains $block $refFull }} {{ $newBlock := printf `%s%s` $block $inlineFootnote }} {{ $result = replace $result $block $newBlock 1 }} {{ $found = true }} {{ end }} {{ end }} {{ end }} {{ end }} {{ end }} {{ if not $found }} {{ $replacement := printf `%s%s` $refFull $inlineFootnote }} {{ $result = replace $result $refFull $replacement 1 }} {{ end }} {{ end }} {{ end }} {{ $content = $result }} {{ end }} {{ $content | safeHTML }}