添加Preact集成和RSS插件

This commit is contained in:
mazj
2025-01-09 13:37:34 +08:00
parent 1e2731ba14
commit 5c6d17981b
26 changed files with 4342 additions and 19 deletions

View File

@ -1,5 +1,10 @@
// @ts-check
import { defineConfig } from 'astro/config';
import preact from "@astrojs/preact";
// https://astro.build/config
export default defineConfig({});
export default defineConfig({
site: "https://example.com",
integrations: [preact()]
});

View File

@ -9,6 +9,12 @@
"astro": "astro"
},
"dependencies": {
"astro": "^5.1.3"
"@astrojs/preact": "^4.0.1",
"@astrojs/rss": "^4.0.11",
"astro": "^5.1.3",
"preact": "^10.25.4"
},
"devDependencies": {
"@astrojs/ts-plugin": "^1.10.4"
}
}

3772
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,4 @@
---
const { url, title } = Astro.props;
---
<li><a href={url}>{title}</a></li>

View File

@ -0,0 +1,17 @@
---
import Social from "./Social.astro";
---
<footer>
<Social platform="twitter" username="astrodotbuild" />
<Social platform="github" username="withastro" />
<Social platform="youtube" username="astrodotbuild" />
</footer>
<style>
footer {
display: flex;
gap: 1rem;
margin-top: 2rem;
}
</style>

View File

@ -0,0 +1,21 @@
import { useState } from 'preact/hooks';
export interface GreetingProps {
messages: string[];
}
export default function Greeting({ messages }: GreetingProps) {
const randomMessage = () => messages[(Math.floor(Math.random() * messages.length))];
const [greeting, setGreeting] = useState(messages[0]);
return (
<div>
<h3>{greeting}! Thank you for visiting!</h3>
<button onClick={() => setGreeting(randomMessage())}>
New Greeting
</button>
</div>
);
}

View File

@ -0,0 +1,9 @@
---
---
<div class="hamburger">
<span class="line"></span>
<span class="line"></span>
<span class="line"></span>
</div>

View File

@ -0,0 +1,13 @@
---
import Hamburger from "./Hamburger.astro";
import Navigation from "./Navigation.astro";
import ThemeIcon from "./ThemeIcon.astro";
---
<header>
<nav>
<Hamburger />
<ThemeIcon />
<Navigation />
</nav>
</header>

View File

@ -0,0 +1,10 @@
---
---
<div class="nav-links">
<a href="/">首页</a>
<a href="/about/">关于</a>
<a href="/blog/">博客</a>
<a href="/tags/">标签</a>
</div>

View File

@ -0,0 +1,14 @@
---
const { platform, username } = Astro.props;
---
<a href={`https://www.${platform}.com/${username}`}>{platform}</a>
<style>
a {
padding: 0.5rem 1rem;
color: white;
background-color: #4c1d95;
text-decoration: none;
}
</style>

View File

@ -0,0 +1,73 @@
---
---
<button id="themeToggle">
<svg width="30px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
class="sun"
fill-rule="evenodd"
d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"
></path>
<path
class="moon"
fill-rule="evenodd"
d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"
></path>
</svg>
</button>
<style>
#themeToggle {
border: 0;
background: none;
}
.sun {
fill: black;
}
.moon {
fill: transparent;
}
:global(.dark) .sun {
fill: transparent;
}
:global(.dark) .moon {
fill: white;
}
</style>
<script is:inline>
const theme = (() => {
if (
typeof localStorage !== "undefined" &&
localStorage.getItem("theme")
) {
return localStorage.getItem("theme") ?? "light";
}
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
})();
if (theme === "light") {
document.documentElement.classList.remove("dark");
} else {
document.documentElement.classList.add("dark");
}
window.localStorage.setItem("theme", theme);
const handleToggleClick = () => {
const element = document.documentElement;
element.classList.toggle("dark");
const isDark = element.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
};
document
.getElementById("themeToggle")
?.addEventListener("click", handleToggleClick);
</script>

View File

@ -0,0 +1,24 @@
---
import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
import '../styles/global.css';
const { pageTitle } = Astro.props;
---
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>{pageTitle}</title>
</head>
<body>
<Header />
<h1>{pageTitle}</h1>
<slot />
<Footer />
<script>
import "../scripts/menu.js";
</script>
</body>
</html>

View File

@ -0,0 +1,42 @@
---
import BaseLayout from "./BaseLayout.astro";
const { frontmatter } = Astro.props;
---
<BaseLayout pageTitle={frontmatter.title}>
<p>作者: {frontmatter.author}</p>
<p>日期: {frontmatter.pubDate.toString().slice(0, 10)}</p>
<p><em>{frontmatter.description}</em></p>
<img src={frontmatter.image.url} width="300" alt={frontmatter.image.alt} />
<div class="tags">
{
frontmatter.tags.map((tag: string) => (
<p class="tag">
<a href={`/tags/${tag}`}>{tag}</a>
</p>
))
}
</div>
<slot />
</BaseLayout>
<style>
a {
color: #00539f;
}
.tags {
display: flex;
flex-wrap: wrap;
}
.tag {
margin: 0.25em;
border: dotted 1px #a1a1a1;
border-radius: 0.5em;
padding: 0.5em 1em;
font-size: 1.15em;
background-color: #f8fcfd;
}
</style>

