Skip to content

Commit 8811842

Browse files
authored
feat: render description links (#22)
1 parent d69e953 commit 8811842

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/components/BaseHead.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ interface Props {
55
image?: string;
66
}
77
8-
import { withBase } from "../lib/utils";
9-
const { title, description = "The official blog of the Python core development team.", image } = Astro.props;
8+
import { withBase, stripDescriptionLinks } from "../lib/utils";
9+
const { title, description: rawDescription = "The official blog of the Python core development team.", image } = Astro.props;
10+
const description = stripDescriptionLinks(rawDescription);
1011
const baseUrl = import.meta.env.DEV ? Astro.url.origin : Astro.site;
1112
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
1213
const ogImage = image ? new URL(image, baseUrl) : new URL(withBase("/og-default.png"), baseUrl);

src/components/BlogPostCard.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
import { formatDate, postUrl, slugify, withBase } from "../lib/utils";
2+
import { formatDate, postUrl, slugify, withBase, renderDescriptionLinks } from "../lib/utils";
33
44
interface Props {
55
slug: string;
@@ -29,7 +29,7 @@ const { slug, title, publishDate, author, description, tags, showEditLink = true
2929
<time datetime={publishDate}>{formatDate(publishDate)}</time>
3030
</div>
3131
{description && (
32-
<p class="mt-1.5 line-clamp-2 text-sm leading-relaxed text-zinc-500 dark:text-zinc-400">{description}</p>
32+
<p class="mt-1.5 line-clamp-2 text-sm leading-relaxed text-zinc-500 dark:text-zinc-400" set:html={renderDescriptionLinks(description)} />
3333
)}
3434
{tags && tags.length > 0 && (
3535
<div class="mt-2.5 flex flex-wrap gap-1.5">

src/lib/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ export function postUrl(slug: string, publishDate: string | Date): string {
3434
return `/${year}/${month}/${slug}`;
3535
}
3636

37+
/**
38+
* Converts markdown-style links [text](url) in a description to HTML anchor tags.
39+
*/
40+
export function renderDescriptionLinks(desc: string): string {
41+
return desc.replace(
42+
/\[([^\]]+)\]\(([^)]+)\)/g,
43+
'<a href="$2" class="text-amber-700 underline hover:text-amber-900 dark:text-amber-400 dark:hover:text-amber-300">$1</a>',
44+
);
45+
}
46+
47+
/**
48+
* Strips markdown-style links [text](url) to plain text for meta tags.
49+
*/
50+
export function stripDescriptionLinks(desc: string): string {
51+
return desc.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");
52+
}
53+
3754
export function slugify(text: string): string {
3855
return text
3956
.normalize("NFD")

src/pages/index.astro

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const prerender = true;
33
44
import BaseLayout from "../layouts/BaseLayout.astro";
55
import PythonLogo from "../components/PythonLogo.astro";
6-
import { formatDate, postUrl, withBase } from "../lib/utils";
6+
import { formatDate, postUrl, withBase, renderDescriptionLinks } from "../lib/utils";
77
import { getCollection } from "astro:content";
88
99
const allPosts = await getCollection("posts");
@@ -66,9 +66,7 @@ const authors = new Set(posts.map((p) => p.data.author));
6666
</div>
6767

6868
{featured.data.description && (
69-
<p class="mt-3 max-w-2xl text-base leading-relaxed text-zinc-600 dark:text-zinc-400 sm:text-lg">
70-
{featured.data.description}
71-
</p>
69+
<p class="mt-3 max-w-2xl text-base leading-relaxed text-zinc-600 dark:text-zinc-400 sm:text-lg" set:html={renderDescriptionLinks(featured.data.description)} />
7270
)}
7371
</a>
7472

@@ -132,7 +130,7 @@ const authors = new Set(posts.map((p) => p.data.author));
132130
<time datetime={post.data.publishDate.toISOString()}>{formatDate(post.data.publishDate.toISOString())}</time>
133131
</div>
134132
{post.data.description && (
135-
<p class="mt-2 line-clamp-2 text-sm leading-relaxed text-zinc-500 dark:text-zinc-400">{post.data.description}</p>
133+
<p class="mt-2 line-clamp-2 text-sm leading-relaxed text-zinc-500 dark:text-zinc-400" set:html={renderDescriptionLinks(post.data.description)} />
136134
)}
137135
</article>
138136
))}

0 commit comments

Comments
 (0)