How to get the path of the parent with a cascade param?

Hey all,

I am trying to calculate the direct link to the file source in git. However, the content comes from multiple git repositories, so I cannot just use .File.Dir to get the path within the git repo.

I can have the file content/dir/_index.md with property cascade.git: https://github.com/my/repo. Then to get the path to content/dir/example/index.md in the git repo I would need the path relative to content/dir (https://github.com/my/repo/blob/master/example/index.md).

I can also cascade the directory where the _index.md file is and then calculate the relative path with that param and .File.Dir. But I would have to remember to keep them updated it if their path changes.

Is there a way to get the path to the file that is cascading the git param without having to hardcode it?

You could try and walk up the .Parent pages until you find the .Page highest on the tree with the same param value.

1 Like

Thanks I did not think about that :smile: . Here is my take for reference.

On main file

{{ $filePath := .File.Path }}
{{ $parentPath := partial "getPath.html" . }}
<p>
The dir of parent is {{ $parentPath }}<br>
The path of file is {{ $filePath }}<br>
The rel path is {{ strings.TrimPrefix $parentPath $filePath}}
</p>

partial/getPath.html

{{ $returnPath := "" }}
{{ $hasParentWithParam := false }}
{{ if .Parent }}
{{ $hasParentWithParam = eq (index .Params "aParam") (index .Parent.Params "aParam") }}
{{ end }}
{{ if $hasParentWithParam }}
{{ $returnPath = partial "getPath.html" .Parent }}
{{ else }}
{{ $returnPath = .File.Dir }}
{{ end }}
{{ return $returnPath }}

output 0

The dir of parent is b/
The path of file is b/c/file.md
The rel path is c/file.md

output 1

The dir of parent is b/
The path of file is b/file.md
The rel path is file.md

output 2

The dir of parent is /
The path of file is a/file.md
The rel path is a/file.md