簡體   English   中英

如何在 MkDocs 中將腳本添加到 head 標簽?

[英]How to add script to head tag in MkDocs?

我是 MkDocs 的新手,我使用 MkDocs 建立了一個網站,我想在<head>...</head>中添加一些腳本。

我在我的 markdown 文件頂部嘗試了這段代碼,但它沒有用:

<head>
<script>
...
</script>
</head>

我發現腳本會顯示在<body>...</body>而不是<head>...</head>中。

如何將<script>放在<head>標簽中?

你可以使用材料。 in your theme (mkdocs.yml), add a reference to custom dir, and in that dir, you can add a file named main.html which will be included in the "head" part See https://squidfunk.github.io /mkdocs-material/customization/#overriding-partials了解更多詳情。

假設您使用 MkDocs Material作為主題,在您的mkdocs.yml中,您可以將條目添加到extra_javascript列表。

mkdocs.yml

site_name: "..."
theme: "..."
# ...
extra_javascript:
  - https://cdn.someCdn.someLibrary.js # Online Resource
  - js/ourJsFile.js # Path relative to "docs" Directory
# Works the same way for CSS!
extra_css:
  - css/someCSS.css # Again, relative to the "docs" Directory

供參考: https://squidfunk.github.io/mkdocs-material/customization/#adding-assets

應該出現在所有主題中的extrahead占位符允許對<head>進行添加。

您需要在 YAML 文件中指定一個custom_dir ,並且在此目錄中有一個帶有main.html塊的extrahead

看:

https://www.mkdocs.org/user-guide/customizing-your-theme/#using-the-theme_dir

嘗試這個:

<!DOCTYPE html>
<html>
  <head>
 <script src="https://file.js"></script>
  </head>
  <body>
    {{ content }}
  </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM