Skip to content

Commit

Permalink
feat: new snippets for last modified
Browse files Browse the repository at this point in the history
  • Loading branch information
zce committed Aug 4, 2024
1 parent 1407cd5 commit 5259a33
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
]
},
Expand Down
58 changes: 58 additions & 0 deletions docs/guide/last-modified.md
Original file line number Diff line number Diff line change
@@ -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<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(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<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(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()
}
})
```
51 changes: 50 additions & 1 deletion docs/other/snippets.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
# Snippets

#### Remote Image with BlurDataURL Schema
## Last Modified Schema

### Based on file stat

```ts
const timestamp = () =>
s.custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(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<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(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'
Expand Down

0 comments on commit 5259a33

Please sign in to comment.