|
| 1 | +{{/* Simple sidenotes. |
| 2 | +Assumes main content to be col-md-9 and the rows to be justify-content-end. |
| 3 | +Some content breaks paragraphs! |
| 4 | + |
| 5 | +Usage: |
| 6 | + |
| 7 | +{{< sidenote class="optional CSS class" valign=<top,center,bottom> |
| 8 | + numbered=numbered raw=raw header="Some text" >}} |
| 9 | +Content here. |
| 10 | +{{< /sidenote >}} |
| 11 | +Parameters: |
| 12 | + class: one or more CSS class names |
| 13 | + valign: where the bottom of the sidenote will be placed wrt the inline content. |
| 14 | + "top" is the default: the sidenote starts at the height it is inserted. |
| 15 | + "center" shifts it by 50% upwards, "bottom" by 100%. |
| 16 | + numbered: whether to prefix the content with an autoincreased counter and |
| 17 | + display it in the main text. |
| 18 | + raw: set to true if there are nested shortcodes, e.g. {{<figure>}} or {{<toc>}} |
| 19 | + header: This will be rendered bold and centered above the content |
| 20 | +*/}} |
| 21 | +{{- $this := . -}} |
| 22 | +{{- $class := "sidenote" -}} |
| 23 | +{{- with .Get "valign" -}} |
| 24 | + {{- if (eq . "center") -}} |
| 25 | + {{- $class = printf "%s %s" $class (safeHTMLAttr "sidenote-center") -}} |
| 26 | + {{- end -}} |
| 27 | + {{- if (eq . "bottom") -}} |
| 28 | + {{- $class = printf "%s %s" $class (safeHTMLAttr "sidenote-bottom") -}} |
| 29 | + {{- end -}} |
| 30 | +{{- end -}} |
| 31 | +{{- with .Get "class" -}} |
| 32 | + {{- $class = printf "%s %s" $class (safeHTMLAttr .) -}} |
| 33 | +{{- end -}} |
| 34 | +{{- $refText := "" -}} |
| 35 | +{{- $content := .Inner -}} |
| 36 | +{{- with .Get "numbered" -}} |
| 37 | + {{- $this.Page.Scratch.Add "sidenote-counter" 1 -}} |
| 38 | + {{- $refText = printf "%d" ($this.Page.Scratch.Get "sidenote-counter") -}} |
| 39 | + {{- $content = printf "<span class=\"sidenote-ref\">%s</span> %s" $refText $content -}} |
| 40 | +{{- end -}} |
| 41 | +{{- if not (.Get "raw") -}} |
| 42 | + {{- $content = $content | markdownify -}} |
| 43 | + {{- $content = replace $content "<p>" "<span class=\"sidenote-paragraph\">" -}} |
| 44 | + {{- $content = replace $content "</p>" "</span>" -}} |
| 45 | +{{- end -}} |
| 46 | +{{- if .Inner -}} |
| 47 | + {{/*HACKY and incomplete. See https://stackoverflow.com/a/9852381/493464*/}} |
| 48 | + {{- $hasNonPhrasingContent := findRE "<(p|div|pre|h[1-5]|figure|nav)" $content -}} |
| 49 | + {{- if or $hasNonPhrasingContent (.Get "raw") -}} |
| 50 | + <aside class="{{$class}}"> |
| 51 | + {{- with .Get "header" -}} |
| 52 | + <div class="font-weight-bold mb-1 text-center">{{.}}</div> |
| 53 | + {{- end -}} |
| 54 | + {{- $content | safeHTML -}} |
| 55 | + </aside> |
| 56 | + {{- else -}} |
| 57 | + <span class="{{$class}}"> |
| 58 | + {{- with .Get "header" -}} |
| 59 | + <span class="font-weight-bold mb-1 text-center">{{.}}</span> |
| 60 | + {{- end -}} |
| 61 | + {{- $content | safeHTML -}} |
| 62 | + </span> |
| 63 | + {{- end -}} |
| 64 | + <span class="sidenote-ref">{{- $refText -}}</span> |
| 65 | +{{- else -}} |
| 66 | + <div class="alert alert-danger" role="alert"> |
| 67 | + Warning: unclosed sidenote shortcode |
| 68 | + </div> |
| 69 | +{{- end -}} |
0 commit comments