refactor: 优化文章摘要生成逻辑并移除多余的阅读全文链接
This commit is contained in:
1 parent
de02716e11
commit
0a0f921040
2 files changed
+14
-3
No files matched your search
@@ -40,12 +40,23 @@ export const load: PageServerLoad = async ({ platform }) => {
|
|||||||
const year = d.getFullYear();
|
const year = d.getFullYear();
|
||||||
|
|
||||||
let excerpt = '';
|
let excerpt = '';
|
||||||
const contentStr = post.content as string || '';
|
let contentStr = post.content as string || '';
|
||||||
|
|
||||||
|
// Filter out Hugo shortcodes like {{< admonition abstract "标题" >}}
|
||||||
|
contentStr = contentStr.replace(/\{\{<[\s\S]*?>\}\}/g, '');
|
||||||
|
|
||||||
const parts = contentStr.split('<!--more-->');
|
const parts = contentStr.split('<!--more-->');
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
excerpt = parts[0].trim();
|
excerpt = parts[0].trim();
|
||||||
} else {
|
} else {
|
||||||
excerpt = contentStr.replace(/[#*\`>~\\[\\]\\(\\)\\-]/g, '').substring(0, 150) + (contentStr.length > 150 ? '...' : '');
|
// Strip basic markdown syntax for the automatic excerpt
|
||||||
|
let plainText = contentStr.replace(/[#*\`>~\\[\\]\\(\\)\\-]/g, '').trim();
|
||||||
|
excerpt = plainText.substring(0, 150).trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append ... to indicate there is more to read
|
||||||
|
if (excerpt && !excerpt.endsWith('...')) {
|
||||||
|
excerpt += '...';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not send full content to the list view to save bandwidth
|
// Do not send full content to the list view to save bandwidth
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
<time>{post.date}</time>
|
<time>{post.date}</time>
|
||||||
</div>
|
</div>
|
||||||
<h1 class="title">{post.title}</h1>
|
<h1 class="title">{post.title}</h1>
|
||||||
<p class="excerpt">{post.excerpt} | <span class="full-link">阅读全文</span></p>
|
<p class="excerpt">{post.excerpt}</p>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
Reference in new issue
Block a user