feat: add Table shortcode to render a CSV (#2393)

This commit is contained in:
Bevan Stanely authored and GitHub committed 2021-08-07 13:22:32 +01:00
1 parent 18817339c3
commit 44819d4299
1 file changed
+36
+36
View File
@@ -0,0 +1,36 @@
{{/* Table Shortcode for Wowchemy. */}}
{{/* Load table from page dir falling back back to remote URL */}}
{{/* Supports delimiter separated files. The default delimiter is ',' */}}
{{ $src := .Get "src" }}
{{ $delimiter := .Get "delimiter" | default "," }}
{{ $useHeaderRow := .Get "header" | default true }}
{{ $caption := .Get "caption" }}
{{ $is_remote := strings.HasPrefix $src "http" }}
{{ if not $is_remote }}
{{ $src = path.Join "content" $.Page.File.Dir $src }}
{{ end }}
{{ $rows := getCSV $delimiter $src }}
<table>
{{ if $useHeaderRow }}
{{ $headerRow := index $rows 0 }}
{{ $rows = after 1 $rows }}
<tr> {{ range $headerRow }} <th>{{ . | markdownify | emojify }}</th> {{ end }} </tr>
{{ end }}
{{ range $rows }}
<tr>
{{ range . }}
{{ if (findRE "^\\d+$" .) }}
<td data-table-dtype="number">{{ . }}</td>
{{ else }}
<td data-table-dtype="text">{{ . | markdownify | emojify }}</td>
{{ end }}
{{ end }}
</tr>
{{ end }}
{{ if $caption }}
<caption>{{ $caption | markdownify | emojify }}</caption>
{{ end }}
</table>