微格式

微格式 是一套用於在 HTML 中嵌入語義和結構化資料的標準,並提供了一個可供社交網路應用程式、搜尋引擎、聚合器和其他工具使用的 API。這些最小的 HTML 模式用於標記從基本到特定領域的實體,例如人員、組織、事件和位置。

  • 要建立微格式物件,請在 class 屬性中使用 h-* 類名。
  • 要向物件新增屬性,請在物件的後代之一上使用 p-*u-*dt-*e-* 類名。

微格式使用支援詞彙表來描述物件,並使用名稱-值對來為其屬性分配值。屬性包含在可以新增到任何 HTML 元素的 class 屬性中,而資料值則重複使用 HTML 元素內容和語義屬性。

Microformats2(有時稱為 mf2)是對微格式的更新,它提供了一種比以前使用 RDFa 和微資料的方法更簡單的方法來註釋 HTML 結構化語法和詞彙表。這些以前的方法需要學習新的屬性。

大多數語言都提供了 開源的 microformats2 解析庫

微格式的工作原理

網頁作者可以在他們的 HTML 中新增微格式。例如,如果他們想識別自己,他們可以使用一個 h-card,例如

HTML 示例

html
<a class="h-card" href="https://alice.example.com">Alice Blogger</a>

當解析器遇到此資料時,它將知道此頁面包含一個“卡片”,該卡片描述了一個名為 Alice Blogger 的人和組織,其 URL 為 https://alice.example.com/。解析器透過可用於不同應用程式的 API 提供此資料。例如,一個應用程式可以掃描頁面以查詢 h-card,將其用作已註冊服務的使用者的個人資料資訊。

在本例中,一些標記模式只需要一個微格式根類名,解析器使用該類名查詢一些通用屬性,例如 nameurlphoto

微格式用例

微格式有許多用例。首先,Webmention 標準 使用微格式提供了一種從一個站點向另一個站點發送訊息和評論的方法。Webmention 規範定義了站點可以釋出和使用的特定屬性,以建立一種豐富且互操作的釋出訊息和評論的方式。微格式還可以與 Webmentions 一起使用,以實現從一個站點向另一個站點發送社交反應,例如點贊、轉發和書籤。

微格式還支援跨站點的輕鬆聯合釋出。聚合器可以解析釋出了微格式的頁面,以查詢資訊,例如帖子標題、帖子正文和帖子作者。然後,此聚合器可以使用收集到的語義資訊在其站點上呈現結果。例如,新聞聚合器和社群發帖板可以促進提交併使用微格式從頁面中提取相關內容。此外,網站可以使用微格式向第三方傳送精心設計的請求以釋出內容,例如社交網路。

所有主要搜尋引擎都支援讀取和解釋微格式。搜尋引擎從直接訪問這些結構化資料中獲益匪淺,因為它使他們能夠理解網頁上的資訊。藉助這些資訊,搜尋引擎可以向用戶提供更相關的結果。某些搜尋引擎可能會根據微格式中提供的資料在搜尋結果頁面上呈現特殊的程式碼段,例如星級評定。

除了機器可讀之外,微格式還設計為人易讀。這種方法使人們能夠輕鬆理解和維護微格式資料。

微格式字首

所有微格式都包含一個根和一組屬性。屬性都是可選的,並且可能有多個值 - 需要單個值的應用程式可以使用屬性的第一個例項。分層資料以巢狀微格式表示,通常作為屬性值本身。

