feat: support frontmatter hide

This commit is contained in:
YunYouJun 2023-01-28 22:23:10 +08:00
parent 0d21c20892
commit 632c09c38a
10 changed files with 102 additions and 16 deletions

View File

@ -0,0 +1,5 @@
---
title: Hidden Only in Index
hide: index
date: 2023-01-28
---

View File

@ -0,0 +1,5 @@
---
title: Hidden Post
hide: true
date: 2023-01-28
---

View File

@ -5,6 +5,26 @@ categories:
- Guide
---
## FrontMatter
**文章**`post`)继承自**页面**`page`),因此**页面**中的 Frontmatter 通用被**文章**支持。
> 单篇文章支持的配置项。
譬如:
```md
---
title: Title
hide: true
---
```
- `title`: 文章标题
- `hide`: 你可以在文章头部添加 hide 属性,来临时隐藏某篇文章。(该文章仍然会被渲染)
- `true` / `all`: 当设置为 `true``all` 时,该文章仍然会被渲染,你可以直接访问链接进行查看。但不会被显示在展示的文章卡片与归档中。
- `index`: 设置为 `index` 时,将只在首页隐藏,归档中仍然展示。(譬如放一些没有必要放在首页的笔记,并在归档中方便自己查看。)
## 摘要
你可以通过插入 `<!-- more -->` 的方式生成摘要excerpt

View File

@ -30,3 +30,35 @@ Hexo 博客目录与 Valaxy 博客目录对应关系如下,将相关内容复
## 示例
更复杂的迁移示例,您还可以对比 [yunyoujun.github.io | GitHub](https://github.com/YunYouJun/yunyoujun.github.io) 仓库 [hexo](https://github.com/YunYouJun/yunyoujun.github.io/tree/hexo) 分支与 [valaxy](https://github.com/YunYouJun/yunyoujun.github.io/tree/valaxy) 的异同。
## 常见问题
### 摘要截断符
默认为 `<!-- more -->``more` 前后需有空格。
### Markdown 换行
Valaxy 的 Markdown 解析基于 [`markdown-it`](https://github.com/markdown-it/markdown-it) 实现。
`markdown-it` 的策略在 Markdown 中换行后渲染的内容并没有换行:
```md
第一行
没有换行
```
第一行
没有换行
---
如果需要正常换行,需在末尾添加两个空格:
```md
第一行(末尾有两个空格)
换行了
```
第一行(末尾有两个空格)
换行了

View File

@ -11,20 +11,22 @@ const props = defineProps<{
const { t } = useI18n()
const years = ref<number[]>([])
const postList = ref<Record<string, Post[]>>({})
const postListByYear = ref<Record<string, Post[]>>({})
watch(() => props.posts, () => {
postList.value = {}
postListByYear.value = {}
years.value = []
props.posts.forEach((post) => {
if (post.hide && post.hide !== 'index')
return
if (post.date) {
const year = parseInt(formatDate(post.date, 'YYYY'))
if (postList.value[year]) {
postList.value[year].push(post)
if (postListByYear.value[year]) {
postListByYear.value[year].push(post)
}
else {
years.value.push(year)
postList.value[year] = [post]
postListByYear.value[year] = [post]
}
}
})
@ -58,7 +60,7 @@ const sortedYears = computed(() => {
</h2>
</div>
<article v-for="post, j in sortByDate(postList[year], isDesc)" :key="j" class="post-item">
<article v-for="post, j in sortByDate(postListByYear[year], isDesc)" :key="j" class="post-item">
<header class="post-header">
<div class="post-meta">
<time v-if="post.date" class="post-time" font="mono" opacity="80">{{ formatDate(post.date, 'MM-DD') }}</time>

View File

@ -4,34 +4,39 @@ import { formatDate } from 'valaxy'
import { useI18n } from 'vue-i18n'
defineProps<{
frontmatter: Post
// FrontMatter
fm: Post
}>()
const { t } = useI18n()
</script>
<template>
<div v-if="frontmatter.draft" class="post-draft-icon" title="draft">
<div v-if="fm.draft" class="post-draft-icon" title="draft">
<div i-ri-draft-line />
</div>
<div v-if="frontmatter.top" class="post-top-icon">
<div v-if="fm.hide" class="post-top-icon" color="$va-c-danger" :title="`hide:${fm.hide}`">
<div v-if="fm.hide === 'index'" i-ri-eye-close-line />
<div i-ri-eye-off-line />
</div>
<div v-if="fm.top" class="post-top-icon" color="$va-c-warning">
<div i-ri-pushpin-line />
</div>
<div v-if="frontmatter" class="post-meta justify-center" flex="~" text="sm" py="1">
<div v-if="frontmatter.date" class="post-time flex items-center">
<div v-if="fm" class="post-meta justify-center" flex="~" text="sm" py="1">
<div v-if="fm.date" class="post-time flex items-center">
<span class="inline-flex-center" :title="t('post.posted')">
<div class="inline-block" i-ri-calendar-line />
<time m="l-1">{{ formatDate(frontmatter.date) }}</time>
<time m="l-1">{{ formatDate(fm.date) }}</time>
</span>
<span
v-if="frontmatter.updated && frontmatter.updated !== frontmatter.date"
v-if="fm.updated && fm.updated !== fm.date"
class="inline-flex-center" :title="t('post.edited')"
>
<span m="x-2">-</span>
<div i-ri-calendar-2-line />
<time m="l-1">{{ formatDate(frontmatter.updated) }}</time>
<time m="l-1">{{ formatDate(fm.updated) }}</time>
</span>
</div>
</div>
@ -52,7 +57,6 @@ const { t } = useI18n()
position: absolute;
top: 1rem;
right: 1rem;
color: var(--va-c-warning);
font-size: 1.2rem;
}
</style>

View File

@ -10,7 +10,11 @@ import 'uno.css'
import setupMain from './setup/main'
const routes = setupLayouts(__DEV__ ? generatedRoutes : generatedRoutes.filter(i => !i.meta?.frontmatter.draft))
const routes = setupLayouts(__DEV__
? generatedRoutes
: generatedRoutes.filter(i =>
!i.meta?.frontmatter.draft && !i.meta?.frontmatter.hide,
))
// https://github.com/antfu/vite-ssg
export const createApp = ViteSSG(

View File

@ -33,6 +33,9 @@ export async function generateFuseList(options: ResolvedValaxyOptions) {
continue
}
if (data.hide)
continue
posts.push({
title: data.title || '',
tags: data.tags || [],

View File

@ -72,6 +72,10 @@ export async function build(options: ResolvedValaxyOptions) {
continue
}
// hidden
if (data.hide)
continue
await formatMdDate(data, i, siteConfig.date?.format, siteConfig.lastUpdated)
// todo i18n

View File

@ -120,6 +120,13 @@ export interface Post extends Record<string, any> {
* @description 稿
*/
draft?: boolean
/**
* hide in index
* - true/`all`: hide in index & archive
* - `index`: hide in index
* @description
*/
hide?: 'index' | boolean
/**
* cover
* @description