Ver código fonte

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

Bevan Stanely 3 anos atrás
pai
commit
44819d4299
1 arquivos alterados com 36 adições e 0 exclusões
  1. 36 0
      wowchemy/layouts/shortcodes/table.html

+ 36 - 0
wowchemy/layouts/shortcodes/table.html

@@ -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>