59
src/pages/about.astro Normal file
View File

@ -0,0 +1,59 @@
---
import BaseLayout from "../layouts/BaseLayout.astro";
import "../styles/global.css";
const identity = {
firstName: "莎拉",
country: "加拿大",
occupation: "技术撰稿人",
hobbies: ["摄影", "观鸟", "棒球"],
};
const skills = ["HTML", "CSS", "JavaScript", "React", "Astro", "Writing Docs"];
const happy = true;
const finished = true;
const goal = 3;
const skillColor = "navy";
const fontWeight = "bold";
const textCase = "uppercase";
---
<BaseLayout pageTitle="关于我">
<p>我的信息:</p>
<ul>
<li>
我叫{identity.firstName},是{identity.country}{
identity.occupation
}。
</li>
<li>
我的兴趣有:{
identity.hobbies.map((hobby) => <span>{hobby}, </span>)
}
</li>
</ul>
<p>我的技能:</p>
<ul>
{skills.map((skill) => <li class="skill">{skill}</li>)}
</ul>
{happy && <p>我非常乐意学习 Astro</p>}
{finished && <p>我完成了这节教程!</p>}
{goal === 3 ? <p>我的目标是在三天内完成。</p> : <p>我的目标不是 3 天。</p>}
<style is:global define:vars={{ skillColor, fontWeight, textCase }}>
h1 {
color: purple;
font-size: 4rem;
}
.skill {
color: var(--skillColor);
font-weight: var(--fontWeight);
text-transform: var(--textCase);
}
</style>
</BaseLayout>

14
src/pages/blog.astro Normal file
View File

@ -0,0 +1,14 @@
---
import BlogPost from "../components/BlogPost.astro";
import BaseLayout from "../layouts/BaseLayout.astro";
import "../styles/global.css";
const allPosts = Object.values(import.meta.glob('./posts/*.md', { eager: true }));
---
<BaseLayout pageTitle="博客">
<p>这是我的 Astro 学习笔记</p>
<ul>
{allPosts.map((post: any) => <BlogPost url={post.url} title={post.frontmatter.title} />)}
</ul>
</BaseLayout>

View File

@ -1,16 +1,9 @@
---
import BaseLayout from '../layouts/BaseLayout.astro';
import Greeting from '../components/Greeting';
const pageTitle = "首页";
---
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>
<h1>Astro</h1>
</body>
</html>
<BaseLayout pageTitle = {pageTitle}>
<h2>My awesome blog subtitle</h2>
<Greeting client:load messages={["Hej", "Hallo", "Hola", "Habari"]} />
</BaseLayout>

25
src/pages/posts/post-1.md Normal file
View File

@ -0,0 +1,25 @@
---
layout: ../../layouts/MarkdownPostLayout.astro
title: '我的第一篇博客文章'
pubDate: 2022-07-01
description: '这是我 Astro 博客的第一篇文章。'
author: 'Astro 学习者'
image:
url: 'https://docs.astro.build/assets/rose.webp'
alt: 'The Astro logo on a dark background with a pink glow.'
tags: ["astro", "blogging", "learning in public"]
---
欢迎来到我学习关于 Astro 的新博客!在这里,我将分享我建立新网站的学习历程。
## 我做了什么
1. **安装 Astro**:首先,我创建了一个新的 Astro 项目并设置好了我的在线账号。
2. **制作页面**:然后我学习了如何通过创建新的 `.astro` 文件并将它们保存在 `src/pages/` 文件夹里来制作页面。
3. **发表博客文章**:这是我的第一篇博客文章!我现在有用 Astro 编写的页面和用 Markdown 写的文章了!
## 下一步计划
我将完成 Astro 教程,然后继续编写更多内容。关注我以获取更多信息。

12
src/pages/posts/post-2.md Normal file
View File

@ -0,0 +1,12 @@
---
layout: ../../layouts/MarkdownPostLayout.astro
title: 我的第二篇博客文章
author: Astro 学习者
description: "学习了一些 Astro 后,我根本停不下来!"
image:
url: "https://docs.astro.build/assets/arc.webp"
alt: "The Astro logo on a dark background with a purple gradient arc."
pubDate: 2022-07-08
tags: ["astro", "blogging", "learning in public", "successes"]
---
在学习 Astro 大约一周后,我决定尝试些新的东西。我编写并导入了一个小组件!

12
src/pages/posts/post-3.md Normal file
View File

