Skip to content

Commit

Permalink
feat: support global configs on lineNos, wrap and header for code blocks
Browse files Browse the repository at this point in the history
Closes #1416
  • Loading branch information
HEIGE-PCloud committed Dec 29, 2024
1 parent 3203c35 commit 3bb57f3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ bundle = false
# the maximum number of lines of displayed code by default
# 默认展开显示的代码行数
maxShownLines = 10
# whether to enable line numbers in the code block
# 是否显示代码行号
lineNos = true
# whether to enable line wrapping in the code block
# 是否开启代码换行
wrap = false
# whether to display the header in the code block
# 是否显示代码块标题
header = true
# Table config
# 表格配置
[page.table]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ Please open the code block below to view the complete sample configuration {{< f
[params.page.code]
# the maximum number of lines of displayed code by default
maxShownLines = 10
# whether to enable line numbers in the code block
lineNos = true
# whether to enable line wrapping in the code block
wrap = false
# whether to display the header in the code block
header = true
# {{< version 0.2.14 >}} Table config
[params.page.table]
# whether to enable sorting in the tables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ optimizeImages = true
[params.page.code]
# 默认展开显示的代码行数
maxShownLines = 10
# 是否显示代码行号
lineNos = true
# 是否开启代码换行
wrap = false
# 是否显示代码块标题
header = true
# {{< version 0.2.14 >}} 表格配置
[params.page.table]
# 是否开启表格排序
Expand Down
18 changes: 11 additions & 7 deletions layouts/_default/_markup/render-codeblock.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{{ $result := transform.HighlightCodeBlock . }}
{{- $result := transform.HighlightCodeBlock . -}}
{{- $params := page.Scratch.Get "params" -}}
{{- $id := dict "Scratch" .Page.Scratch | partial "function/id.html" -}}
{{- $title := .Attributes.title | default .Type | default "text" -}}
{{- $lineNos := cond (eq .Options.linenos false) "" "show-line-numbers" -}}
{{- $lineNos := .Options.linenos | default $params.code.linenos | default true -}}
{{- $showLineNumbers := cond (eq $lineNos false) "" "show-line-numbers" -}}
{{- $lines := len (split .Inner "\n") -}}
{{- $maxShownLines := .Page.Params.code.maxshownlines | default site.Params.page.code.maxshownlines -}}
{{- $maxShownLines := $params.code.maxshownlines -}}
{{- $open := "is-open" -}}
{{- if eq .Attributes.open nil -}}
{{- $open = cond (gt $lines $maxShownLines) "is-closed" "is-open" -}}
{{- else -}}
{{- $open = cond (eq .Attributes.open false) "is-closed" "is-open" -}}
{{- end -}}
{{- $wrap := cond (eq .Attributes.wrap true) "is-wrap" "" -}}
{{- $header := cond (eq .Attributes.header false) "tw-hidden" "" -}}
<div class="code-block highlight {{ $open }} {{ $lineNos }} {{ $wrap }} tw-group tw-my-2">
{{- $wrap := .Attributes.wrap | default $params.code.wrap | default false -}}
{{- $isWrap := cond (eq $wrap true) "is-wrap" "" -}}
{{- $header := .Attributes.header | default $params.code.header | default true -}}
{{- $showHeader := cond (eq $header false) "tw-hidden" "" -}}
<div class="code-block highlight {{ $open }} {{ $showLineNumbers }} {{ $isWrap }} tw-group tw-my-2">
<div class="
code-block-title
{{ $header }}
{{ $showHeader }}
tw-flex
tw-flex-row
tw-justify-between
Expand Down

0 comments on commit 3bb57f3

Please sign in to comment.