I have created a custom shortcode to display audio with a waveform using wavesurfer.js. Inefficiencies aside from including the javascript everytime I call the shortcode how can I create a unique identifier for each repeated use of the shortcode in my Markdown?
Shortcode
<script src="https://unpkg.com/wavesurfer.js"></script>
<div id="waveform">
<script type="text/javascript">
var wavesurfer = WaveSurfer.create({
container: document.querySelector('#waveform'),
barWidth: 2,
barHeight: 1, // the height of the wave
barGap: null // the optional spacing between bars of the wave, if not provided will be calculated in legacy format
});
wavesurfer.load("/{{ index .Params 0 }}");
</script>
<button class="btn btn-primary" data-action="play" onclick="wavesurfer.playPause()">Play / Pause</button>
</div>
The div and variable WaveSurfer.create is assigned to need to be unique as what happens now is the button that is created with each call of the shortcode end up only working on the audio file which is downloaded and rendered first. I also need to be able to reference those randomly assigned variables to the onclick field of the button too.
Ideally I’d have a random hash, or some incremental knowledge of how many shortcodes are in the file.
Another way is to take an md5 checksum of whatever field makes each copy of the shortcode unique. For instance, I implemented a view/hide toggle on a bootstrap-based site by checksumming the text to be hidden:
Okay so I have a good grasp now on using .Scratch but the largest issue now is I cannot template javascript. Following the advice on changing my mediaType config so that javascript is rendered with variables I can am a step closer, however, using {{$unique_id}} makes something like:
Right! So I can just use x for all the stuff inside the tags? I think the problem I’m getting then is as you can see there is some nonsense in the string in the but apart from that it should work if the variables are scoped correctly.