Skip to content

Commit

Permalink
feat: support rss (#42)
Browse files Browse the repository at this point in the history
* feat: support rss

* fix: title of plugins page

* tweaks

* 放弃使用插件

* Add RSS icon and clean code

* clean code
  • Loading branch information
northword committed Jun 20, 2024
1 parent 0188449 commit 9318f6a
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { markdown } from "./config/markdown.config";
import { head } from "./config/head.config";
import vite from "./config/vite.config";

const RssIcon = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 448 512"><title>RSS</title><path d="M400 32H48C21.49 32 0 53.49 0 80v352c0 26.51 21.49 48 48 48h352c26.51 0 48-21.49 48-48V80c0-26.51-21.49-48-48-48zM112 416c-26.51 0-48-21.49-48-48s21.49-48 48-48s48 21.49 48 48s-21.49 48-48 48zm157.533 0h-34.335c-6.011 0-11.051-4.636-11.442-10.634c-5.214-80.05-69.243-143.92-149.123-149.123c-5.997-.39-10.633-5.431-10.633-11.441v-34.335c0-6.535 5.468-11.777 11.994-11.425c110.546 5.974 198.997 94.536 204.964 204.964c.352 6.526-4.89 11.994-11.425 11.994zm103.027 0h-34.334c-6.161 0-11.175-4.882-11.427-11.038c-5.598-136.535-115.204-246.161-251.76-251.76C68.882 152.949 64 147.935 64 141.774V107.44c0-6.454 5.338-11.664 11.787-11.432c167.83 6.025 302.21 141.191 308.205 308.205c.232 6.449-4.978 11.787-11.432 11.787z" fill="currentColor"></path></svg>`;

export default defineConfig({
title: "Zotero 中文社区",
description: "Zotero 非官方中文维护小组",
Expand Down Expand Up @@ -43,6 +45,7 @@ export default defineConfig({

socialLinks: [
{ icon: "github", link: "https://github.com/zotero-chinese/" },
{ icon: { svg: RssIcon }, link: "/rss.rss", ariaLabel: "RSS Feed" },
],

search: {
Expand Down
84 changes: 57 additions & 27 deletions .vitepress/config/buildEnd.config.ts
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");
};
8 changes: 8 additions & 0 deletions .vitepress/config/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from "@nolebase/vitepress-plugin-git-changelog/vite";
import { MarkdownTransform } from ".vitepress/plugins/markdownTransform";

import { RssPlugin } from "vitepress-plugin-rss";

export default defineConfig({
define: {
// 启用生产环境构建下激活不匹配的详细警告
Expand All @@ -30,6 +32,12 @@ export default defineConfig({
}),
MarkdownTransform(),

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

// Git Changelog
GitChangelog({
includeDirs: ["src/wiki/**/*.md"],
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)
);
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"highcharts": "11.4.3",
"highcharts-vue": "2.0.1",
"vitepress": "1.2.3",
"vitepress-plugin-rss": "^0.2.6",
"vue": "3.4.29"
},
"devDependencies": {
Expand All @@ -41,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 9318f6a

Please sign in to comment.