tips

Hugo - เปิด External Link ใน tab ใหม่

เราสามารถกำหนดให้เว็บที่สร้างด้วย Hugo ทำการเปิดลิงก์ภายนอกด้วย tab ใหม่ได้
Hugo - เปิด External Link ใน tab ใหม่
Share this

เราสามารถกำหนดให้เว็บที่สร้างด้วย Hugo ทำการเปิดลิงก์ภายนอกด้วย tab ใหม่ได้

เราสามารถ override การ render ลิงก์ของ Hugo โดยการสร้างไฟล์ layouts/_default/_markup/render-link.html แล้วใส่ code ดังนี้

<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if or (strings.HasPrefix .Destination "http") (strings.HasPrefix .Destination "https") }} target="_blank"{{ end }} >{{ .Text | safeHTML }}</a>

โค้ดนี้จะทำการเช็คก่อนการ render link บนหน้าเว็บว่า ถ้าลิงก์ขึ้นต้นด้วย http หรือ https ก็ให้ทำการใส่ target="_blank" เข้าไปในลิงก์ด้วย


credit: https://digitaldrummerj.me/hugo-links-to-other-pages/
Photo by Maria Stewart: pexels.com

Post Views: 345