mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 15:31:52 +00:00
Merge pull request #2275 from ZhenShuo2021/refactor/gallery
♻️ Refactor(gallery): improve readibility
This commit is contained in:
@@ -1,43 +1,31 @@
|
|||||||
{{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" -}}
|
{{ $id := delimit (slice "gallery" (partial "functions/uid.html" .)) "-" -}}
|
||||||
|
{{ $content := .Inner -}}
|
||||||
|
|
||||||
|
{{/* find all img tags */}}
|
||||||
|
{{ range findRE `<img\s+[^>]*>` $content -}}
|
||||||
|
{{ $imgTag := . -}}
|
||||||
|
{{/* extract src attribute */}}
|
||||||
|
{{ with findRESubmatch `src=['"]([^'"]+)['"]` $imgTag -}}
|
||||||
|
{{ $srcAttr := index (index . 0) 0 -}}
|
||||||
|
{{ $srcValue := index (index . 0) 1 -}}
|
||||||
|
{{ $srcValueFinal := $srcValue -}}
|
||||||
|
|
||||||
|
{{ if or (hasPrefix $srcValue "http://") (hasPrefix $srcValue "https://") -}}
|
||||||
|
{{ with resources.GetRemote $srcValue -}}{{ $srcValueFinal = .RelPermalink -}}{{ end -}}
|
||||||
|
{{ else -}}
|
||||||
|
{{ with $.Page.Resources.GetMatch $srcValue -}}
|
||||||
|
{{ $srcValueFinal = .RelPermalink -}}
|
||||||
|
{{ else -}}
|
||||||
|
{{ with resources.GetMatch $srcValue -}}{{ $srcValueFinal = .RelPermalink -}}{{ end -}}
|
||||||
|
{{ end -}}
|
||||||
|
{{ end -}}
|
||||||
|
|
||||||
|
{{ $newTag := replace $imgTag $srcAttr (printf `src="%s"` $srcValueFinal) -}}
|
||||||
|
{{ $content = replace $content $imgTag $newTag -}}
|
||||||
|
{{ end -}}
|
||||||
|
{{ end -}}
|
||||||
|
|
||||||
|
|
||||||
<div id="{{- $id -}}" class="gallery">
|
<div id="{{- $id -}}" class="gallery">
|
||||||
{{ $page := .Page -}}
|
{{ $content | safeHTML -}}
|
||||||
|
|
||||||
|
|
||||||
<!-- find all img tags -->
|
|
||||||
{{ $imgTagRegex := `<img\s+[^>]*>` -}}
|
|
||||||
{{ $imgTags := findRE $imgTagRegex .Inner -}}
|
|
||||||
{{ $newContent := .Inner -}}
|
|
||||||
|
|
||||||
{{ range $imgTags -}}
|
|
||||||
{{ $imgTag := . -}}
|
|
||||||
<!-- extract src attribute -->
|
|
||||||
{{ $srcRegex := `src=['"]([^'"]+)['"]` -}}
|
|
||||||
{{ $srcMatches := findRESubmatch $srcRegex $imgTag -}}
|
|
||||||
|
|
||||||
{{ if $srcMatches -}}
|
|
||||||
{{ $srcFull := index (index $srcMatches 0) 0 -}}
|
|
||||||
{{ $src := index (index $srcMatches 0) 1 -}}
|
|
||||||
|
|
||||||
{{ $finalSrc := $src -}}
|
|
||||||
{{ $isExternalURL := or (hasPrefix $src "http://") (hasPrefix $src "https://") -}}
|
|
||||||
|
|
||||||
{{ if $isExternalURL -}}
|
|
||||||
{{ with resources.GetRemote $src -}}{{ $finalSrc = .RelPermalink -}}{{ end -}}
|
|
||||||
{{ else -}}
|
|
||||||
{{ with $page.Resources.GetMatch $src -}}
|
|
||||||
{{ $finalSrc = .RelPermalink -}}
|
|
||||||
{{ else -}}
|
|
||||||
{{ with resources.GetMatch $src -}}{{ $finalSrc = .RelPermalink }}{{ end -}}
|
|
||||||
{{ end -}}
|
|
||||||
{{ end -}}
|
|
||||||
|
|
||||||
{{ $newSrc := printf `src="%s"` $finalSrc -}}
|
|
||||||
{{ $newImg := replace $imgTag $srcFull $newSrc -}}
|
|
||||||
{{ $newContent = replace $newContent $imgTag $newImg -}}
|
|
||||||
{{ end -}}
|
|
||||||
{{ end -}}
|
|
||||||
|
|
||||||
{{ $newContent | safeHTML -}}
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user