所有微格式類名都使用字首。字首是**與詞彙表無關的語法**,詞彙表是單獨開發的。

  • **“h-*” 用於根類名**,例如“h-card”、“h-entry”、“h-feed”等等。這些頂級根類通常表示型別和相應的預期屬性詞彙表。例如
  • “p-*” 用於普通(文字)屬性,例如“p-name”,“p-summary”
    • 通用純文字解析,通常為元素文字。在某些 HTML 元素上,首先使用特殊屬性,例如 img/alt,abbr/title。
  • “u-*” 用於 URL 屬性,例如“u-url”,“u-photo”,“u-logo”
    • 特殊解析:元素屬性 a/href,img/src,object/data 等屬性優先於元素內容。
  • “dt-*” 用於日期時間屬性,例如“dt-start”,“dt-end”,“dt-bday”
    • 特殊解析:time 元素的 datetime 屬性,value-class-pattern 和單獨的日期時間值解析以提高可讀性。
  • “e-*” 用於元素樹屬性,其中整個包含的元素層次結構是值,例如“e-content”。“e-” 字首也可以記憶為“element tree”(元素樹),“embedded markup”(嵌入標記)或“encapsulated markup”(封裝標記)。

一些微格式示例

h-card

h-card 微格式表示個人或組織。

每個屬性的值在 HTML 中使用 class 屬性定義,任何元素都可以攜帶該屬性。

h-card 示例

html
<p class="h-card">
  <img class="u-photo" src="https://example.org/photo.png" alt="" />
  <a class="p-name u-url" href="https://example.org">Joe Bloggs</a>
  <a class="u-email" href="mailto:jbloggs@example.com">jbloggs@example.com</a>,
  <span class="p-street-address">17 Austerstræti</span>
  <span class="p-locality">Reykjavík</span>
  <span class="p-country-name">Iceland</span>
</p>
屬性 描述
p-name 個人或組織的完整/格式化名稱。
u-email 電子郵件地址
u-photo 個人或組織的照片
u-url 個人或組織的主頁或其他 URL
u-uid 通用唯一識別符號,最好是規範 URL
p-street-address 街道門牌號 + 街道名稱
p-locality 城市/鎮/村莊
p-country-name 國家名稱

巢狀 h-card 示例

html
<div class="h-card">
  <a class="p-name u-url" href="https://blog.lizardwrangler.com/">
    Mitchell Baker
  </a>
  (<a class="p-org h-card" href="https://mozilla.org/">Mozilla Foundation</a>)
</div>

解析後的 JSON

json
{
  "items": [
    {
      "type": ["h-card"],
      "properties": {
        "name": ["Mitchell Baker"],
        "url": ["https://blog.lizardwrangler.com/"],
        "org": [
          {
            "value": "Mozilla Foundation",
            "type": ["h-card"],
            "properties": {
              "name": ["Mozilla Foundation"],
              "url": ["https://mozilla.org/"]
            }
          }
        ]
      }
    }
  ]
}

在此示例中,為個人及其代表的組織都指定了 h-card。使用 p-org 屬性指定個人與連結組織的關聯。

注意:巢狀的 h-card 具有隱含的“name”和“url”屬性,就像 <a href> 上任何其他僅包含根類名稱的 h-card 一樣。

h-entry

h-entry 微格式表示網路上的分期內容或帶時間戳的內容。h-entry 通常與旨在進行聯合釋出的內容一起使用,例如部落格文章和簡短筆記。

作為部落格文章的 h-entry 示例

html
<article class="h-entry">
  <h1 class="p-name">Microformats are amazing</h1>
  <p>
    Published by
    <a class="p-author h-card" href="https://example.com">W. Developer</a> on
    <time class="dt-published" datetime="2013-06-13 12:00:00">
      13<sup>th</sup> June 2013
    </time>
  </p>

  <p class="p-summary">In which I extoll the virtues of using microformats.</p>

  <div class="e-content">
    <p>Blah blah blah</p>
  </div>
</article>

屬性

屬性 描述
p-name 條目名稱/標題
p-author 誰撰寫了條目,可選地嵌入 h-card
dt-published 條目釋出時間
p-summary 簡短的條目摘要
e-content 條目的完整內容

解析後的回覆 h-entry 示例

