Layout
框架 API 目前默认支持以下布局,布局支持与最终表现通常与主题有关。
post
:文章布局tags
:标签布局archives
:归档布局categories
:分类布局collections
:合集布局
使用布局
合集布局
新建总览页 pages/collections/index.md
,并指定布局为 collections
。
collections
: 合集列表,须指定唯一合集 ID。
md
---
layout: collections
icon: i-ri-gallery-view
collections:
- hamster
- love-and-peace
---
新建合集文件夹 pages/collections/hamster/
:
index.md
:合集总览页,指定布局为collection
。index.ts
:合集配置文件。1.md
:合集中的第一篇文章。
新建合集入口页 pages/collections/hamster/index.md
,并指定布局为 collection
。
md
---
layout: collection
---
定义当前合集信息:
ts
import { defineCollection } from 'valaxy'
export default defineCollection({
key: 'hamster',
title: '仓鼠',
cover: 'https://cover.sli.dev',
description: 'The story of I and She',
items: [
{
title: '第一章 仓鼠的笼子',
// 文章唯一索引,对应路径为 `pages/collections/hamster/1.md`
key: '1',
},
{
title: '第二章 白昼之光,岂知夜色之深。',
key: '2',
},
{
title: '第三章 作茧自缚',
key: '3',
},
]
})
新建合集中文章:
layout: collection
可省略,pages/collections/
目录下的所有文章默认使用collection
布局。
md
---
title: 第一章 仓鼠的笼子
layout: collection
---
效果预览:合集|Valaxy Theme Yun
实现布局
如 valaxy-theme-yun 自 v0.25.9
支持了 collections
布局。
按约定,主题需要在 layouts
目录下创建对应的布局文件,文件名与布局名称相同。
在主题中,你可以使用以下合集相关 API。
useCollections
API 获取合集列表。useCollection
API 获取单个合集(根据路径判断当前合集 ID)。
vue
<script setup lang="ts">
import { useCollections, useFrontmatter, useValaxyI18n } from 'valaxy'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const { $tO } = useValaxyI18n()
const fm = useFrontmatter()
const { collections } = useCollections()
</script>
<template>
<YunLayoutWrapper>
<YunLayoutLeft />
<RouterView v-slot="{ Component }">
<component :is="Component">
<template #main-header>
<YunPageHeader
:title="$tO(fm.title) || t('menu.collections')"
:icon="fm.icon"
:page-title-class="fm.pageTitleClass"
/>
</template>
<template #main-content>
<div flex="~ wrap" gap="6">
<YunCollectionItem
v-for="collection in collections"
:key="collection.key"
:collection="collection"
/>
</div>
</template>
</component>
</RouterView>
</YunLayoutWrapper>
<YunFooter />
</template>