I would use a fenced code block in your markdown files, conditionally adding the JavaScript library if the page has one or more echarts.
git clone --single-branch -b hugo-forum-topic-45500 https://github.com/jmooring/hugo-testing hugo-forum-topic-45500
cd hugo-forum-topic-45500
hugo server
Files of interest:
- layouts/_default/_markup/render-codeblock-echart.html
- layouts/_default/baseof.html (look within the
head
element for the conditional JS library inclusion)