Skip to content

Commit

Permalink
放弃使用插件
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Jun 20, 2024
1 parent 561408b commit ddeac7c
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default defineConfig({

markdown,

// buildEnd,
buildEnd,

vite,
metaChunk: true,
Expand Down
64 changes: 49 additions & 15 deletions .vitepress/config/buildEnd.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import path from "path";
import path, { join } from "path";
import { writeFileSync } from "fs";
import { Feed } from "feed";
import { createContentLoader, type SiteConfig } from "vitepress";
import {
createContentLoader,
createMarkdownRenderer,
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://zotero-chinese.com";

Expand All @@ -13,36 +20,63 @@ export const buildEnd = async (config: SiteConfig) => {
link: siteUrl,
language: "zh",
image: "https://zotero-chinese/logo.png",
favicon: "https://vitejs.dev/logo.svg",
favicon: "https://zotero-chinese.com/logo.png",
copyright: "Copyright © 2018-present Zotero 中文社区及贡献者",
});

const posts = await createContentLoader("**/*.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: await getGitTimestamp(path),
};
}),
);

// 渲染 md
const posts = await createContentLoader("wiki/**/*.md", {
includeSrc: true,
excerpt: true,
render: true,
// render: true,
}).load();

// 匹配时间
posts.map((post) => {
post.frontmatter.updated = updatedDates.find(
(v) => v.router === post.url,
)?.updated;
return post;
});

posts.sort(
(a, b) =>
+new Date(b.frontmatter.date as string) -
+new Date(a.frontmatter.date as string),
+new Date(b.frontmatter.updated) - +new Date(a.frontmatter.updated),
);

for (const { url, excerpt, frontmatter, html } of posts) {
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: new Date(frontmatter.updated || frontmatter.date) || new Date(),
});
}

writeFileSync(path.join(config.outDir, "blog.rss"), feed.rss2());
writeFileSync(path.join(config.outDir, "rss.rss"), feed.rss2());
console.log("🎉 RSS generated");
};
10 changes: 5 additions & 5 deletions .vitepress/config/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export default defineConfig({
}),
MarkdownTransform(),

RssPlugin({
title: "Zotero 中文社区",
baseUrl: "https://zotero-chinese.com",
copyright: "Copyright (c) 2018-present, Zotero 中文社区",
}),
// RssPlugin({
// title: "Zotero 中文社区",
// baseUrl: "https://zotero-chinese.com",
// copyright: "Copyright (c) 2018-present, Zotero 中文社区",
// }),

// Git Changelog
GitChangelog({
Expand Down
27 changes: 27 additions & 0 deletions .vitepress/utils/getGitTimestamp.ts
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);
});
}
33 changes: 33 additions & 0 deletions .vitepress/utils/markdown.ts
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, "&lt;")
.replace(/>/g, "&gt;")
?.trim()
?.slice(0, count)
);
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"@vue/tsconfig": "0.5.1",
"eslint": "8.57.0",
"eslint-plugin-vue": "9.26.0",
"execa": "^9.2.0",
"fast-glob": "^3.3.2",
"fs-extra": "11.2.0",
"husky": "9.0.11",
"lint-staged": "15.2.7",
Expand Down
Loading

0 comments on commit ddeac7c

Please sign in to comment.