@ -0,0 +1,12 @@
---
layout: ../../layouts/MarkdownPostLayout.astro
title: 我的第三篇博客文章
author: Astro 学习者
description: "我遇到了一些问题,但是在社区里面提问真的很有帮助!"
image:
url: "https://docs.astro.build/assets/rays.webp"
alt: "The Astro logo on a dark background with rainbow rays."
pubDate: 2022-07-15
tags: ["astro", "learning in public", "setbacks", "community"]
---
尽管这并不总是一帆风顺,但我很享受使用 Astro 进行搭建。并且,[Discord 社区](https://astro.build/chat)真的很友好而且乐于助人!

12
src/pages/posts/post-4.md Normal file
View File

@ -0,0 +1,12 @@
---
layout: ../../layouts/MarkdownPostLayout.astro
title: '我的第四篇博客文章'
author: 'Astro 学习者'
description: "这篇文章会自己出现在列表中!"
image:
url: "https://docs.astro.build/default-og-image.png"
alt: "The word astro against an illustration of planets and stars."
pubDate: 2022-08-08
tags: ["astro", "successes"]
---
这篇文章应该会与其他的博客文章一起显示,因为 `import.meta.glob()` 会返回一个包含所有文章的列表,以创建这个文章列表。

11
src/pages/rss.xml.js Normal file
View File

@ -0,0 +1,11 @@
import rss, { pagesGlobToRssItems } from '@astrojs/rss';
export async function GET(context) {
return rss({
title: 'Astro Learner | Blog',
description: 'My journey learning Astro',
site: context.site,
items: await pagesGlobToRssItems(import.meta.glob('./**/*.md')),
customData: `<language>en-us</language>`,
});
}

View File

@ -0,0 +1,33 @@
---
import BlogPost from "../../components/BlogPost.astro";
import BaseLayout from "../../layouts/BaseLayout.astro";
export async function getStaticPaths() {
const allPosts = Object.values(
import.meta.glob("../posts/*.md", { eager: true }),
);
const uniqueTags = [
...new Set(allPosts.map((post: any) => post.frontmatter.tags).flat()),
];
return uniqueTags.map((tag) => {
const filteredPosts = allPosts.filter((post: any) =>
post.frontmatter.tags.includes(tag),
);
return {
params: { tag },
props: { posts: filteredPosts },
};
});
}
const { tag } = Astro.params;
const { posts } = Astro.props;
---
<BaseLayout pageTitle={tag}>
<p>包含「{tag}」标签的文章</p>
<ul>
{posts.map((post: any) => <BlogPost url={post.url} title={post.frontmatter.title}/>)}
</ul>
</BaseLayout>

View File

@ -0,0 +1,37 @@
---
import BaseLayout from "../../layouts/BaseLayout.astro";
const allPosts = await Astro.glob("../posts/*.md");
const tags = [...new Set(allPosts.map((post) => post.frontmatter.tags).flat())];
---
<BaseLayout pageTitle="Index">
<div class="tags">
{
tags.map((tag) => (
<p class="tag">
<a href={`/tags/${tag}`}>{tag}</a>
</p>
))
}
</div>
</BaseLayout>
<style>
a {
color: #00539f;
}
.tags {
display: flex;
flex-wrap: wrap;
}
.tag {
margin: 0.25em;
border: dotted 1px #a1a1a1;
border-radius: 0.5em;
padding: 0.5em 1em;
font-size: 1.15em;
background-color: #f8fcfd;
}
</style>

3
src/scripts/menu.js Normal file
View File

@ -0,0 +1,3 @@
document.querySelector('.hamburger').addEventListener('click', () => {
document.querySelector('.nav-links').classList.toggle('expanded');
});

92
src/styles/global.css Normal file
View File

@ -0,0 +1,92 @@
html {
background-color: #f1f5f9;
font-family: sans-serif;
}
body {
margin: 0 auto;
width: 100%;
max-width: 80ch;
padding: 1rem;
line-height: 1.5;
}
* {
box-sizing: border-box;
}
h1 {
margin: 1rem 0;
font-size: 2.5rem;
}
/* 导航样式 */
.hamburger {
padding-right: 20px;
cursor: pointer;
}
.hamburger .line {
display: block;
width: 40px;
height: 5px;
margin-bottom: 10px;
background-color: #ff9776;
}
.nav-links {
width: 100%;
top: 5rem;
left: 48px;
background-color: #ff9776;
display: none;
margin: 0;
}
.nav-links a {
display: block;
text-align: center;
padding: 10px 0;
text-decoration: none;
font-size: 1.2rem;
font-weight: bold;
text-transform: uppercase;
}
.nav-links a:hover,
.nav-links a:focus {
background-color: #ff9776;
}
.expanded {
display: unset;
}
@media screen and (min-width: 636px) {
.nav-links {
margin-left: 5em;
display: block;
position: static;
width: auto;
background: none;
}
.nav-links a {
display: inline-block;
padding: 15px 20px;
}
.hamburger {
display: none;
}
}
html.dark {
background-color: #0d0950;
color: #fff;
}
.dark .nav-links a {
color: #fff;
}

View File

@ -1,5 +1,15 @@
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
"include": [
".astro/types.d.ts",
"**/*"
],
"exclude": [
"dist"
],
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact",
"types": ["astro/client"]
}
}