feat: support link in code block title (#523)

* feat: support link in code block title

* refactor: simplify code block filename link implementation

* docs: update syntax-highlighting.md

* chore: rename `filename_uri_base` to `base_url`

[skip ci]

* refactor: use `base_url` int code block implementation

---------

Co-authored-by: Xin <xin@imfing.com>
This commit is contained in:
strowk 2024-12-28 19:41:23 +07:00 committed by GitHub
parent cf61e606c1
commit 26a298da5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 5 deletions

View File

@ -40,6 +40,22 @@ def say_hello():
print("Hello!") print("Hello!")
``` ```
### Link to File
You can use the `base_url` attribute to provide a base URL that will be combined with the file name to generate a link.
The file name can include a relative path if it specifies the file's location within the base path.
````markdown {filename="Markdown"}
```go {base_url="https://github.com/imfing/hextra/blob/main/",filename="exampleSite/hugo.work"}
go 1.20
```
````
```go {base_url="https://github.com/imfing/hextra/blob/main/",filename="exampleSite/hugo.work"}
go 1.20
```
### Line Numbers ### Line Numbers
To set line numbers, set attribute `linenos` to `table` and optionally set `linenostart` to the starting line number: To set line numbers, set attribute `linenos` to `table` and optionally set `linenostart` to the starting line number:

View File

@ -1,12 +1,13 @@
{{- $class := .Attributes.class | default "" -}} {{- $class := .Attributes.class | default "" -}}
{{- $filename := .Attributes.filename | default "" -}} {{- $filename := .Attributes.filename | default "" -}}
{{- $base_url := .Attributes.base_url | default "" -}}
{{- $lang := .Attributes.lang | default .Type -}} {{- $lang := .Attributes.lang | default .Type -}}
<div class="hextra-code-block hx-relative hx-mt-6 first:hx-mt-0 hx-group/code"> <div class="hextra-code-block hx-relative hx-mt-6 first:hx-mt-0 hx-group/code">
{{ partial "components/codeblock" (dict "filename" $filename "lang" $lang "content" .Inner "options" .Options) }} {{- partial "components/codeblock" (dict "filename" $filename "lang" $lang "base_url" $base_url "content" .Inner "options" .Options) -}}
{{- if or (eq site.Params.highlight.copy.enable nil) (site.Params.highlight.copy.enable) }} {{- if or (eq site.Params.highlight.copy.enable nil) (site.Params.highlight.copy.enable) -}}
{{- partialCached "components/codeblock-copy-button" (dict "filename" $filename) $filename }} {{- partialCached "components/codeblock-copy-button" (dict "filename" $filename) $filename -}}
{{ end }} {{- end -}}
</div> </div>

View File

@ -1,11 +1,27 @@
{{ $filename := .filename | default "" -}} {{ $filename := .filename | default "" -}}
{{ $base_url := .base_url | default "" -}}
{{ $lang := .lang | default "" }} {{ $lang := .lang | default "" }}
{{ $content := .content }} {{ $content := .content }}
{{ $options := .options | default (dict) }} {{ $options := .options | default (dict) }}
{{- if $filename -}} {{- if $filename -}}
<div class="filename" dir="auto">{{ $filename }}</div> <div class="filename not-prose" dir="auto">
{{- if $base_url -}}
{{- $base_url = strings.TrimSuffix "/" $base_url -}}
{{- $filename = strings.TrimPrefix "/" $filename -}}
{{- $file_url := urls.JoinPath $base_url $filename -}}
<a class="hx-no-underline hx-inline-flex hx-items-center hx-gap-1" href="{{ $file_url }}" target="_blank" rel="noopener noreferrer">
<span>{{- $filename -}}</span>
{{- partial "utils/icon" (dict "name" "external-link" "attributes" "height=1em") -}}
</a>
{{- else -}}
{{- $filename -}}
{{- end -}} {{- end -}}
</div>
{{- end -}}
{{- if transform.CanHighlight $lang -}} {{- if transform.CanHighlight $lang -}}
<div>{{- highlight $content $lang $options -}}</div> <div>{{- highlight $content $lang $options -}}</div>
{{- else -}} {{- else -}}