HasShortcode
Syntax
PAGE.HasShortcode NAME
Returns
bool
By example, let’s use MathJax to render a LaTeX mathematical expression:
contents/physics/lesson-1.md
Albert Einstein’s theory of special relativity expresses
the fact that mass and energy are the same physical entity
and can be changed into each other.
{{< math >}}
$$
E=mc^2
$$
{{< /math >}}
In the equation, the increased relativistic mass (m) of a
body times the speed of light squared (c2) is equal to
the kinetic energy (E) of that body.The shortcode is simple:
layouts/shortcodes/math.html
{{ trim .Inner "\r\n" }}Now we can selectively load the required CSS and JavaScript on pages that call the “math” shortcode:
layouts/baseof.html
<head>
...
{{ if .HasShortcode "math" }}
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
{{ end }}
...
</head>