-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new usePrevNextPost composable (#160)
- Loading branch information
1 parent
3a5429d
commit 7696060
Showing
7 changed files
with
118 additions
and
8 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
#### Staging | ||
|
||
```ts | ||
```ts twoslash | ||
const staging = await isStaging() | ||
``` | ||
More about this on the [staging](../3.advanced/4.staging.md) page. |
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,38 @@ | ||
|
||
#### get Previous/Next posts | ||
|
||
```ts twoslash | ||
const { prev: prev, next: next } = await usePrevNextPost('currentPostSlug') | ||
``` | ||
|
||
|
||
```vue twoslash [..uri.vue] | ||
<script setup lang="ts"> | ||
const route = useRoute() | ||
const { prev: prev, next: next } = await usePrevNextPost(route.params.slug[0]) | ||
</script> | ||
<template> | ||
<div class="flex justify-start"> | ||
<UButton | ||
v-if="prev" | ||
:alt="prevButton" | ||
:to="'/' + prev" | ||
icon="i-heroicons-arrow-left-16-solid" | ||
variant="soft" | ||
size="sm" | ||
/> | ||
</div> | ||
<div class="flex justify-end"> | ||
<UButton | ||
v-if="next" | ||
:alt="nextButton" | ||
:to="'/' + next" | ||
trailing-icon="i-heroicons-arrow-right-16-solid" | ||
variant="soft" | ||
size="sm" | ||
/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<script setup lang="ts"> | ||
defineProps<{ | ||
prev?: string | ||
next?: string | ||
prevButton?: string | ||
nextButton?: string | ||
}>() | ||
</script> | ||
|
||
<template> | ||
<div class="grid grid-cols-3 gap-6 max-w-28"> | ||
<div class="flex justify-start"> | ||
<UButton | ||
v-if="prev" | ||
:alt="prevButton" | ||
:to="'/' + prev" | ||
icon="i-heroicons-arrow-left-16-solid" | ||
variant="soft" | ||
size="sm" | ||
/> | ||
</div> | ||
<div class="flex justify-center"> | ||
<UButton | ||
to="/" | ||
icon="i-heroicons-home" | ||
variant="soft" | ||
size="sm" | ||
/> | ||
</div> | ||
<div class="flex justify-end"> | ||
<UButton | ||
v-if="next" | ||
:alt="nextButton" | ||
:to="'/' + next" | ||
trailing-icon="i-heroicons-arrow-right-16-solid" | ||
variant="soft" | ||
size="sm" | ||
/> | ||
</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
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,29 @@ | ||
import { useWPContent } from './useWPContent' | ||
|
||
const _usePrevNextPost = async (currentPostSlug: string) => { | ||
const allPosts = await getAllPosts() | ||
if (!allPosts) return { prev: null, next: null } | ||
const currentIndex: number = allPosts.slugs.findIndex((slug: string) => slug === currentPostSlug) | ||
const nextPost = currentIndex > 0 ? allPosts.slugs[currentIndex - 1] : null | ||
const prevPost = allPosts.slugs.length > (currentIndex + 1) ? allPosts.slugs[currentIndex + 1] : null | ||
|
||
return { | ||
prev: prevPost ? prevPost : null, | ||
next: nextPost ? nextPost : null | ||
} | ||
} | ||
|
||
const getAllPosts = async () => { | ||
const { data: allPosts } = await useWPContent('Posts', ['posts', 'nodes'], false) | ||
if (allPosts.value) { | ||
return { | ||
slugs: allPosts.value?.map((post) => { | ||
if (post) return post.slug | ||
else return null | ||
}) | ||
} | ||
} | ||
return | ||
} | ||
|
||
export const usePrevNextPost = _usePrevNextPost |