Relative urls in shortcodes

We have our site organized by versions like: (eg: example.org/v1 , example.org/v2)

content/
├── v1/
│   ├── command1.md
│   └── subdir
│         └── command2.md
├── v2/
│   ├── command1.md
│   └── subdir
│         └── command2.md
└── _index.md
... v3,v4 ...

Now inside a shortcode, (that could be used at any depth), I would like to put a link, say to command1.
I can’t use /v1/command1 as that would always point to only the v1’s command1 even if I use the shortcode anywhere in v2/v3 ....
I can’t use ../../command1, as that won’t work at all depths.

What is the right way to generate the correct relative URL in this scenario?

Any help would be great !!

I had hoped for a simpler way to do this, but we have to (a) restrict the search to the current top-level section, and (b) search everywhere in the current section (up, down, and sideways).

The best I have so far is:

{{< link-to-page-in-section "command1" >}}

layouts/shortcodes/link-to-page-in-section.html

{{- $targetPage := .Get 0 }}
{{- with (where .Page.FirstSection.RegularPagesRecursive "File.ContentBaseName" $targetPage) }}
  {{- if gt (len .) 1 }}
    {{- errorf "The %q shortcode was unable to resolve %q to a page (ambiguous target). See %s" $.Name $targetPage $.Position }}
  {{- else }}
    {{- with index . 0 -}}
      <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
    {{- end }}
  {{- end }}
{{- else }}
  {{- errorf "The %q shortcode was unable to resolve %q to a page. See %s" .Name $targetPage .Position }}
{{- end -}}

It throws an error when there are (a) zero matching pages, or (b) multiple matching pages.

You can link to regular pages (including leaf bundles), but not to section pages.