-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support rss * fix: title of plugins page * tweaks * 放弃使用插件 * Add RSS icon and clean code * clean code
- Loading branch information
Showing
8 changed files
with
253 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,79 @@ | ||
import path from "path"; | ||
import { writeFileSync } from "fs"; | ||
import path, { join } from "path"; | ||
import { writeFileSync, copyFileSync } from "fs"; | ||
import { Feed } from "feed"; | ||
import { createContentLoader, type SiteConfig } from "vitepress"; | ||
import { getGitTimestamp } from ".vitepress/utils/getGitTimestamp"; | ||
import { getDefaultTitle, getTextSummary } from ".vitepress/utils/markdown"; | ||
import FastGlob from "fast-glob"; | ||
|
||
const siteUrl = "https://vitejs.dev"; | ||
const blogUrl = `${siteUrl}/blog`; | ||
const siteUrl = "https://zotero-chinese.com"; | ||
|
||
export const buildEnd = async (config: SiteConfig) => { | ||
const feed = new Feed({ | ||
title: "Vite", | ||
description: "Next Generation Frontend Tooling", | ||
id: blogUrl, | ||
link: blogUrl, | ||
language: "en", | ||
image: "https://vitejs.dev/og-image.png", | ||
favicon: "https://vitejs.dev/logo.svg", | ||
copyright: "Copyright © 2019-present Evan You & Vite Contributors", | ||
title: "Zotero 中文社区", | ||
description: "非官方 Zotero 中文维护小组", | ||
id: siteUrl, | ||
link: siteUrl, | ||
language: "zh", | ||
image: "https://zotero-chinese/logo.png", | ||
favicon: "https://zotero-chinese.com/logo.png", | ||
copyright: "Copyright © 2018-present Zotero 中文社区及贡献者", | ||
}); | ||
|
||
const posts = await createContentLoader("blog/*.md", { | ||
const paths = await FastGlob.glob("src/wiki/**/*.md", { | ||
ignore: ["README.md", "node_modules"], | ||
}); | ||
|
||
// 获取每一条路径的 Git 时间 | ||
const updatedDates = await Promise.all( | ||
paths.map(async (path) => { | ||
return { | ||
router: path | ||
.replace("src", "") | ||
.replace("index.md", "") | ||
.replace(".md", ""), | ||
updated: new Date(await getGitTimestamp(path)), | ||
}; | ||
}), | ||
); | ||
|
||
// 渲染 md | ||
const posts = await createContentLoader("wiki/**/*.md", { | ||
includeSrc: true, | ||
excerpt: true, | ||
render: true, | ||
// render: true, | ||
}).load(); | ||
|
||
posts.sort( | ||
(a, b) => | ||
+new Date(b.frontmatter.date as string) - | ||
+new Date(a.frontmatter.date as string), | ||
); | ||
// 匹配时间 | ||
posts.map((post) => { | ||
post.frontmatter.updated = updatedDates.find( | ||
(v) => v.router === post.url, | ||
)?.updated; | ||
return post; | ||
}); | ||
|
||
for (const { url, excerpt, frontmatter, html } of posts) { | ||
posts.sort((a, b) => b.frontmatter.updated - a.frontmatter.updated); | ||
|
||
for (const { url, excerpt, frontmatter, html, src } of posts) { | ||
feed.addItem({ | ||
title: frontmatter.title, | ||
title: frontmatter.title || getDefaultTitle(src!), | ||
id: `${siteUrl}${url}`, | ||
link: `${siteUrl}${url}`, | ||
description: excerpt, | ||
content: html, | ||
link: `${siteUrl}${url.replace("wiki/", "")}`, | ||
description: excerpt || getTextSummary(src!), | ||
// content: html, | ||
author: [ | ||
{ | ||
name: frontmatter.author.name, | ||
name: "Zotero 中文社区", | ||
}, | ||
], | ||
date: frontmatter.date, | ||
date: frontmatter.updated || new Date(), | ||
}); | ||
} | ||
|
||
writeFileSync(path.join(config.outDir, "blog.rss"), feed.rss2()); | ||
writeFileSync(path.join(config.outDir, "feed.rss"), feed.rss2()); | ||
copyFileSync( | ||
path.join(config.outDir, "feed.rss"), | ||
path.join(config.outDir, "rss.rss"), | ||
); | ||
console.log("🎉 RSS generated"); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { execa } from "execa"; | ||
import fs from "fs-extra"; | ||
import { basename, dirname } from "path"; | ||
|
||
const cache = new Map<string, number>(); | ||
|
||
export function getGitTimestamp(file: string) { | ||
const cached = cache.get(file); | ||
if (cached) return cached; | ||
|
||
return new Promise<number>((resolve, reject) => { | ||
const cwd = dirname(file); | ||
if (!fs.existsSync(cwd)) return resolve(0); | ||
const fileName = basename(file); | ||
const child = execa("git", ["log", "-1", '--pretty="%ai"', fileName], { | ||
cwd, | ||
}); | ||
let output = ""; | ||
child.stdout.on("data", (d) => (output += String(d))); | ||
child.on("close", () => { | ||
const timestamp = +new Date(output); | ||
cache.set(file, timestamp); | ||
resolve(timestamp); | ||
}); | ||
child.on("error", reject); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* 获取 markdown 内容中的标题 | ||
*/ | ||
export function getDefaultTitle(content: string) { | ||
const match = content.match(/^(#+)\s+(.+)/m); | ||
return match?.[2] || ""; | ||
} | ||
|
||
export function getTextSummary(text: string, count = 100) { | ||
return ( | ||
text | ||
// 去除frontmatter | ||
?.replace(/^---[\s\S]*?---/, "") | ||
// 首个标题 | ||
?.replace(/^#+\s+.*/, "") | ||
// 除去标题 | ||
?.replace(/#/g, "") | ||
// 除去图片 | ||
?.replace(/!\[.*?\]\(.*?\)/g, "") | ||
// 除去链接 | ||
?.replace(/\[(.*?)\]\(.*?\)/g, "$1") | ||
// 除去加粗 | ||
?.replace(/\*\*(.*?)\*\*/g, "$1") | ||
?.split("\n") | ||
?.filter((v) => !!v) | ||
?.join("\n") | ||
?.replace(/>(.*)/, "") | ||
?.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
?.trim() | ||
?.slice(0, count) | ||
); | ||
} |
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
Oops, something went wrong.