1- {{- $u := urls.Parse .Destination -}}
1+ <!-- ==============================================================================
2+ - File: render-image.html (Markdown Image Render Hook)
3+ - Author: Amey Thakur
4+ - Profile: https://github.com/Amey-Thakur
5+ - Repository: https://github.com/Amey-Thakur/Amey-Thakur.github.io
6+ - Release Date: December 16, 2025
7+ - License: MIT License
8+ - ==============================================================================
9+ -
10+ - DESCRIPTION:
11+ - This render hook intercepts the standard Markdown image syntax to provide
12+ - enhanced architectural control over visual assets. It facilitates
13+ - resource-aware path resolution and lazy-loading performance.
14+ -
15+ - HOW IT WORKS:
16+ - The script parses the image destination URL to distinguish between absolute
17+ - and relative paths. It utilizes Hugo's resource management to resolve local
18+ - assets from page bundles or the global assets directory, preserving query
19+ - parameters and fragments. It eventually renders a semantic <img> tag with
20+ - optimized attributes.
21+ -
22+ - TECH STACK:
23+ - - Hugo Goldmark Render Hooks
24+ - - URL Parsing & Resource Resolution
25+ - - HTML5 Optimization (Lazy-loading)
26+ -
27+ - ============================================================================== -->
28+
29+ {{- /* Path Orchestration: Resolving the image source across local and global scopes. */ -}}
30+ {{- $dest := .Destination -}}
31+ {{- $u := urls.Parse $dest -}}
232{{- $src := $u.String -}}
33+
34+ {{- /* Relational Resolution: Handling non-absolute paths via Hugo resource pipeline. */ -}}
335{{- if not $u.IsAbs -}}
4- {{- $path := strings.TrimPrefix "./" $u.Path }}
5- {{- with or (.PageInner.Resources.Get $path) (resources.Get $path) -}}
6- {{- $src = .RelPermalink -}}
7- {{- with $u.RawQuery -}}
8- {{- $src = printf "%s?%s" $src . -}}
9- {{- end -}}
10- {{- with $u.Fragment -}}
11- {{- $src = printf "%s#%s" $src . -}}
12- {{- end -}}
13- {{- end -}}
36+ {{- $path := strings.TrimPrefix "./" $u.Path }}
37+ {{- /*
38+ Resolution Logic: We prioritize page-specific resources before checking
39+ the global asset directory. Note: Using .Page instead of .PageInner.
40+ */ -}}
41+ {{- with or (.Page.Resources.Get $path) (resources.Get $path) -}}
42+ {{- $src = .RelPermalink -}}
43+ {{- with $u.RawQuery -}}
44+ {{- $src = printf "%s?%s" $src . -}}
1445{{- end -}}
15- {{- $attributes := merge .Attributes (dict "alt" .Text "src" $src "title" (.Title | transform.HTMLEscape) "loading" "lazy") -}}
16- < img
17- {{- range $k, $v : = $attributes -}}
18- {{- if $v -}}
19- {{- printf " %s=%q " $k $v | safeHTMLAttr -}}
20- {{- end -}}
21- {{- end -}}>
22- {{- /**/ -}}
23-
46+ {{- with $u.Fragment -}}
47+ {{- $src = printf "%s#%s" $src . -}}
48+ {{- end -}}
49+ {{- end -}}
50+ {{- end -}}
51+
52+ {{- /* Attribute Synthesis: Merging markdown parameters with architectural defaults. */ -}}
53+ {{- $attributes := merge .Attributes (dict "alt" .Text "src" $src "title" (.Title | transform.HTMLEscape) "loading"
54+ "lazy") -}}
55+ < img {{- range $k, $v : =$attributes -}} {{- if $v -}} {{- printf " %s=%q " $k $v | safeHTMLAttr -}} {{- end -}} {{- end
56+ -}}>
57+ {{- /**/ -}}
0 commit comments