我有一個私人資源庫,其中有一些pdf檔案。然而,這些內容是通過MkDocs(材料)和github-pages公開提供的。我想把這些本地可用的pdf檔案嵌入到網站上(用MkDocs創建)。到目前為止,我已經試過這樣做了:
# Method-1
<object data="/path/to/file. pdf" type="application/pdf">。
<embed src="/path/to/file。 pdf" type="application/pdf" />。
</object>
# 方法-2
<a href="/path/to/file. pdf" class="image fit">< i class="fas fa-file-pdf"> </i></a>>
/path/to/file.pdf,當從Google Drive(公開提供的)分享時,可以作業。但是,當我試圖顯示保存在我的github資源庫中的docs檔案夾內的檔案時,它卻不起作用。
我怎樣才能從版本庫本身顯示這些檔案(而不必從 GDrive 復制和共享這些檔案)?
對于github頁面:
- 只有
docs檔案夾的內容被推送到gh-pages分支,然后GitHub Pages將這些內容作為網站發布。
uj5u.com熱心網友回復:
解決方案
使pdf檔案在發布的網站上可用的主要挑戰是創建一個不被破壞的鏈接,并實際從資源庫中訪問該檔案。Mkdocs 附帶了很多擴展,這些擴展增強了該框架的功能。你需要使用pymdownx.pathconverter來解決這一問題。
下面是你需要處理的部分。
A. 安裝PyMdown擴展
連同其他必須具備的條件......pip install
mkdocs
mkdocs-material
mkdocs-material-extensions
pymdown-extensions
B. 更新mkdocs.yml
。
在mkdocs.yml
# Strategy: 使用絕對路徑
markdown_extensions:
- pymdownx.pathconverter:
base_path: 'YOUR_REPO_NAME' # default: ' '
relative_path: '' # default ''
absolute: true # default: false
tags: 'a script img link object embed'/span>
給讀者一個提示。你也可以用pymdownx.pathconverter來使用一個相對路徑。有關細節,請參見檔案。然而,為了簡潔起見,萬一你最終使用了相對路徑,這就是你需要做的:
在
pymdownx.pathconverter的擴展設定中設定absolute: false。使用相對路徑(這應該考慮到你的URL路徑層次)。例如,如果你在一個標記檔案
docs/artifacts/file.pdf中嵌入一個pdf檔案docs/howto/embedding_pdf.md,并且標記檔案的鏈接看起來像http://localhost:8000/demo_mkdocs_project/howto/embedding-a-pdf-file(請看以下章節以了解更多背景),那么在嵌入該檔案時的相對路徑將看起來像:
"././artifacts/file.pdf"。
C. 從資源庫中嵌入PDF檔案
這里我們將假設pdf檔案位于。docs/artifacts/file.pdf,docs檔案夾位于版本庫的根部。在下面的代碼塊中,我在檔案docs/howto/embedding_pdf.md中嵌入了一個pdf檔案。
<!--file: docs/howto/embedding_pdf.md-->
{% with pdf_file = "artifacts/file.pdf" %}.
{% set solid_filepdf = '<i class="fas fa-file-df"> </i>'%}。
{% set empty_filepdf = '<i class="far fa-file-df"> </i>' %}。
##實體。嵌入一個PDF檔案
< object data="{pdf_file }}" type="application/pdf">
<embed src="{{ pdf_file }}" type="application/pdf" />
</object>
##實體。創建一個PDF檔案的鏈接
< a href="{{pdf_file }}" class="image fit"> {{ solid_filepdf }}</a>
{% endwith %}
D. 在mkdocs.yml
中添加一個頁面鏈接。
這將創建一個頂層鏈接HowTo,然后在其下創建另一個鏈接Embedding a PDF file
# file: mkdocs.yml。
nav:
- Home: index.md
- HowTo:
- 嵌入一個PDF檔案: howto/embedding_pdf.md
這四個步驟本質上是使pdf檔案在你的本地服務器(localhost:8000)和github頁面(如果你在那里發布)上都可用。該檔案的鏈接將類似于以下內容:
本地服務器。
http://localhost:8000/demo_mkdocs_project/artifacts/file.pdfGitHub 頁面。
http://githubid.github.io/demo_mkdocs_project/artifacts/file.pdf
注釋:
我假定
- Git用戶ID。
。githubid- 存盤庫名稱。
。demo_mkdocs_project
參考文獻
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333007.html
標籤:
