Skip to content

Add Next/Prev Post Links #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig(
markdown: {
// TODO: Maybe use shiki and see if it is better and has line highlighting.
syntaxHighlight: false,
mode: "mdx",
remarkPlugins: [
"remark-gfm",
codeHighlightPre,
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const { threshold = 0, percentage = false, rootMargin = 0 } = Astro.props
scrollElement.addEventListener("scroll", e => {
cloneContainer.style.bottom = `${e.target.scrollTop}px`
})
console.log(`${rootMargin}px`)

const observer = new IntersectionObserver(
entries => observerCallback(entries[0], [mainElement, cloneElement]),
{ threshold, rootMargin: `${rootMargin}px`, root: scrollElement }
Expand Down
6 changes: 3 additions & 3 deletions src/components/BlogList.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useState } from "preact/hooks"
import TagBar from "./TagBar.jsx"
import SearchBar from "./SearchBar.jsx"
import BlogPostPreview from "./BlogPostPreview.jsx"
import { useSessionStorage } from "../hooks/useStorage.js"

export default function BlogList({ allPosts }) {
const [selectedTags, setSelectedTags] = useState([])
const [searchQuery, setSearchQuery] = useSessionStorage("searchTerm", "")
const [selectedTags, setSelectedTags] = useSessionStorage("selectedTags", [])
const tags = Object.entries(
allPosts.reduce((totals, post) => {
return post.tags.reduce((tagTotals, tag) => {
Expand All @@ -20,7 +21,6 @@ export default function BlogList({ allPosts }) {
.map(tag => {
return { ...tag, selected: selectedTags.includes(tag.name) }
})
const [searchQuery, setSearchQuery] = useState("")
const filteredPosts = allPosts.filter(post => {
return (
(post.title.toLowerCase().includes(searchQuery) ||
Expand Down
39 changes: 32 additions & 7 deletions src/components/BlogPost.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import dateFormatter from '../utils/dateFormatter.js'
import TagBar from './TagBar.jsx'
import ShareButtons from '/src/components/ShareButtons.jsx'
import ShareButtons from './ShareButtons.jsx'

export interface Props {
title: string;
Expand All @@ -10,7 +10,7 @@ export interface Props {
tags: string[]
}

const { title, date, tags, url } = Astro.props;
const { title, date, tags, url, nextPost, prevPost } = Astro.props;
const siteUrl = Astro.site.href
---

Expand All @@ -32,15 +32,23 @@ const siteUrl = Astro.site.href
<main>
<slot />
</main>
<!-- TODO: Add author and next/previous buttons. Also, maybe add a scroll to top button as well -->
<footer>
<div>
{prevPost && <div class="adjacent-post-link-title">Prev Article</div>}
{prevPost && <a href={prevPost.url}>{prevPost.title}</a>}
</div>
<div class="next">
{nextPost && <div class="adjacent-post-link-title">Next Article</div>}
{nextPost && <a href={nextPost.url}>{nextPost.title}</a>}
</div>
</footer>
<!-- TODO: Add related blog post links to the end of the articles. Have this data pulled from the frontmatter and also from the articles with the same tag/tags -->
<!-- TODO: Add author section -->
<!-- TODO: Add a scroll to top button as well -->
<!-- TODO: Add custom 404 page -->
<!-- TODO: Add search/tags to URL to make the back button work better -->
<!-- TODO: Add Google Analytics -->
<!-- TODO: Add social share images (https://joeprevite.com/create-a-twitter-card-for-your-blog) (Maybe dynamically generate a CSS page that is scraped with puppeteer or use something like ImageMagik or ffmpeg) -->
<!-- TODO: Add Pagination -->
<!-- TODO: Add in width slider for users -->
<!-- TODO: Add Themes/Dark mode -->
<!-- TODO: Add prefers-color-scheme support -->
</div>
</article>
</div>
Expand All @@ -62,6 +70,13 @@ const siteUrl = Astro.site.href
margin-bottom: 2rem;
}

footer {
margin-top: 2rem;
display: flex;
justify-content: space-between;
gap: 1rem;
}

.title,
.publish-date {
margin: 0;
Expand All @@ -76,6 +91,16 @@ const siteUrl = Astro.site.href
font-weight: 700;
}

.adjacent-post-link-title {
color: var(--theme-text-lighter);
font-weight: bold;
font-size: .8em;
}

.next {
text-align: end;
}

@media (max-width: 50em) {
.title {
font-size: 1.75rem;
Expand Down
35 changes: 35 additions & 0 deletions src/hooks/useStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useCallback, useState, useEffect } from "preact/hooks"

const hasWindow = typeof window !== "undefined"

export function useLocalStorage(key, defaultValue) {
return useStorage(key, defaultValue, hasWindow ? window.localStorage : null)
}

export function useSessionStorage(key, defaultValue) {
return useStorage(key, defaultValue, hasWindow ? window.sessionStorage : null)
}

function useStorage(key, defaultValue, storageObject) {
const [value, setValue] = useState(() => {
const jsonValue = storageObject?.getItem(key)
if (jsonValue != null) return JSON.parse(jsonValue)

if (typeof defaultValue === "function") {
return defaultValue()
} else {
return defaultValue
}
})

useEffect(() => {
if (value === undefined) return storageObject.removeItem(key)
storageObject.setItem(key, JSON.stringify(value))
}, [key, value, storageObject])

const remove = useCallback(() => {
setValue(undefined)
}, [])

return [value, setValue, remove]
}
33 changes: 0 additions & 33 deletions src/layouts/BlogPost.astro

This file was deleted.

24 changes: 0 additions & 24 deletions src/pages/[blog].astro

This file was deleted.

Loading