@jet-w/astro-blog 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jet-w/astro-blog",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "A modern Astro blog theme with Vue and Tailwind CSS support",
5
5
  "type": "module",
6
6
  "exports": {
@@ -36,7 +36,7 @@
36
36
  @click="handleResultClick"
37
37
  >
38
38
  <h4 class="text-sm font-medium text-slate-900 dark:text-slate-100 mb-1" v-html="highlightText(result.title)"></h4>
39
- <p class="text-xs text-slate-600 dark:text-slate-400 line-clamp-2" v-html="highlightText(result.description)"></p>
39
+ <p class="text-xs text-slate-600 dark:text-slate-400 line-clamp-2" v-html="highlightText(getMatchedContent(result))"></p>
40
40
  <div class="flex flex-wrap gap-1 mt-2">
41
41
  <span
42
42
  v-for="tag in result.tags.slice(0, 3)"
@@ -138,6 +138,38 @@ const handleResultClick = () => {
138
138
  searchResults.value = []
139
139
  }
140
140
 
141
+ // 获取匹配的内容片段,优先显示匹配到的内容上下文
142
+ const getMatchedContent = (result: SearchResult): string => {
143
+ const query = searchQuery.value.toLowerCase()
144
+
145
+ // 如果标题或描述匹配,优先显示描述
146
+ if (result.title.toLowerCase().includes(query) || result.description.toLowerCase().includes(query)) {
147
+ return result.description || result.content.substring(0, 150)
148
+ }
149
+
150
+ // 如果内容匹配,显示匹配位置的上下文
151
+ if (result.content) {
152
+ const contentLower = result.content.toLowerCase()
153
+ const matchIndex = contentLower.indexOf(query)
154
+
155
+ if (matchIndex !== -1) {
156
+ // 提取匹配位置前后的上下文
157
+ const contextStart = Math.max(0, matchIndex - 50)
158
+ const contextEnd = Math.min(result.content.length, matchIndex + query.length + 100)
159
+ let snippet = result.content.substring(contextStart, contextEnd)
160
+
161
+ // 添加省略号
162
+ if (contextStart > 0) snippet = '...' + snippet
163
+ if (contextEnd < result.content.length) snippet = snippet + '...'
164
+
165
+ return snippet
166
+ }
167
+ }
168
+
169
+ // 默认返回描述或内容开头
170
+ return result.description || result.content.substring(0, 150)
171
+ }
172
+
141
173
  const highlightText = (text: string) => {
142
174
  if (!searchQuery.value || !text) return text || ''
143
175
 
@@ -1,17 +1,55 @@
1
1
  import type { APIRoute } from 'astro';
2
2
  import { getCollection } from 'astro:content';
3
3
 
4
+ // 从 Markdown 内容中提取纯文本
5
+ function extractPlainText(markdown: string): string {
6
+ if (!markdown) return '';
7
+
8
+ return markdown
9
+ // 移除代码块
10
+ .replace(/```[\s\S]*?```/g, '')
11
+ .replace(/`[^`]*`/g, '')
12
+ // 移除 HTML 标签
13
+ .replace(/<[^>]*>/g, '')
14
+ // 移除图片
15
+ .replace(/!\[.*?\]\(.*?\)/g, '')
16
+ // 移除链接但保留文本
17
+ .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')
18
+ // 移除标题标记
19
+ .replace(/^#{1,6}\s+/gm, '')
20
+ // 移除粗体/斜体标记
21
+ .replace(/\*\*([^*]*)\*\*/g, '$1')
22
+ .replace(/\*([^*]*)\*/g, '$1')
23
+ .replace(/__([^_]*)__/g, '$1')
24
+ .replace(/_([^_]*)_/g, '$1')
25
+ // 移除引用标记
26
+ .replace(/^>\s+/gm, '')
27
+ // 移除列表标记
28
+ .replace(/^[\s]*[-*+]\s+/gm, '')
29
+ .replace(/^[\s]*\d+\.\s+/gm, '')
30
+ // 移除水平线
31
+ .replace(/^[-*_]{3,}$/gm, '')
32
+ // 移除多余空白
33
+ .replace(/\n{3,}/g, '\n\n')
34
+ .trim();
35
+ }
36
+
4
37
  export const GET: APIRoute = async () => {
5
38
  const posts = await getCollection('posts', ({ data }) => !data.draft);
6
39
 
7
- const searchIndex = posts.map(post => ({
8
- title: post.data.title,
9
- description: post.data.description || '',
10
- url: `/posts/${post.id.toLowerCase()}`,
11
- content: post.body?.substring(0, 500) || '', // 取前500字符作为搜索内容
12
- tags: post.data.tags || [],
13
- categories: post.data.categories || []
14
- }));
40
+ const searchIndex = posts.map(post => {
41
+ // 提取纯文本内容用于搜索
42
+ const plainContent = extractPlainText(post.body || '');
43
+
44
+ return {
45
+ title: post.data.title,
46
+ description: post.data.description || '',
47
+ url: `/posts/${post.id.toLowerCase()}`,
48
+ content: plainContent, // 完整的纯文本内容
49
+ tags: post.data.tags || [],
50
+ categories: post.data.categories || []
51
+ };
52
+ });
15
53
 
16
54
  return new Response(JSON.stringify(searchIndex), {
17
55
  headers: {