一、如何在網頁嵌入 Youtube 影片 #
使用以下程式碼,直接複製到 MarkDown 應該就能正常使用
<div style="max-width: 800px; margin: 24px auto; aspect-ratio: 16 / 9; border-radius: 16px; overflow: hidden; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15); background: #000;">
<iframe
src="https://www.youtube.com/embed/XXXXXXX"
title="YouTube video player"
style="width: 100%; height: 100%; border: 0; display: block;"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowfullscreen
></iframe>
</div>
需要把 XXXXXXX 改成影片 ID ,以這部傳奇影片為例:
他的影片 ID 是 dQw4w9WgXcQ
二、如何給圖片加上附註 #
效果像這樣子:
模板:
<figure>
<img
src="XX.jpg"
alt="圖片內容描述"
loading="lazy"
decoding="async"
>
<figcaption>
圖片來源:<a href="https://example.com" target="_blank" rel="noopener noreferrer">網站名稱</a>
</figcaption>
</figure>
-
img src="XX.jpg":放的是圖片位址 -
<a href="https://example.com" target="_blank">網站名稱</a>:效果是當點擊「網站名稱」4 個字後,會跳到https://example.com,而target="_blank"的意思是點擊後會在新的分頁開啟連結目標 -
alt="":直接空白設定成""也沒關係,他的作用是圖片載入失敗時顯示的文字,或是 Google 等搜尋引擎讀取用(是搜尋引擎優化的一環) -
loading="lazy":是延遲載入,只有當使用者把網頁往下滑、快看到這張圖時,瀏覽器才會開始下載它。 -
decoding="async":非同步解碼。 -
rel="noopener noreferrer":似乎是只要有target="_blank"這種跳到別的網站的功能就建議加上這個
或是只有文字不加入超連結,像是:
模板:
<figure>
<img
src="XX.jpg"
alt="圖片內容描述"
loading="lazy"
decoding="async"
>
<figcaption>
圖片來源:網站名稱
</figcaption>
</figure>
三、備註 #
因為這是使用 HTML 內嵌的方法,如果圖片的 src="" 寫的是相對路徑,RSS 閱讀器會抓不到圖片,比如:
<figure>
<img
src="206A7C~1.webp"
alt="圖片內容描述"
loading="lazy"
decoding="async"
>
<figcaption>
圖片來源:《Summer Pockets REFLECTION BLUE》
</figcaption>
</figure>
"206A7C~1.webp" 這張圖在 RSS 閱讀器會顯示不出來
第一個解決方法是改成局絕對路徑:
<figure>
<img
src="https://yuncolorblog.com/posts/%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98/how-to-embed-youtube-videos-on-a-web-page-and-add-source-information-to-images/206A7C~1.webp"
alt="圖片內容描述"
loading="lazy"
decoding="async"
>
<figcaption>
圖片來源:《Summer Pockets REFLECTION BLUE》
</figcaption>
</figure>
第二個解決方法是修改 RSS 模板檔案,並用正規表達示把路徑自動加上,以本站使用 Hugo Blowfish 為例,要去 \layouts\_default\rss.xml 修改,如果沒有的話需要到 themes/blowfish/layouts/_default/rss.xml 複製一份過來改。
修改方法可以問 AI ,基本上是要加這個東西進去
{{- $content := .Content -}}
{{- $permalink := .Permalink -}}
{{- $content = $content | replaceRE `(<img\s+[^>]*?src=")([^":/]+?)(")` (printf `${1}%s${2}${3}` $permalink) -}}
(至於具體要加哪裡我就不清楚了🫡,因為我的 rss.xml 有修改過,和預設的不太一樣)
或是不用那麼麻煩,如果用的是 Hugo 的 Blowfish,Blowfish 有提供預設的 Shortcode (用來快速插入功能的簡短標記)
★{< figure
src="XX.jpg"
alt="圖片內容描述"
caption="圖片來源:[網站名稱](https://example.com))"
>}}
因為 Blowfish 似乎有 BUG ,即使用了程式碼區塊,還是會顯示圖片,像這樣:
所以我把 { 改成 ★,若要使用,需把 ★ 換回 {