表格渲染鉤子
創建表格渲染鉤子模板以覆蓋 Markdown 表格到 HTML 的渲染。
New in
v0.134.0
上下文
表格_渲染鉤子_ 模板接收以下 上下文:
- Attributes
- (
map) Markdown 屬性,如果您的站點配置如下則可用:markup: goldmark: parser: attribute: block: true[markup] [markup.goldmark] [markup.goldmark.parser] [markup.goldmark.parser.attribute] block = true{ "markup": { "goldmark": { "parser": { "attribute": { "block": true } } } } } - Ordinal
- (
int) 頁面上表格的從零開始的序號。 - Page
- (
page) 對當前頁面的引用。 - PageInner
- (
page) 對通過RenderShortcodes方法嵌套的頁面的引用。詳見。 - Position
- (
string) 表格在頁面內容中的位置。 - THead
- (
slice) 表格標題行的切片,其中每個元素是表格單元格的切片。 - TBody
- (
slice) 表格主體行的切片,其中每個元素是表格單元格的切片。
表格單元格
THead 和 TBody 方法返回的切片切片中的每個表格單元格具有以下字段:
- Alignment
- (
string) 表格單元格內文本的對齊方式,left、center或right之一。 - Text
- (
template.HTML) 表格單元格內的文本。
示例
在默認配置中,Hugo 根據 GitHub Flavored Markdown 規范 渲染 Markdown 表格。要創建執行相同操作的渲染鉤子:
layouts/_markup/render-table.html
<table
{{- range $k, $v := .Attributes }}
{{- if $v }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
{{- end }}>
<thead>
{{- range .THead }}
<tr>
{{- range . }}
<th
{{- with .Alignment }}
{{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
{{- end -}}
>
{{- .Text -}}
</th>
{{- end }}
</tr>
{{- end }}
</thead>
<tbody>
{{- range .TBody }}
<tr>
{{- range . }}
<td
{{- with .Alignment }}
{{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
{{- end -}}
>
{{- .Text -}}
</td>
{{- end }}
</tr>
{{- end }}
</tbody>
</table>PageInner 詳情
PageInner 的主要用途是相對於包含的 Page 解析鏈接和 頁面資源。例如,創建一個 “include” 短代碼,從多個內容文件組合頁面,同時保留腳注和目錄的全局上下文:
layouts/_shortcodes/include.html
{{ with .Get 0 }}
{{ with $.Page.GetPage . }}
{{- .RenderShortcodes }}
{{ else }}
{{ errorf "短代碼 %q 無法找到 %q。參見 %s" $.Name . $.Position }}
{{ end }}
{{ else }}
{{ errorf "短代碼 %q 需要一個位置參數來指示要包含的文件的邏輯路徑。參見 %s" .Name .Position }}
{{ end }}然後在您的 Markdown 中調用短代碼:
content/posts/post-1.md
{{% include "/posts/post-2" %}}在渲染 /posts/post-2 時觸發的任何渲染鉤子將獲得:
- 調用
Page時獲得/posts/post-1 - 調用
PageInner時獲得/posts/post-2
如果不相關,PageInner 會回退到 Page 的值,並且始終返回一個值。
PageInner 方法僅與調用 RenderShortcodes 方法的短代碼相關,並且您必須使用 Markdown 表示法 調用短代碼。
作為實際示例,Hugo 的嵌入式鏈接和圖像渲染鉤子使用 PageInner 方法來解析 Markdown 鏈接和圖像目標。請參閱各自的源代碼: