-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: next generate page header (#225)
- Loading branch information
1 parent
d185781
commit d7a7fd7
Showing
9 changed files
with
929 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable */ | ||
/* prettier-ignore */ | ||
// @ts-nocheck | ||
// Generated by unplugin-vue-components | ||
// Read more: https://github.com/vuejs/core/pull/3399 | ||
export {} | ||
|
||
declare module 'vue' { | ||
export interface GlobalComponents { | ||
HomeContent: typeof import('./theme/components/HomeContent.vue')['default'] | ||
PageInfo: typeof import('./theme/components/PageInfo.vue')['default'] | ||
RouterLink: typeof import('vue-router')['RouterLink'] | ||
RouterView: typeof import('vue-router')['RouterView'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Plugin } from 'vite' | ||
import { resolve, relative } from 'path' | ||
|
||
const ROOT = resolve(__dirname, '../../') | ||
|
||
export function MarkdownTransform(): Plugin { | ||
return { | ||
name: 'docs-md-transform', | ||
enforce: 'pre', | ||
async transform(code, id) { | ||
if (!id.endsWith('.md')) | ||
return null | ||
|
||
id = relative(ROOT, id) | ||
|
||
if (id == 'index.md') | ||
return null | ||
|
||
code = pageHeaderTemplate(code) | ||
|
||
return code | ||
}, | ||
} | ||
} | ||
|
||
|
||
const pageHeaderTemplate = (code: string) => code.replace(/(---\n\n)/, `$1 | ||
# {{ $frontmatter.title }} | ||
<PageInfo /> | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<script setup lang="ts"> | ||
import { useData } from 'vitepress' | ||
import { computed, onMounted, ref, watchEffect } from 'vue'; | ||
const { frontmatter, page, theme, lang } = useData() | ||
const date = computed( | ||
() => new Date(frontmatter.value.lastUpdated ?? page.value.lastUpdated) | ||
) | ||
const isoDatetime = computed(() => date.value.toISOString()) | ||
const datetime = ref('') | ||
// Avoid hydration errors | ||
onMounted(() => { | ||
watchEffect(() => { | ||
datetime.value = new Intl.DateTimeFormat( | ||
theme.value.lastUpdated?.formatOptions?.forceLocale ? lang.value : undefined, | ||
theme.value.lastUpdated?.formatOptions ?? { | ||
dateStyle: 'short', | ||
timeStyle: 'short' | ||
} | ||
).format(date.value) | ||
}) | ||
}) | ||
const authors = computed(() => { | ||
let author = (frontmatter.value?.author ?? []) as string[] | ||
if (!Array.isArray(author)) | ||
author = [author] | ||
if (!author.length) | ||
author = ['匿名'] | ||
return author | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-wrap gap-4 mt-4 mb-10"> | ||
<div class="inline-flex items-center gap-1"> | ||
<span class="i-octicon:person" /> | ||
<span>作者:</span> | ||
<span class="space-x-2"> | ||
<span v-for="author of authors"> | ||
{{ author }} | ||
</span> | ||
</span> | ||
</div> | ||
|
||
<div class="inline-flex items-center gap-1"> | ||
<span class="i-octicon:calendar-16" /> | ||
<span>{{ theme.lastUpdated?.text || 'Last updated' }}:</span> | ||
<time :datetime="isoDatetime">{{ datetime }}</time> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { resolve } from 'node:path' | ||
import { defineConfig } from 'vite' | ||
import { MarkdownTransform } from './.vitepress/plugins/markdownTransform' | ||
import Components from 'unplugin-vue-components/vite' | ||
import UnoCSS from 'unocss/vite' | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
MarkdownTransform(), | ||
Components({ | ||
dirs: resolve(__dirname, '.vitepress/theme/components'), | ||
include: [/\.vue$/, /\.vue\?vue/, /\.md$/], | ||
dts: './.vitepress/components.d.ts', | ||
transformer: 'vue3', | ||
}), | ||
|
||
UnoCSS(), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.