From 5259a3397a0bdd34b853d90e1837fca68216dfad Mon Sep 17 00:00:00 2001 From: zce Date: Sun, 4 Aug 2024 21:35:12 +0800 Subject: [PATCH] feat: new snippets for last modified --- docs/.vitepress/config.ts | 3 +- docs/guide/last-modified.md | 58 +++++++++++++++++++++++++++++++++++++ docs/other/snippets.md | 51 +++++++++++++++++++++++++++++++- 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 docs/guide/last-modified.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index daeaaeb..f7614a9 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -68,7 +68,8 @@ export default defineConfig({ { text: 'Code Highlighting', link: 'code-highlighting' }, { text: 'Integration with Next.js', link: 'with-nextjs' }, { text: 'Custom Loader', link: 'custom-loader' }, - { text: 'Custom Schema', link: 'custom-schema' } + { text: 'Custom Schema', link: 'custom-schema' }, + { text: 'Last Modified', link: 'last-modified' } // { text: 'Fast Refresh', link: 'fast-refresh' } ] }, diff --git a/docs/guide/last-modified.md b/docs/guide/last-modified.md new file mode 100644 index 0000000..779503b --- /dev/null +++ b/docs/guide/last-modified.md @@ -0,0 +1,58 @@ +# Last Modified Schema + +We provide a last modified timestamp schema based on file stat and git timestamp. + +## Based on file stat + +create a timestamp schema based on file stat. + +```ts +const timestamp = () => + s.custom(i => i === undefined || typeof i === 'string').transform(async (value, { meta, addIssue }) => { + if (value != null) { + addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the file modified timestamp' }) + } + + const stats = await stat(meta.path) + return stats.mtime.toISOString() + }) +``` + +use it in your schema + +```ts +const posts = defineCollection({ + // ... + schema: { + // ... + lastModified: timestamp() + } +}) +``` + +## Based on git timestamp + +```ts +const execAsync = promisify(exec) + +const timestamp = () => + s.custom(i => i === undefined || typeof i === 'string').transform(async (value, { meta, addIssue }) => { + if (value != null) { + addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the value from `git log -1 --format=%cd`' }) + } + const { stdout } = await execAsync(`git log -1 --format=%cd ${meta.path}`) + return new Date(stdout).toISOString() + }) +``` + +use it in your schema + +```ts +const posts = defineCollection({ + // ... + schema: { + // ... + lastModified: timestamp() + } +}) +``` diff --git a/docs/other/snippets.md b/docs/other/snippets.md index 4195d32..e1e535c 100644 --- a/docs/other/snippets.md +++ b/docs/other/snippets.md @@ -1,6 +1,55 @@ # Snippets -#### Remote Image with BlurDataURL Schema +## Last Modified Schema + +### Based on file stat + +```ts +const timestamp = () => + s.custom(i => i === undefined || typeof i === 'string').transform(async (value, { meta, addIssue }) => { + if (value != null) { + addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the file modified timestamp' }) + } + + const stats = await stat(meta.path) + return stats.mtime.toISOString() + }) + +// use it in your schema +const posts = defineCollection({ + // ... + schema: { + // ... + lastModified: timestamp() + } +}) +``` + +### Based on git timestamp + +```ts +const execAsync = promisify(exec) + +const timestamp = () => + s.custom(i => i === undefined || typeof i === 'string').transform(async (value, { meta, addIssue }) => { + if (value != null) { + addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the value from `git log -1 --format=%cd`' }) + } + const { stdout } = await execAsync(`git log -1 --format=%cd ${meta.path}`) + return new Date(stdout).toISOString() + }) + +// use it in your schema +const posts = defineCollection({ + // ... + schema: { + // ... + lastModified: timestamp() + } +}) +``` + +## Remote Image with BlurDataURL Schema ```ts import { getImageMetadata, s } from 'velite'