-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBlogCard.svelte
More file actions
158 lines (134 loc) · 4.18 KB
/
BlogCard.svelte
File metadata and controls
158 lines (134 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<script lang="ts">
import Panel from '$lib/components/Panel.svelte';
import Button from '$lib/components/Button.svelte';
import type { BlogPost } from '$lib/blog/types.js';
let { post }: { post: BlogPost } = $props();
function formatDate(dateString: string) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
function truncateContent(content: string, maxLength = 300) {
// Remove YouTube embeds and other media
let cleanContent = content
// Remove YouTube URLs (various formats)
.replace(/https?:\/\/(www\.)?(youtube\.com\/(embed\/|watch\?v=)|youtu\.be\/)[^\s\n\r\])]*/g, '')
// Remove other video embeds
.replace(/https?:\/\/(www\.)?(vimeo\.com|dailymotion\.com|twitch\.tv)[^\s\n\r\])]*/g, '')
// Remove image markdown
.replace(/!\[.*?\]\(.*?\)/g, '')
// Remove HTML img tags
.replace(/<img[^>]*>/g, '')
// Remove HTML iframe tags (embeds)
.replace(/<iframe[^>]*>.*?<\/iframe>/gs, '')
// Remove HTML video/audio tags
.replace(/<(video|audio)[^>]*>.*?<\/(video|audio)>/gs, '')
// Remove code blocks
.replace(/```[\s\S]*?```/g, '')
// Remove inline code
.replace(/`[^`]*`/g, '')
// Remove markdown links but keep text
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
// Remove extra whitespace and line breaks
.replace(/\s+/g, ' ')
.trim();
if (cleanContent.length <= maxLength) {
return cleanContent;
}
return cleanContent.substring(0, maxLength) + '...';
}
</script>
<article class="blog-card" data-test="blog-post-{post.slug}">
<Panel>
<h2 class="blog-title">
<a href="/blogs/{post.slug}" data-test="blog-link-{post.slug}">
{post.title}
</a>
</h2>
<div class="blog-meta">
<div class="blog-author">
<img
src={post.author.avatar}
alt="{post.author.name} avatar"
width="24"
height="24"
/>
<span>{post.author.name}</span>
</div>
<time datetime={post.publishedAt}>
{formatDate(post.publishedAt)}
</time>
</div>
<div class="blog-content">
{truncateContent(post.content)}
</div>
{#if post.labels.length > 0}
<div class="blog-labels">
{#each post.labels as label}
{#if label !== 'blog'}
<span class="label">{label}</span>
{/if}
{/each}
</div>
{/if}
<a
href="/blogs/{post.slug}"
class="button small"
data-test="blog-read-more-{post.slug}"
>
Read more
</a>
</Panel>
</article>
<style lang="postcss">
.blog-card {
height: 100%;
}
.blog-meta {
display: flex;
align-items: center;
gap: 1em;
margin-bottom: 1rem;
font-size: 0.875rem;
color: var(--color-text-muted);
}
.blog-author {
display: flex;
align-items: center;
gap: 0.5rem;
}
.blog-author img {
border-radius: 50%;
}
.blog-title {
text-align: left;
font-size: 1.25rem;
& a {
color: inherit;
text-decoration: none;
transition: color 0.2s ease;
}
}
.blog-content {
margin-bottom: 1rem;
line-height: 1.6;
color: var(--color-text-secondary);
}
.blog-labels {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
margin-bottom: 1rem;
}
.label {
display: inline-block;
padding: 0.25rem 0.5rem;
background-color: var(--color-bg-secondary);
border-radius: 0.25rem;
font-size: 0.75rem;
color: var(--color-text-muted);
}
</style>