html
<div class="h-entry">
  <p>
    <span class="p-author h-card">
      <a href="https://quickthoughts.jgregorymcverry.com/profile/jgmac1106">
        <img
          class="u-photo"
          alt="Greg McVerry"
          src="https://quickthoughts.jgregorymcverry.com/file/2d6c9cfed7ac8e849f492b5bc7e6a630/thumb.jpg" />
      </a>
      <a
        class="p-name u-url"
        href="https://quickthoughts.jgregorymcverry.com/profile/jgmac1106">
        Greg McVerry
      </a>
    </span>
    Replied to
    <a
      class="u-in-reply-to"
      href="https://mdn.club.tw/en-US/docs/Web/HTML/microformats">
      a post on <strong>developer.mozilla.org</strong>
    </a>
    :
  </p>
  <p class="p-name e-content">
    Hey thanks for making this microformats resource
  </p>
  <p>
    <a href="https://quickthoughts.jgregorymcverry.com/profile/jgmac1106">
      Greg McVerry
    </a>
    published this
    <a
      class="u-url url"
      href="https://quickthoughts.jgregorymcverry.com/2019/05/31/hey-thanks-for-making-this-microformats-resource">
      <time class="dt-published" datetime="2019-05-31T14:19:09+0000">
        31 May 2019
      </time>
    </a>
  </p>
</div>
json
{
  "items": [
    {
      "type": ["h-entry"],
      "properties": {
        "in-reply-to": [
          "https://mdn.club.tw/en-US/docs/Web/HTML/microformats"
        ],
        "name": ["Hey thanks for making this microformats resource"],
        "url": [
          "https://quickthoughts.jgregorymcverry.com/2019/05/31/hey-thanks-for-making-this-microformats-resource"
        ],
        "published": ["2019-05-31T14:19:09+0000"],
        "content": [
          {
            "html": "Hey thanks for making this microformats resource",
            "value": "Hey thanks for making this microformats resource",
            "lang": "en"
          }
        ],
        "author": [
          {
            "type": ["h-card"],
            "properties": {
              "name": ["Greg McVerry"],
              "photo": [
                "https://quickthoughts.jgregorymcverry.com/file/2d6c9cfed7ac8e849f492b5bc7e6a630/thumb.jpg"
              ],
              "url": [
                "https://quickthoughts.jgregorymcverry.com/profile/jgmac1106"
              ]
            },
            "lang": "en",
            "value": "Greg McVerry"
          }
        ]
      },
      "lang": "en"
    }
  ]
}

h-feed

h-feedh-entry 文章的流或提要,例如主頁或歸檔頁面上的完整文章,或文章的摘要或其他簡短列表。

h-feed 示例

html
<div class="h-feed">
  <h1 class="p-name">Microformats Blogs</h1>
  <article class="h-entry">
    <h2 class="p-name">Microformats are amazing</h2>
    <p>
      Published by
      <a class="p-author h-card" href="https://example.com">W. Developer</a> on
      <time class="dt-published" datetime="2013-06-13 12:00:00">
        13<sup>th</sup> June 2013
      </time>
    </p>
    <p class="p-summary">
      In which I extoll the virtues of using microformats.
    </p>
    <div class="e-content"><p>Blah blah blah</p></div>
  </article>
</div>

屬性

屬性 描述
p-name 提要名稱
p-author 提要的作者,可選地嵌入 h-card

子項

巢狀的 h-entry
表示提要專案的物件

h-event

h-event 用於網路上的事件。h-event 通常與事件列表和單個事件頁面一起使用。

html
<div class="h-event">
  <h1 class="p-name">Microformats Meetup</h1>
  <p>
    From
    <time class="dt-start" datetime="2013-06-30 12:00">
      30<sup>th</sup> June 2013, 12:00
    </time>
    to <time class="dt-end" datetime="2013-06-30 18:00">18:00</time> at
    <span class="p-location">Some bar in SF</span>
  </p>
  <p class="p-summary">
    Get together and discuss all things microformats-related.
  </p>
