Skip to content

Commit 4c6cacd

Browse files
authored
Metadata: Add shortcode for shortcode metadata (#347)
1 parent 715426c commit 4c6cacd

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: "Shortcode usage"
3+
layout: shortcode-usage
4+
---
5+
This page demonstrates the `shortcode-usage` shortcode.
6+
7+
The usage is as follows:
8+
``` go-html-template
9+
{{</* shortcode-usage name="include" */>}}
10+
{{</* shortcode-usage name="icon" */>}}
11+
{{</* shortcode-usage name="call-out" */>}}
12+
```
13+
`shortcode-usage` uses regex to parse the shortcode name and attributes. The output shows usages grouped by shortcode name, and sorted by the number of usages.
14+
15+
{{< shortcode-usage name="include" >}}
16+
{{< shortcode-usage name="icon" >}}
17+
{{< shortcode-usage name="call-out" >}}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{{/* Group single-tag includes like:
2+
{{< include "acm/how-to/policies-intro" >}}
3+
{{< include "acm/how-to/policies-proxy-intro.md" >}}
4+
*/}}
5+
6+
{{ $short := .Shortcode }}
7+
{{ $site := .Site }}
8+
9+
<h2>Usages of <code>{{ $short }}</code></h2>
10+
11+
{{ $patQuoted := print `(?s)\{\{\s*(?:<|%)\s*` $short `\b[^}]*?(?:"[^"]+"|'[^']+')[^}]*?(?:%|>)\}\}` }}
12+
13+
{{/* Accumulate: key -> dict(path -> Page) */}}
14+
{{ $byKey := dict }}
15+
16+
{{ range $pg := $site.RegularPages }}
17+
{{ $src := readFile $pg.File.Path }}
18+
{{ $matches := findRE $patQuoted $src }}
19+
{{ if gt (len $matches) 0 }}
20+
{{ range $m := $matches }}
21+
{{/* first quoted string inside the tag */}}
22+
{{ $qs := findRE `(?s)"[^"]+"|'[^']+'` $m }}
23+
{{ if gt (len $qs) 0 }}
24+
{{ $raw := index $qs 0 }}
25+
{{ $key := replaceRE `^['"]|['"]$` "" $raw }}
26+
{{ if ne $key "" }}
27+
{{ $per := default (dict) (index $byKey $key) }}
28+
{{ if not (isset $per $pg.File.Path) }}
29+
{{ $per = merge $per (dict $pg.File.Path $pg) }}
30+
{{ $byKey = merge $byKey (dict $key $per) }}
31+
{{ end }}
32+
{{ end }}
33+
{{ end }}
34+
{{ end }}
35+
{{ end }}
36+
{{ end }}
37+
38+
{{ $scratch := newScratch }}
39+
{{ $scratch.Set "__groups" (slice) }}
40+
{{ range $k, $per := $byKey }}
41+
{{ $scratch.Add "__groups" (dict "k" $k "per" $per "count" (len $per)) }}
42+
{{ end }}
43+
44+
{{ $groups := default (slice) ($scratch.Get "__groups") }}
45+
{{ $groupsSorted := sort $groups "count" "desc" }}
46+
47+
{{ range $g := $groupsSorted }}
48+
{{ $k := index $g "k" }}
49+
{{ $per := index $g "per" }}
50+
{{ $count := index $g "count" }}
51+
52+
<h3 id="include-{{ urlize $k }}"><code>{{ $k }}</code> — used on {{ $count }} {{ if eq $count 1 }}page{{ else }}pages{{ end }}</h3>
53+
<ul>
54+
{{ if gt $count 0 }}
55+
{{ range $path, $p := $per }}
56+
<li><a href="{{ $p.Permalink }}">{{ $p.Title }}</a><small>{{ $p.File.Path }}</small></li>
57+
{{ end }}
58+
{{ else }}
59+
<li><em>No pages found</em></li>
60+
{{ end }}
61+
</ul>
62+
{{ end }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{/*
2+
Usage: {{< shortcode-usage "include" >}} or {{< shortcode name="include" >}}.
3+
*/}}
4+
5+
{{ $name := .Get 0 | default (.Get "name") | default "include" }}
6+
{{ $ctx := dict "Site" .Page.Site "Shortcode" $name }}
7+
{{ partial "shortcode-usage.html" $ctx }}

0 commit comments

Comments
 (0)