I also find the typical practice of queuing globally or from inside shortcodes problematic.
I had a similar challenge with implementing code highlight on my site — I only wanted scripts and style to load as necessary. I ended up checking content for <pre>
tag as condition to output:
{% if '<pre>' in get_the_content() %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/solarized_dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
{% if has_term( 'twig', 'post_tag' ) %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/twig.min.js"></script>
{% endif %}
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('pre').each(function (i, e) {
hljs.highlightBlock(e)
});
});
</script>
{% endif %}
This is in Twig template (and doesn’t use WP queue), but you can use same approach in PHP just as well.