有没有办法将JavaScript嵌入到Vitepress的Markdown模板中?

4
有没有办法在Vitepress的markdown中嵌入来自外部源和本地脚本的脚本,以使其生成?
这个例子:
## my test button

<script src="https://www.jsdeliver.com/sdk/js?yadayada"></script>
<script>
  function initButton() {
     ...
  }
</script>

出现了一个问题

[vite] 热更新 /test/index.md (x2) 19:00:17 [vite] 内部服务器错误:在客户端组件模板中,带副作用的标签(和)将被忽略。 插件:vite:vue

1个回答

4

第一种可能的方法是通过配置文件(.vitepress / config.js)将脚本嵌入生成的vitepress index.html中。文档没有很好地解释它,但如果我们需要将脚本放置在头部,则可以使用此方法。

以下是Google标记头脚本的示例。

export default {
  title: 'mydocumentation',
  head: [
    [
      'script',
      {
        async: true,
        src: 'https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxxx'
      }
    ],
    [
      'script',
      {},
      `
      window.dataLayer = window.dataLayer || [];
      ...

      gtag('config', 'G-xxxxxxxxxxx');
      `
    ]
  ]
}

更多上下文:https://vitepress.dev/reference/site-config#example-using-google-analytics - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接