-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
songxingguo
committed
Jun 5, 2024
1 parent
c9468c6
commit 664a0d3
Showing
7 changed files
with
66 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule posts
updated
3 files
+0 −2 | 从零开始搭建在线个人简历.md | |
+0 −2 | 使用Github Action部署静态网站.md | |
+0 −2 | 如何将语雀文章发布到Hexo博客.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,60 @@ | ||
import { getCollection } from 'astro:content' | ||
import sanitizeHtml from 'sanitize-html' | ||
import MarkdownIt from 'markdown-it' | ||
import { getCollection } from "astro:content"; | ||
import sanitizeHtml from "sanitize-html"; | ||
import MarkdownIt from "markdown-it"; | ||
|
||
export async function getCategories() { | ||
const posts = await getPosts() | ||
const posts = await getPosts(); | ||
|
||
const categories = new Map<string, Post[]>() | ||
const categories = new Map<string, Post[]>(); | ||
|
||
posts.forEach((post) => { | ||
if (post.data.categories) { | ||
post.data.categories.forEach((c) => { | ||
const posts = categories.get(c) || [] | ||
posts.push(post) | ||
categories.set(c, posts) | ||
}) | ||
const posts = categories.get(c) || []; | ||
posts.push(post); | ||
categories.set(c, posts); | ||
}); | ||
} | ||
}) | ||
}); | ||
|
||
return categories | ||
return categories; | ||
} | ||
|
||
export async function getPosts() { | ||
const posts = await getCollection('posts') | ||
const posts = await getCollection("posts"); | ||
posts.sort((a, b) => { | ||
const aDate = a.data.pubDate || new Date() | ||
const bDate = b.data.pubDate || new Date() | ||
return bDate.getTime() - aDate.getTime() | ||
}) | ||
return posts | ||
const aDate = a.data.date || new Date(); | ||
const bDate = b.data.date || new Date(); | ||
return bDate.getTime() - aDate.getTime(); | ||
}); | ||
return posts; | ||
} | ||
|
||
const parser = new MarkdownIt() | ||
const parser = new MarkdownIt(); | ||
|
||
export function getPostDescription(post: Post) { | ||
if (post.data.description) { | ||
return post.data.description | ||
return post.data.description; | ||
} | ||
|
||
const html = parser.render(post.body) | ||
const sanitized = sanitizeHtml(html, { allowedTags: [] }) | ||
return sanitized.slice(0, 400) | ||
const html = parser.render(post.body); | ||
const sanitized = sanitizeHtml(html, { allowedTags: [] }); | ||
return sanitized.slice(0, 400); | ||
} | ||
|
||
export function formatDate(date?: Date) { | ||
if(!date) return '--' | ||
const year = date.getFullYear().toString().padStart(4, '0') | ||
const month = (date.getMonth() + 1).toString().padStart(2, '0') | ||
const day = date.getDate().toString().padStart(2, '0') | ||
if (!date) return "--"; | ||
const year = date.getFullYear().toString().padStart(4, "0"); | ||
const month = (date.getMonth() + 1).toString().padStart(2, "0"); | ||
const day = date.getDate().toString().padStart(2, "0"); | ||
|
||
return `${year}-${month}-${day}` | ||
return `${year}-${month}-${day}`; | ||
} | ||
|
||
export function getPathFromCategory(category: string, category_map: {name: string, path: string}[]) { | ||
const mappingPath = category_map.find(l => l.name === category) | ||
return mappingPath ? mappingPath.path : category | ||
} | ||
export function getPathFromCategory( | ||
category: string, | ||
category_map: { name: string; path: string }[] | ||
) { | ||
const mappingPath = category_map.find((l) => l.name === category); | ||
return mappingPath ? mappingPath.path : category; | ||
} |