国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 web前端 js教程 從Astro開始的內容收集開始

從Astro開始的內容收集開始

Feb 07, 2025 pm 02:18 PM

Astro 內容集合入門:構建強大的內容模型

本文節(jié)選自 SitePoint Premium 上現(xiàn)已發(fā)行的《釋放 Astro 的力量》一書。我們將學習如何利用 Astro 的內容集合功能構建靈活且可擴展的內容模型。

Getting Started with Content Collections in Astro

Astro 使用特殊的 src/content 文件夾來管理內容集合。您可以創(chuàng)建子文件夾來組織不同的內容集合,例如 src/content/dev-blogsrc/content/corporate-blog。

Getting Started with Content Collections in Astro

每個內容集合都可以在配置文件(例如 /src/content/config.js.ts)中進行配置,並使用 Zod 定義集合模式。 Zod 是一個“基於 TypeScript 的模式驗證工具,具有靜態(tài)類型推斷功能”,已集成到 Astro 中。

以下是一個配置示例:

// src/content/config.js
import { z, defineCollection } from 'astro:content';

const devBlogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string().default('The Dev Team'),
    tags: z.array(z.string()),
    date: z.date(),
    draft: z.boolean().default(true),
    description: z.string(),
  }),
});

const corporateBlogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string(),
    date: z.date(),
    featured: z.boolean(),
    language: z.enum(['en', 'es']),
  }),
});

export const collections = {
  devblog: devBlogCollection,
  corporateblog: corporateBlogCollection,
};

代碼中定義了兩個內容集合:“開發(fā)者博客”和“企業(yè)博客”。 defineCollection 方法允許您為每個集合創(chuàng)建模式。

Markdown 文件和前端內容

本教程中的內容集合示例假設 .md 文件包含與配置文件中指定的模式匹配的前端內容。例如,一個“企業(yè)博客”文章可能如下所示:

---
title: 'Buy!!'
author: 'Jack from Marketing'
date: 2023-07-19
featured: true
language: 'en'
---

# Some Marketing Promo

This is the best product!

Slug 生成

Astro 會根據(jù)文件名自動生成文章的 slug。例如,first-post.md 的 slug 為 first-post。如果在前端內容中提供 slug 字段,Astro 將使用自定義 slug。

請注意,export const collections 對像中指定的屬性必須與內容所在的文件夾名稱匹配(並且區(qū)分大小寫)。

數(shù)據(jù)查詢

準備好 Markdown 文件(位於 src/content/devblogsrc/content/corporateblog)和 config.js 文件後,您可以開始查詢集合中的數(shù)據(jù):

---
import { getCollection } from 'astro:content';
const allDevPosts = await getCollection('devblog');
const allCorporatePosts = await getCollection('corporateblog');
---
{JSON.stringify(allDevPosts)}
{JSON.stringify(allCorporatePosts)}

getCollection 方法可用於檢索給定集合中的所有條目。示例中檢索了“開發(fā)者博客”(devblog)和“企業(yè)博客”(corporateblog)中的所有文章。模板中使用 JSON.stringify() 返回原始數(shù)據(jù)。

除了前端內容數(shù)據(jù)外,返回的數(shù)據(jù)還包含 idslugbody 屬性(body 屬性包含文章內容)。

您還可以通過迭代所有文章來過濾草稿或特定語言的文章:

import { getCollection } from 'astro:content';

const spanishEntries = await getCollection('corporateblog', ({ data }) => {
  return data.language === 'es';
});

getCollection 返回所有文章,但您也可以使用 getEntry 返回集合中的單個條目:

import { getEntry } from 'astro:content';

const singleEntry = await getEntry('corporateblog', 'pr-article-1');

getCollection vs getEntries

雖然有兩種方法可以從集合中返回多篇文章,但兩者之間存在細微差別。 getCollection() 根據(jù)集合名稱檢索內容集合條目的列表,而 getEntries() 檢索來自同一集合的多個集合條目。

Astro 文檔中給出了 getEntries() 用於檢索內容的示例,其中使用了引用實體(例如,相關的文章列表)。

內容顯示

現(xiàn)在我們知道如何查詢數(shù)據(jù),讓我們討論如何以格式化的方式顯示數(shù)據(jù)。 Astro 提供了一個名為 render() 的便捷方法,用於將 Markdown 的全部內容渲染到內置的 Astro 組件 <content></content> 中。構建和顯示內容的方式還取決於您使用的是靜態(tài)站點生成還是服務器端渲染模式。

對於預渲染,您可以使用 getStaticPaths() 方法:

// src/content/config.js
import { z, defineCollection } from 'astro:content';

const devBlogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string().default('The Dev Team'),
    tags: z.array(z.string()),
    date: z.date(),
    draft: z.boolean().default(true),
    description: z.string(),
  }),
});

const corporateBlogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string(),
    date: z.date(),
    featured: z.boolean(),
    language: z.enum(['en', 'es']),
  }),
});

export const collections = {
  devblog: devBlogCollection,
  corporateblog: corporateBlogCollection,
};

代碼中使用了 getStaticPaths()。然後依靠 Astro.props 來捕獲條目,該條目將是一個包含有關條目的元數(shù)據(jù)、id、slugrender() 方法的對象。此方法負責將 Markdown 條目渲染到 Astro 模板中的 HTML,它通過創(chuàng)建 <content></content> 組件來實現(xiàn)。令人驚奇的是,現(xiàn)在您只需將 <content></content> 組件添加到模板中,即可看到渲染到 HTML 的 Markdown 內容。

以上是從Astro開始的內容收集開始的詳細內容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語言,各自適用於不同的應用場景。 Java用於大型企業(yè)和移動應用開發(fā),而JavaScript主要用於網(wǎng)頁開發(fā)。

JavaScript評論:簡短說明 JavaScript評論:簡短說明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

如何在JS中與日期和時間合作? 如何在JS中與日期和時間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時間處理需注意以下幾點:1.創(chuàng)建Date對像有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設置時間信息可用get和set方法,注意月份從0開始;3.手動格式化日期需拼接字符串,也可使用第三方庫;4.處理時區(qū)問題建議使用支持時區(qū)的庫,如Luxon。掌握這些要點能有效避免常見錯誤。

為什麼要將標籤放在的底部? 為什麼要將標籤放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

JavaScript:探索用於高效編碼的數(shù)據(jù)類型 JavaScript:探索用於高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

什麼是在DOM中冒泡和捕獲的事件? 什麼是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個階段,捕獲是從頂層向下到目標元素,冒泡是從目標元素向上傳播到頂層。 1.事件捕獲通過addEventListener的useCapture參數(shù)設為true實現(xiàn);2.事件冒泡是默認行為,useCapture設為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委託,提高動態(tài)內容處理效率;5.捕獲可用於提前攔截事件,如日誌記錄或錯誤處理。了解這兩個階段有助於精確控制JavaScript響應用戶操作的時機和方式。

Java和JavaScript有什麼區(qū)別? Java和JavaScript有什麼區(qū)別? Jun 17, 2025 am 09:17 AM

Java和JavaScript是不同的編程語言。 1.Java是靜態(tài)類型、編譯型語言,適用於企業(yè)應用和大型系統(tǒng)。 2.JavaScript是動態(tài)類型、解釋型語言,主要用於網(wǎng)頁交互和前端開發(fā)。

See all articles