Compare commits

...

5 commits

Author SHA1 Message Date
grassblock
e59c84bb2f feat: text version for posts 2025-05-04 11:44:26 +08:00
grassblock
6474fbcac1 refactor: load search index only when the search bar is focused 2025-05-04 11:43:54 +08:00
grassblock
f42d71a948 refactor: simplify global css 2025-05-04 11:43:06 +08:00
grassblock
9411ec22c1 feat: custom pages support 2025-05-04 11:42:46 +08:00
grassblock
7e5f8ce06f feat: move articles link from 'blog/' to 'post/' paths 2025-05-04 11:42:09 +08:00
15 changed files with 113 additions and 33 deletions

View file

@ -19,16 +19,26 @@
let fuse;
let posts = [];
let searchInitialized = false;
async function initializeSearch() {
const response = await fetch('/search-index.json');
posts = await response.json();
fuse = new Fuse(posts, {
keys: ['title', 'description', 'content'],
threshold: 0.3,
includeMatches: true
});
// Prevent multiple initializations
if (searchInitialized) return;
try {
// Fetch the search index
const response = await fetch('/search-index.json');
posts = await response.json();
fuse = new Fuse(posts, {
keys: ['title', 'description', 'content'],
threshold: 0.3,
includeMatches: true
});
searchInitialized = true;
document.getElementById('search-results').innerHTML = '';
} catch (error) {
console.error('Error fetching search index:', error);
}
}
function performSearch(query) {
@ -37,6 +47,8 @@
return;
}
if (!searchInitialized) return;
const results = fuse.search(query);
const resultsElement = document.getElementById('search-results');
@ -58,14 +70,27 @@
resultsElement.innerHTML = html;
}
// Initialize search when the component mounts
initializeSearch();
// Add event listener for search input
const searchInput = document.getElementById('search-input');
// Initialize search only when the input is focused or clicked
searchInput.addEventListener('focus', initializeSearch);
searchInput.addEventListener('click', initializeSearch);
searchInput.addEventListener('input', (e) => {
performSearch(e.target.value);
if (!searchInitialized) {
initializeSearch().then(() => {
performSearch(e.target.value);
});
} else {
performSearch(e.target.value);
}
});
searchInput.addEventListener('focus', () => {
if (!searchInitialized) {
document.getElementById('search-results').innerHTML = '<p>Loading search index...</p>';
}
});
</script>

View file

@ -1,11 +1,17 @@
import { defineCollection } from 'astro:content';
import { blogs } from './blog/_schemas';
import { posts } from './posts/_schemas';
import { pages } from "./pages/_schemas";
const blogCollection = defineCollection({
type: 'content',
schema: blogs,
schema: posts,
});
const pageCollection = defineCollection({
type: 'content',
schema: pages,
});
export const collections = {
'blog': blogCollection,
'posts': blogCollection,
'pages': pageCollection,
};

View file

@ -0,0 +1,7 @@
import { z } from 'astro:content';
export const pages = z.object({
title: z.string(),
description: z.string(),
heroImage: z.string().optional()
});

View file

@ -0,0 +1,5 @@
---
title: 'Test Page'
description: 'This is a test page'
---
testestestest

View file

@ -1,6 +1,6 @@
import { z } from 'astro:content';
export const blogs = z.object({
export const posts = z.object({
title: z.string(),
description: z.string(),
pubDate: z.coerce.date(),

21
src/pages/[...slug].astro Normal file
View file

@ -0,0 +1,21 @@
---
import Layout from '../layouts/Layout.astro';
import { getCollection } from "astro:content";
export async function getStaticPaths() {
const pageEntries = await getCollection('pages');
return pageEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry },
}));
}
const { entry } = Astro.props;
const { Content } = await entry.render();
---
<Layout title={`${entry.data.title} | Terminal Blog`} path={`/var/log/${entry.slug}`}>
<h1 class="post-title">{entry.data.title}</h1>
<div class="post-content">
<Content/>
</div>
</Layout>

View file

@ -2,7 +2,7 @@
import Layout from '../layouts/Layout.astro';
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
const posts = await getCollection('posts');
posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime());
---
@ -18,7 +18,7 @@ posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDat
{posts.map((post) => (
<p>
<span style="color: var(--terminal-yellow);">{new Date(post.data.pubDate).toISOString().split('T')[0]}</span>
<a href={`/blog/${post.slug}`}>{post.data.title}</a>
<a href={`/post/${post.slug}`}>{post.data.title}</a>
</p>
))}

View file

@ -3,7 +3,7 @@ import Layout from '../../layouts/Layout.astro';
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const blogEntries = await getCollection('blog');
const blogEntries = await getCollection('posts');
return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry },
}));

View file

@ -0,0 +1,25 @@
import { getCollection } from 'astro:content';
export const prerender = true;
export async function getStaticPaths() {
const blogEntries = await getCollection('posts');
return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry },
}));
}
export async function GET({ props }) {
const { entry } = props;
// Format the content as plain text
const title = entry.data.title;
const date = entry.data.pubDate.toISOString().split('T')[0];
const content = entry.body;
// Combine the post info and body into a single text file
const textContent = `Title: ${title}\nPublished at: ${date}\n\n${content}`;
return new Response(textContent, {
headers: {
'Content-Type': 'text/plain',
},
});
}

View file

@ -21,7 +21,7 @@ export async function GET(context) {
const container = await AstroContainer.create({ renderers });
// Load the content collection entries to add to our RSS feed.
const posts = (await getCollection("blog")).sort((a, b) =>
const posts = (await getCollection("posts")).sort((a, b) =>
// Sort by publication date descending.
a.data.pubDate > b.data.pubDate ? -1 : 1
);

View file

@ -1,7 +1,7 @@
import { getCollection } from 'astro:content';
export async function GET() {
const posts = await getCollection('blog');
const posts = await getCollection('posts');
const searchIndex = posts.map(post => ({
title: post.data.title,
description: post.data.description,

View file

@ -13,7 +13,7 @@
}
/* Light theme */
@media (prefers-color-scheme: light) {
:root {
:root:not([data-theme="dark"]) {
--bg-color: #f3f4f6;
--text-color: #374151;
--accent-color: #3b82f6;
@ -23,16 +23,6 @@
--terminal-yellow: #d97706;
--terminal-red: #dc2626;
}
:root:not([data-theme="light"]) {
--bg-color: #1f2937;
--text-color: #a5b4cf;
--accent-color: #64a0ff;
--border-color: #3b4351;
--header-color: #83a2ce;
--terminal-green: #4ade80;
--terminal-yellow: #fbbf24;
--terminal-red: #ef4444;
}
}
/* Light theme override (for switch) */
:root[data-theme="light"] {
@ -46,6 +36,7 @@
--terminal-red: #dc2626;
}
*, *::before, *::after {
box-sizing: border-box;
margin: 0;