</div>

屬性

屬性 描述
p-name 事件名稱(或標題)
p-summary 事件的簡短摘要
dt-start 事件開始的日期時間
dt-end 事件結束的日期時間
p-location 事件發生地點,可選地嵌入 h-card

解析後的 h-event 示例

html
<div class="h-event">
  <h2 class="p-name">IndieWeb Summit</h2>
  <time class="dt-start" datetime="2019-06-29T09:00:00-07:00">
    June 29, 2019 at 9:00am (-0700)
  </time>
  <br />through
  <time class="dt-end" datetime="2019-06-30T18:00:00-07:00">
    June 30, 2019 at 6:00pm (-0700)
  </time>
  <br />
  <div class="p-location h-card">
    <div>
      <span class="p-name">Mozilla</span>
    </div>
    <div>
      <span class="p-street-address">1120 NW Couch St</span>,
      <span class="p-locality">Portland</span>,
      <span class="p-region">Oregon</span>,
      <span class="p-country">US</span>
    </div>
    <data class="p-latitude" value="45.52345"></data>
    <data class="p-longitude" value="-122.682677"></data>
  </div>
  <div class="e-content">Come join us</div>
  <div>
    <span class="p-author h-card">
      <a class="u-url p-name" href="https://aaronparecki.com">Aaron Parecki</a>
    </span>
    Published this
    <a href="https://aaronparecki.com/2019/06/29/1/" class="u-url">event </a>on
    <time class="dt published" datetime="2019-05-25T18:00:00-07:00">
      May 5th, 2019
    </time>
  </div>
</div>
json
{
  "items": [
    {
      "type": ["h-event"],
      "properties": {
        "name": ["IndieWeb Summit"],
        "url": ["https://aaronparecki.com/2019/06/29/1/"],
        "author": [
          {
            "type": ["h-card"],
            "properties": {
              "name": ["Aaron Parecki"],
              "url": ["https://aaronparecki.com"]
            },
            "lang": "en",
            "value": "Aaron Parecki"
          }
        ],
        "start": ["2019-06-29T09:00:00-07:00"],
        "end": ["2019-06-30T18:00:00-07:00"],
        "published": ["2019-05-25T18:00:00-07:00"],
        "content": [
          {
            "html": "Come join us",
            "value": "Come join us",
            "lang": "en"
          }
        ],
        "location": [
          {
            "type": ["h-card"],
            "properties": {
              "name": ["Mozilla"],
              "p-street-address": ["1120 NW Couch St"],
              "locality": ["Portland"],
              "region": ["Oregon"],
              "country": ["US"],
              "latitude": ["45.52345"],
              "longitude": ["-122.682677"]
            },
            "lang": "en",
            "value": "Mozilla"
          }
        ]
      },
      "lang": "en"
    }
  ]
}

微格式 rel 屬性示例

有一些微格式是透過使用特殊的 rel= 屬性應用於頁面的。這些微格式描述了當前文件與連結文件之間的關係。有關這些關係的完整列表,請參閱 microformats wiki 上的 rel 屬性

rel=author

此屬性表示連結文件表示當前頁面的作者。

html
<a rel="author" href="https://jamesg.blog">James Gallagher</a>

rel=license

此屬性表示連結文件包含當前頁面釋出的許可證。

html
<a rel="license" href="https://mit-license.org/">MIT License</a>

rel=nofollow

此屬性表示連結文件不應被可能源自當前頁面的搜尋引擎排名演算法賦予任何權重。這對於防止連結圖演算法在看到指向文件的連結後將頁面的權重提高到高於其原本應該具有的權重非常有用。

html
<a rel="nofollow" href="https://jamesg.blog">James Gallagher</a>

瀏覽器相容性

所有瀏覽器都支援 class 屬性及其 DOM API。

另請參閱