Frontmatter
เรียนรู้วิธีใช้ frontmatter ในไฟล์ MDX
frontmatter เป็นส่วนสำคัญของไฟล์ MDX ใช้สำหรับกำหนดข้อมูลเกี่ยวกับเนื้อหาของไฟล์ เช่น title, description, author, publication date ฯลฯ
ส่วนหัวเมตาดาต้าถูกกำหนดไว้ที่จุดเริ่มต้นของไฟล์ ระหว่าง --- (ขีดกลางสามตัว) ตอนต้นและตอนท้าย
รูปแบบ
ส่วนหัวเมตาดาต้าถูกกำหนดในรูปแบบ key-value โดย key คือชื่อของเมตาดาต้า และ value คือเนื้อหาของเมตาดาต้านั้น
ตัวอย่าง:
---
title: Document title
description: Document description
---เมตาดาต้าที่ใช้ได้จะแตกต่างกันไปตามประเภทของเอกสาร (เอกสาร, blog post ฯลฯ)
เมตาดาต้าสำหรับเอกสาร
titledescriptionlinkssourcedocsblogapi
ตัวอย่าง:
---
title: Document title
description: Document description
links:
source: https://google.com
docs: /docs
blog: /blog
api: https://google.com
---เมตาดาต้าสำหรับ blog posts
author_id
นี่คือตัวระบุของผู้เขียนโพสต์
ต้องตรงกับ id ของผู้เขียนที่กำหนดไว้ในไฟล์ src/config/blog.ts
ซึ่งเป็นไฟล์ที่มีข้อมูลครบถ้วนของผู้เขียน
og_image
นี่คือรูปภาพที่จะใช้เป็น featured image ของโพสต์เมื่อแชร์บนโซเชียลมีเดีย หากคุณไม่ต้องการใช้รูปภาพที่สร้างขึ้นโดยอัตโนมัติ
รูปภาพต้องถูกเก็บไว้ในโฟลเดอร์ public/blog-og
ใช้เฉพาะชื่อไฟล์รูปภาพและนามสกุลเท่านั้น
titleexcerptdateauthor_idog_imagetagslinkssourcedocsblogapi
ตัวอย่าง:
---
title: Post title
date: 2024-08-07 22:33:00
excerpt: Post excerpt
author_id: daltonmenezes
og_image: introducing-blogs-og.jpg
links:
source: https://google.com
docs: /docs
blog: /blog
api: https://google.com
tags: [next.js, open-graph, blog]
---