Skip to content

Commit 51019bb

Browse files
committed
Add sidenote shortcode for hugo. Improve test
1 parent c8e19da commit 51019bb

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 -}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ title: "Some title"
44

55

66

7-
This text should have a sidenote next to it.{{< sidenote halign="right" valign="top" >}}This is the note, it is top aligned.{{</ sidenote >}}
7+
This text should have a sidenote next to it.{{< sidenote halign="right" valign="top" numbered="numbered" >}}This is the note, it is top aligned.{{</sidenote>}}

tests/run.scm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
(set-preference "texmacs->markdown:flavour" "hugo")
3030
(set-preference "texmacs->markdown:paragraph-width" #f)
3131
(ftw "./hugo/" run-test)
32-
(ftw "./tfl/" run-test)
3332
(set-preference "texmacs->markdown:hugo-extensions" save-hugo)
3433
(set-preference "texmacs->markdown:paragraph-width" save-width)))

0 commit comments

Comments
 (0)