fix: Replacing "internal-link broken" with link to asset (#232)

This commit is contained in:
Apoorv Khandelwal 2022-11-30 17:41:05 -08:00 committed by GitHub
parent 5fd707714f
commit 5a7936e23a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,6 +14,7 @@
{{$codefences := $raw | findRE "\\x60[^\\x60\\n]+\\x60"}} {{$codefences := $raw | findRE "\\x60[^\\x60\\n]+\\x60"}}
{{$codeblocks := $raw | findRE "\\x60{3}[^\\x60]+\\x60{3}"}} {{$codeblocks := $raw | findRE "\\x60{3}[^\\x60]+\\x60{3}"}}
{{$code := union $codefences $codeblocks}} {{$code := union $codefences $codeblocks}}
{{range $wikilinks}} {{range $wikilinks}}
{{$cur := .}} {{$cur := .}}
{{$incode := false}} {{$incode := false}}
@ -22,41 +23,56 @@
{{$incode = true}} {{$incode = true}}
{{end}} {{end}}
{{end}} {{end}}
{{if not $incode}} {{if not $incode}}
{{if (hasPrefix . "!")}}
{{$inner := . | strings.TrimPrefix "![[" | strings.TrimSuffix "]]" }} <!-- remove link delimiters -->
{{$split := split $inner "|"}} {{$inner := . | strings.TrimPrefix "!" | strings.TrimPrefix "[[" | strings.TrimSuffix "]]" }}
{{$path := index $split 0 | relURL}} <!-- split from alias -->
{{$width := index $split 1}} {{$split := split $inner "|"}}
{{$img := printf "<img src=\"%s\" width=\"%s\" />" $path (default "auto" $width)}} <!-- separate link path -->
{{$content = replace $content . $img}} {{$path := index $split 0}}
{{else}}
<!-- remove link delimiters --> {{$reference := split $path "#"}}
{{$inner := . | strings.TrimPrefix "[[" | strings.TrimSuffix "]]" }} <!-- path with heading link removed -->
<!-- split from alias --> {{$title := index $reference 0}}
{{$split := split $inner "|"}} <!-- $display is hyperlink display text -->
<!-- separate link path --> <!-- use alias, else title -->
{{$path := index $split 0}} {{$display := default $title (index $split 1)}}
{{$reference := split $path "#"}} <!-- remove subfolder from title -->
<!-- path with heading link (i.e. $block) removed --> {{$display := index (last 1 (split $display "/")) 0}}
{{$title := index $reference 0}}
<!-- heading link --> {{$curpage := $page.GetPage $title}}
{{$relpath := relURL $path}}
<!-- If path to Hugo page -->
{{if not (eq $curpage.String "nopPage") }}
{{$block := default "" (index $reference 1)}} {{$block := default "" (index $reference 1)}}
{{$block = strings.TrimRight "/" (cond (eq $block "") $block (printf "#%s" $block)) | urlize | lower}} {{$block = strings.TrimRight "/" (cond (eq $block "") $block (printf "#%s" $block)) | urlize | lower}}
{{$href := strings.TrimRight "/" ($page.GetPage $title).RelPermalink}} {{$href := strings.TrimRight "/" $curpage.RelPermalink}}
<!-- if alias given, use alias, else title --> {{$link := printf "<a href=\"%s%s\" rel=\"noopener\" class=\"internal-link\" data-src=\"%s\">%s</a>" $href $block $href $display}}
{{$display := default $title (index $split 1)}} {{$content = replace $content . $link}}
<!-- ADDED LINE TO REMOVE SUBFOLDER FROM TITLE: --> <!-- If path to existing file -->
{{$display := index (last 1 (split $display "/")) 0}} {{else if fileExists $relpath}}
{{if not $href}} {{$splitpath := split $relpath "/"}}
{{$link := printf "<a class=\"internal-link broken\">%s</a>" $display}} {{$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 . "!")}}
{{$width := index $split 1}}
{{$link := printf "<img src=\"%s\" width=\"%s\" />" $href (default "auto" $width)}}
{{$content = replace $content . $link}} {{$content = replace $content . $link}}
{{else}} {{else}}
{{$fullhref := printf "%s%s" $href $block }} {{$link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\">%s</a>" $href $display}}
{{$link := printf "<a href=\"%s\" rel=\"noopener\" class=\"internal-link\" data-src=\"%s\">%s</a>" $fullhref $href $display}}
{{$content = replace $content . $link}} {{$content = replace $content . $link}}
{{end}} {{end}}
<!-- Broken path -->
{{else}}
{{$link := printf "<a class=\"internal-link broken\">%s</a>" $display}}
{{$content = replace $content . $link}}
{{end}} {{end}}
{{end}} {{end}}
{{end}} {{end}}