hextra icon indicating copy to clipboard operation
hextra copied to clipboard

Support full URL when linking to file within code block

Open imfing opened this issue 4 months ago • 2 comments

Discussed in https://github.com/imfing/hextra/discussions/799

Originally posted by Aerosand August 27, 2025 https://imfing.github.io/hextra/versions/latest/docs/guide/syntax-highlighting/#link-to-file

The way to link a file in a code block is not convenient now when I write my own docs.

The base_url attribute provides a base URL that will be combined with the file name to generate a link.

Some file url may not follow this kind of format.

Is it more general to use the file's full url directly?

imfing avatar Aug 27 '25 18:08 imfing

For this issue I am proposing to replace this line:

{{- $file_url := urls.JoinPath $base_url $filename -}}

with the following logic that supports an optional full_url, while also keeping the old logic for base_url + filename:

{{- $file_url := "" -}}
{{- if $full_url -}}
  {{- $file_url = $full_url -}}
{{- else if $base_url -}}
  {{- $file_url = urls.JoinPath $base_url $filename -}}
{{- else -}}
  {{- $file_url = $filename -}}
{{- end -}}

Declaring $file_url first ensures the variable exists before the conditional assignments.

If full_url is provided, it will be used directly. Otherwise, it falls back to combining base_url and filename, or just using filename if no base_url exists. This way the old method still works while also supporting full URLs that do not fit the old standard formatting.

wharris23 avatar Aug 28 '25 22:08 wharris23

This looks reasonable. Could you please submit a pull request?

imfing avatar Aug 30 '25 12:08 imfing