seperated url and title + escape

This commit is contained in:
2026-05-09 09:59:27 +02:00
parent e6cfd61307
commit 4ff9579cff
10 changed files with 78 additions and 17 deletions
+7 -2
View File
@@ -3,6 +3,7 @@ import type { APIRoute } from 'astro';
interface PostInfo {
slug: string;
date: string;
title?: string;
summary?: string;
excerpt?: string;
tags: string[];
@@ -14,8 +15,12 @@ interface SiteConfig {
subtitle: string;
}
// Strip C0/DEL control chars that are illegal in XML 1.0 (allow tab, LF, CR).
// eslint-disable-next-line no-control-regex
const XML_INVALID = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g;
function escapeXml(s: string): string {
return s.replace(/[<>&'"]/g, c => ({
return s.replace(XML_INVALID, '').replace(/[<>&'"]/g, c => ({
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
@@ -56,7 +61,7 @@ export const GET: APIRoute = async ({ site }) => {
const pubDate = new Date(p.date).toUTCString();
const categories = p.tags.map(t => ` <category>${escapeXml(t)}</category>`).join('\n');
return ` <item>
<title>${escapeXml(formatSlug(p.slug))}</title>
<title>${escapeXml(p.title || formatSlug(p.slug))}</title>
<link>${escapeXml(url)}</link>
<guid isPermaLink="true">${escapeXml(url)}</guid>
<pubDate>${pubDate}</pubDate>
+2
View File
@@ -7,6 +7,7 @@ const API_URL = process.env.PUBLIC_API_URL || 'http://localhost:3000';
interface Post {
slug: string;
date: string;
title?: string;
excerpt?: string;
tags: string[];
draft: boolean;
@@ -78,6 +79,7 @@ function formatSlug(slug: string) {
<PostCard
slug={post.slug}
date={post.date}
title={post.title}
excerpt={post.excerpt}
tags={post.tags}
draft={post.draft}
+3 -2
View File
@@ -9,6 +9,7 @@ interface PostDetail {
slug: string;
date: string;
content: string;
title?: string;
summary?: string;
tags: string[];
draft: boolean;
@@ -46,7 +47,7 @@ const isAdmin = Astro.cookies.get('admin_session')?.value === '1';
---
<Layout
title={post ? formatSlug(post.slug) : 'Post'}
title={post ? (post.title || formatSlug(post.slug)) : 'Post'}
description={post?.summary}
type="article"
>
@@ -72,7 +73,7 @@ const isAdmin = Astro.cookies.get('admin_session')?.value === '1';
<div class="flex flex-col md:flex-row md:justify-between md:items-start mt-2 md:mt-4 gap-4">
<div class="flex-1 min-w-0">
<h1 class="text-3xl md:text-5xl font-extrabold text-mauve mb-3">
{formatSlug(post.slug)}
{post.title || formatSlug(post.slug)}
</h1>
<div class="flex flex-wrap items-center gap-x-3 gap-y-1 text-sm text-subtext0">
<time datetime={post.date}>{formatDate(post.date)}</time>