Inserting JavaScript block in conversion from markdown to HTML


This is not really an Obsidian question but there are a lot of knowleadgeable and helpful people in this community that perhaps can help. I’m using one of the community plugins (the Pandoc plugin) to export markdown as HTML. One nice feature of the plugin is that it injects the following piece of code to create buttons that allow one to copy the contents of code blocks to the clipboard:

<button class="copy-code-button">Copy</button>

I have figured out how to make this work with the following JavaScript block:

  window.onload = function() {
    const preElements = document.querySelectorAll('pre');
    preElements.forEach((pre, index) => {
      const button = pre.querySelector('button');
      const code = pre.querySelector('code');
      button.addEventListener('click', () => {

I guess I can have this as a snippet and add it manually to every HTML document that I generate but I’m sure there must be some more efficient and elegant way to do this. I figure it must involve adding this somewhere in the template that the plugin uses to generate the HTML. I don’t know how to do this, though.

Can anybody suggest a good way to achieve this goal?

I don’t know offhand, but the info should be in the documentation for the plugin and/or for Pandoc itself.

Yes, for HTML it basically refers you to the Pandoc documentation. I’m looking into it but so far without much success.

No success finding the right pandoc info, or no success applying the info? I guess you’ve looked at ?

The latter. The plugin says that if you want to change the default mode of exporting, you have to use Pandocs templates but this is a bit more complicated than it sounds (at least for me). For one thing it talks about some folder where the Pandoc installation has created the templates folder. I am able to locate where Pandoc is installed in my Mac but I can’t find any folders with the templates.

Anyway, thanks. As I said, this is not properly an Obsidian question. I asked just in case someone was using this plugin and had an answer handy.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.