Skip to content

Instantly share code, notes, and snippets.

@kenpetti-toasttab
Last active May 13, 2022 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenpetti-toasttab/c6ae2ad98f237ac8eaff472a4a7b0a2c to your computer and use it in GitHub Desktop.
Save kenpetti-toasttab/c6ae2ad98f237ac8eaff472a4a7b0a2c to your computer and use it in GitHub Desktop.
Hugo annotation shortcode
{{ $mardowned := .Inner | markdownify }}
{{ $optOrg := dict "markup" "html" }}
{{ replaceRE "\\((\\d+)\\)" (trim `<aside id="annotation_$1"><i id="annotation_$1-button" class="fa fa-plus-circle"></i></aside>` "\n") $mardowned | $.Page.RenderString $optOrg }}
{{ with $.Parent }}
{{- if ne $.Parent.Name "annotate" -}}
{{- errorf "annotations must be used within an annotate block" -}}
{{- end -}}
{{- end -}}
{{ $script := slice }}
{{- range $index, $annotation := (findRE "\\d+.*" .Inner) -}}
{{ $annotation_content := replaceRE "\\d+\\." "" $annotation | markdownify }}
{{ $annotation_id := (add $index 1) | printf "%s%d" "annotation_" }}
{{ partial "my-inline-partial.html" (dict "annotation_id" $annotation_id "annotation_content" $annotation_content) }}
{{- end -}}
{{ define "partials/my-inline-partial.html" }}
<script>
$('#{{ .annotation_id }}-button').click(function() {
var annotation = $('#{{ .annotation_id }}')
var annotationButton = $('#{{ .annotation_id }}-button')
var popup = $('#{{ .annotation_id }}-popup')
annotationButton.toggleClass("fa-plus-circle")
annotationButton.toggleClass("fa-times-circle")
if (popup.length) {
popup.remove()
} else {
var popup = $('<div>', {
id: '{{ .annotation_id }}-popup',
class: 'some-class some-other-class',
title: 'now this div has a title!',
css: {
"padding": "0.8rem",
"position": "absolute",
"z-index": 1,
"background": "#eee",
"border-radius": "0.1rem",
"box-shadow": "0 0.2rem 0.5rem rgba(0,0,0,.1),0 0 0.05rem rgba(0,0,0,.25)",
"top": Math.floor(annotationButton.position().top) + 5 + "px",
"left": Math.floor(annotationButton.position().left) + 5 + "px",
}
})
.html("{{ .annotation_content }}")
popup.appendTo(annotation)
}
})
</script>
{{ end }}
title
foo

{{< annotate >}}

this is a codeblock with annotation # (1)
  • this is a list (2) with annotation

    {{< annotations >}}

    1. An annotation 👋
    2. Another annotation 🐶 {{< /annotations >}} {{< /annotate >}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment