Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/content/docs/guide/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ go 1.20
go 1.20
```

### Overriding Link Text

For links that do not contain the desired filename of the code snippet, you can use `name_override` to provide an overridden name for your link.

````markdown {filename="name_override"}
```c++ {base_url="https://godbolt.org/z/6Y7v7e19f", name_override="default godbolt"}
// Type your code here, or load an example.
int square(int num) {
return num * num;
}
```
````

```c++ {base_url="https://godbolt.org/z/6Y7v7e19f", name_override="default godbolt"}
// Type your code here, or load an example.
int square(int num) {
return num * num;
}
```

### Line Numbers

To set line numbers, set attribute `linenos` to `table` and optionally set `linenostart` to the starting line number:
Expand Down
3 changes: 2 additions & 1 deletion layouts/_markup/render-codeblock.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{{- $class := .Attributes.class | default "" -}}
{{- $filename := .Attributes.filename | default "" -}}
{{- $base_url := .Attributes.base_url | default "" -}}
{{- $name_override := .Attributes.name_override | default "" -}}
{{- $lang := .Attributes.lang | default .Type -}}


<div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code">
{{- partial "components/codeblock" (dict "filename" $filename "lang" $lang "base_url" $base_url "content" .Inner "options" .Options) -}}
{{- partial "components/codeblock" (dict "filename" $filename "lang" $lang "base_url" $base_url "name_override" $name_override "content" .Inner "options" .Options) -}}

{{- if or (eq site.Params.highlight.copy.enable nil) (site.Params.highlight.copy.enable) -}}
{{- partialCached "components/codeblock-copy-button" (dict "filename" $filename) $filename -}}
Expand Down
9 changes: 5 additions & 4 deletions layouts/_partials/components/codeblock.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{{ $filename := .filename | default "" -}}
{{ $base_url := .base_url | default "" -}}
{{ $name_override := .name_override | default $filename -}}
{{ $lang := .lang | default "" }}
{{ $content := .content }}
{{ $options := .options | default (dict) }}

{{- if $filename -}}
{{- if $name_override -}}
<div class="hextra-code-filename not-prose" dir="auto">
{{- if $base_url -}}

Expand All @@ -13,11 +14,11 @@
{{- $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>
<span>{{- $name_override -}}</span>
{{- partial "utils/icon" (dict "name" "external-link" "attributes" "height=1em") -}}
</a>
{{- else -}}
{{- $filename -}}
{{- $name_override -}}
{{- end -}}
</div>
{{- end -}}
Expand All @@ -26,4 +27,4 @@
<div>{{- highlight $content $lang $options -}}</div>
{{- else -}}
<div><pre><code>{{ $content }}</code></pre></div>
{{- end -}}
{{- end -}}