{{ $content := .Content }}
{{ $raw := .RawContent }}
{{ $page := .Page }}

{{/* Escape slashes for Latex to fix line breaks */}}
{{$latex := findRE "(?:\\${2}([^\\$]+)\\${2})|(?:\\$([^\\$]*)\\$)" $content}}
{{range $latex}}
  {{$fixed := replaceRE "\\\\(?: +|\\n)" "\\\\ " .}}
  {{$content = replace $content . $fixed}}
{{end}}

{{/* Wikilinks */}}
{{$wikilinks := $content | findRE "!?\\[\\[\\S[^\\[\\]\\|]*(?:\\|[^\\[\\]]*)?\\S\\]\\]" }}
{{$codefences := $raw | findRE "\\x60[^\\x60\\n]+\\x60"}}
{{$codeblocks := $raw | findRE "\\x60{3}[^\\x60]+\\x60{3}"}}
{{$code := union $codefences $codeblocks}}

{{range $wikilinks}}
  {{$cur := .}}
  {{$incode := false}}
  {{range $code}}
    {{if (in . $cur)}}
      {{$incode = true}}
    {{end}}
  {{end}}

  {{if not $incode}}

    <!-- remove link delimiters -->
    {{$inner := . | strings.TrimPrefix "!" | strings.TrimPrefix "[[" | strings.TrimSuffix "]]" }}
    <!-- split from alias -->
    {{$split := split $inner "|"}}
    <!-- separate link path -->
    {{$path := index $split 0}}

    {{$reference := split $path "#"}}
    <!-- path with heading link removed -->
    {{$title := index $reference 0}}
    <!-- $display is hyperlink display text -->
    <!-- use alias, else title -->
    {{$display := default $title (index $split 1)}}
    <!-- remove subfolder from title -->
    {{$display := index (last 1 (split $display "/")) 0}}

    <!-- attempt to get title -->
    {{$searchtitle := $title }}
    {{$curpage := $page.GetPage $searchtitle }}
    <!-- attempt to search md file instead  -->
    {{ if (eq $curpage.String "nopPage") }}
      {{$searchtitle = (add $title ".md") }}
      {{$curpage = $page.GetPage $searchtitle }}
    {{ end }}
    <!-- attempt to reverse typographer behaviour  -->
    {{ if (eq $curpage.String "nopPage") }}
      {{$searchtitle = (replace $searchtitle "&amp;" "&") }}
      {{$searchtitle = (replace $searchtitle "&quot;" "\"") }}
      {{$searchtitle = (replace $searchtitle "&rdquo;" "\"") }}
      {{$searchtitle = (replace $searchtitle "&ldquo;" "\"") }}
      {{$searchtitle = (replace $searchtitle "&rsquo;" "'") }}
      {{$searchtitle = (replace $searchtitle "&lsquo;" "'") }}
      {{$curpage = $page.GetPage $searchtitle }}
    {{ end }}
    {{$relpath := relURL $path}}

    <!-- If path to Hugo page -->
    {{if not (eq $curpage.String "nopPage") }}
      {{$block := default "" (index $reference 1)}}
      {{$block = strings.TrimRight "/" (cond (eq $block "") $block (printf "#%s" $block)) | urlize | lower}}
      {{$href := strings.TrimRight "/" $curpage.RelPermalink}}
      {{$link := printf "<a href=\"%s%s\" rel=\"noopener\" class=\"internal-link\" data-src=\"%s\">%s</a>" $href $block $href $display}}
      {{$content = replace $content . $link}}
    <!-- If path to existing file -->
    {{else if fileExists $relpath}}
      {{$splitpath := split $relpath "/"}}
      {{$dirname := first (sub (len $splitpath) 1) $splitpath | path.Join | urlize}}
      {{$basename := index (last 1 $splitpath) 0}}
      {{$href := printf "/%s/%s" $dirname $basename}}
      <!-- Embedded? -->
      {{if (hasPrefix . "!")}}
        {{ $embed_ext := lower (path.Ext $href) }}
        <!-- Image -->
        {{if in ".png .jpg .jpeg .gif .bmp .svg" $embed_ext }}
          {{$width := default "auto" (index $split 1) }}
          {{$link := printf "<img src=\"%s\" width=\"%s\" />" $href $width}}
          {{$content = replace $content . $link}}
        <!-- Video -->
        {{else if in ".mp4 .webm .ogv .mov .mkv" $embed_ext}}
          {{$link := printf "<video src=\"%s\" style=\"width: -webkit-fill-available;\" controls></video>" $href}}
          {{$content = replace $content . $link}}
        <!-- Audio -->
        {{else if in ".mp3 .webm .wav .m4a .ogg .3gp .flac" $embed_ext}}
          {{$link := printf "<audio src=\"%s\" controls></audio>" $href}}
          {{$content = replace $content . $link}}
        <!-- PDF -->
        {{else if in ".pdf" $embed_ext }}
          {{$src_link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\">[source]</a>" $href}}
          {{$iframe_link := printf "<iframe src=\"%s\" style=\"height: -webkit-fill-available; width: -webkit-fill-available;\"></iframe>" $href}}
          {{$link := printf "%s<br>%s" $src_link $iframe_link}}
          {{$content = replace $content . $link}}
        <!-- other -->
        {{else}}
          {{$link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\">%s</a>" $href $href}}
          {{$content = replace $content . $link}}
        {{end}}
      {{else}}
        {{$link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\">%s</a>" $href $display}}
        {{$content = replace $content . $link}}
      {{end}}
    <!-- Broken path -->
    {{else}}
      {{$link := printf "<a class=\"internal-link broken\">%s</a>" $display}}
      {{$content = replace $content . $link}}
    {{end}}

  {{end}}
{{end}}

