When a project gets bigger, it can become annoying to look up content files in a large folder structure. I’m always using Hugo in server-mode to get an instant local preview and wanted to open the content files from there.
So I created two small partials two place before and after an HTML element without a link in my templates. They check if Hugo is running in server-mode and expect a parameter sourcePath with the full path of the project content directory.
If both prerequisites are fullfilled, they wrap the HTML element in a link. This link contains the full local path preceded by vscode://file and opens the content file in a local vscode instance.
vscode_start.html
{{- if .Site.IsServer -}}
{{- with (.Param "sourcePath") -}}
{{ $src := . }}
{{- with $.File -}}
{{- $path := print "vscode://file" $src .Path -}}
<a {{ printf "href=%q" $path | safeHTMLAttr }} rel="nofollow">
{{- end -}}
{{- end -}}
{{ else }}
{{ warnf "Unable to find and open local content files. Missing 'sourcePath' parameter in the configuration." }}
{{ end }}{{- /**/ -}}
vscode_end.html
{{ if .Site.IsServer }}
{{- with (.Param "sourcePath") }}
{{- with $.File }}
</a>
{{- end }}
{{- end }}
{{ end }}{{- /**/ -}}
I’ve published those two also as a small Hugo module.
Maybe this will come in handy for you,
Georg
