【问题标题】:Is there any way to embed JavaScript into Vitepress markdown template?有没有办法将 JavaScript 嵌入到 Vitepress markdown 模板中?
【发布时间】:2023-02-25 04:18:53
【问题描述】:

有没有办法将来自外部源的脚本以及本地脚本嵌入到 Vitepress markdown 中以生成它?

这个例子

## my test button

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

抛出一个问题

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

【问题讨论】:

    标签: javascript html markdown vite vitepress


    【解决方案1】:

    第一种可能的方法是通过配置文件 (.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');
          `
        ]
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-18
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多