{{/* Add jumpable anchors */}}
{{ $content = $content | replaceRE "(<h[1-9] id=\"([^\"]+)\">)(.+)(</h[1-9]>)" `<a href="#${2}">${1}<span class="hanchor" ariaLabel="Anchor"># </span>${3}${4}</a>` }}

{{/* Callouts */}}
{{if $.Site.Data.config.enableCallouts}}
  {{ $content = $content | replaceRE "<blockquote>" "<blockquote class=callout>" }}
  {{ $blockquoteclasses := findRE `\[!.+\]` $content }}
  {{ $blockquoteclasses1 := findRE "<blockquote.*?>(.|\n)*?</blockquote>" $content }}
  {{ $blockquotetags := findRE `blockquote class=callout` $content }}
  {{ $counter := 0 }}
  {{ $counter1 := 0 }}
  {{ $finder := index $blockquoteclasses1 $counter }}
  {{range $blockquotetags}}
    {{ $finder = index $blockquoteclasses1 $counter }}
    {{ if (in $finder "[!") }}
      {{ $inner := index $blockquoteclasses $counter1 }}
      {{ if (in $finder "]-") }}
        {{ $inner = $inner | replaceRE `\[!([a-zA-Z]+)\]` `callout-collapsible callout-collapsed ${1}`}}
      {{ else if (in $finder "]+") }}
        {{ $inner = $inner | replaceRE `\[!([a-zA-Z]+)\]` `callout-collapsible ${1}`}}
      {{ else}}
        {{ $inner = $inner | replaceRE `\[!([a-zA-Z]+)\]` `${1}` }}
      {{ end }}
      {{ $inner = printf "blockquote class=\"%s-callout\"" $inner | lower}}
      {{ $content = replace $content . $inner 1}}
      {{ $counter1 = add $counter1 1 }}
    {{ else }}
      {{ $inner := print "blockquote" }}
      {{ $content = replace $content . $inner 1}}
    {{ end }}
    {{ $counter = add $counter 1 }}
  {{end}}
  {{ $content = $content | replaceRE `\[![a-zA-Z]+\][-\+]?` "" }}
  {{ $content = $content | replaceRE "blockquote class=callout" "blockquote" }}
  {{ $content = $content | replaceRE `(?s)(<blockquote class="\S+-callout">.*?)<br>(.*?<\/blockquote)` `${1}</p><p>${2}` }}
{{end}}

{{/* Make ==text== into <mark>text</mark> */}}
{{$mark := findRE "==([^=\n]+)==" $content}}
{{range $mark}}
  {{$fixed := printf "<mark>%s</mark>" (replace . "==" "")}}
  {{$content = replace $content . $fixed}}
{{end}}

{{ $content | safeHTML }}
