Skip to content

Instantly share code, notes, and snippets.

@TheCoreMan
Created April 18, 2023 22:42
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 TheCoreMan/9fff946f3cda9b98e5b8118d34318c47 to your computer and use it in GitHub Desktop.
Save TheCoreMan/9fff946f3cda9b98e5b8118d34318c47 to your computer and use it in GitHub Desktop.
Hugo flashcard HTML Shortcode
<!-- Made by Shay Nehmad, feel free to use. For details see
https://mrnice.dev/posts/hugo-flashcard-shortcode-for-study/
-->
<!-- This "with" is for error handling, see
https://gohugo.io/templates/shortcode-templates/#error-handling-in-shortcodes
-->
{{ with .Get "question" }}
<!-- get a random string to add to each ID -->
{{ $seed := now.Format ":time_full" }} {{ $random := delimit (shuffle (split
(md5 $seed) "" )) "" }}
<!-- create a unique ID for the answer, based on the question's SHA and the
random string. -->
{{ $questionSHA := . | sha1 }} {{ $answerID := printf "answer-id-%s-%s"
$questionSHA $random }}
<!-- The CSS and JS for the flashcard -->
<link rel="stylesheet" href="/css/flashcard.css" />
<script src="/js/flashcard.js"></script>
<!-- The flashcard itself -->
<div class="flashcard">
<div class="notepad-line"></div>
<button
class="flashcard__front"
onclick="toggleAnswer({{ $answerID }})"
role="button"
>
{{ . | markdownify }}
</button>
<div id="{{ $answerID }}" class="flashcard__back">
{{ $.Inner | markdownify }}
</div>
</div>
<!-- Error handling stuff -->
{{ else }} {{ errorf "missing value for param 'question': %s" .Position }} {{
end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment