|
1 |
| -import fs from "fs"; |
2 |
| -import { CopyrightAnnouncement, LatestPostCountInHomePage, WebsiteURL } from "@/consts/consts"; |
3 |
| -import { Config } from "@/data/config"; |
4 |
| -import { Feed } from "feed"; |
5 |
| -import { JSDOM } from "jsdom"; |
6 |
| -import { MDXRemote } from "next-mdx-remote"; |
7 |
| -import { serialize } from "next-mdx-remote/serialize"; |
8 |
| -import { renderToString } from "react-dom/server"; |
9 |
| -import rehypeAutolinkHeadings from "rehype-autolink-headings"; |
10 |
| -import rehypeMathJax from "rehype-mathjax/svg"; |
11 |
| -import rehypePresetMinify from "rehype-preset-minify"; |
12 |
| -import rehypeRaw from "rehype-raw"; |
13 |
| -import rehypeSlug from "rehype-slug"; |
14 |
| -import externalLinks from "remark-external-links"; |
15 |
| -import remarkGfm from "remark-gfm"; |
16 |
| -import remarkMath from "remark-math"; |
17 |
| -import remarkPrism from "remark-prism"; |
18 |
| -import { getPostFileContent, sortedPosts } from "./post-process"; |
19 |
| - |
20 |
| -const NoticeForRSSReaders = (postId: string) => ` |
21 |
| ---- |
22 |
| -**NOTE:** Different RSS reader may have deficient even no support for svg formulations rendering. |
23 |
| -If it happens, [please read the origin web page](https://${Config.SiteDomain}/blog/${postId}) to have better experience |
24 |
| -`; |
25 |
| - |
26 |
| -function minifyHTMLCode(htmlString: string): string { |
27 |
| - const dom = new JSDOM(htmlString); |
28 |
| - const document = dom.window.document; |
29 |
| - const elements = document.querySelectorAll("*"); |
30 |
| - const unusedElements = document.querySelectorAll("script, style"); |
31 |
| - |
32 |
| - // Remove all class attributes. |
33 |
| - elements.forEach((element) => { |
34 |
| - element.removeAttribute("class"); |
35 |
| - }); |
36 |
| - |
37 |
| - // Remove all script and style tags. |
38 |
| - unusedElements.forEach((element) => { |
39 |
| - element.parentElement?.removeChild(element); |
40 |
| - }); |
41 |
| - |
42 |
| - return dom.serialize(); |
43 |
| -} |
44 |
| - |
45 |
| -/** |
46 |
| - * Generate the RSS Feed File in `./public` so it could be visited by https://domain/rss.xml |
47 |
| - */ |
48 |
| -export const generateRSSFeed = async () => { |
49 |
| - const feed = new Feed({ |
50 |
| - title: Config.SiteTitle, |
51 |
| - description: Config.Sentence, |
52 |
| - id: Config.SiteDomain, |
53 |
| - link: WebsiteURL, |
54 |
| - image: Config.PageCovers.websiteCoverURL, |
55 |
| - favicon: `https://${Config.SiteDomain}/favcion.ico`, |
56 |
| - copyright: CopyrightAnnouncement, |
57 |
| - generator: "Node.js Feed", |
58 |
| - author: { |
59 |
| - name: Config.AuthorName, |
60 |
| - email: Config.SocialLinks.email, |
61 |
| - link: WebsiteURL, |
62 |
| - }, |
63 |
| - }); |
64 |
| - |
65 |
| - for (let i = 0; i < Math.min(LatestPostCountInHomePage, sortedPosts.allPostList.length); i++) { |
66 |
| - const post = sortedPosts.allPostList[i]; |
67 |
| - const postFileContent = `${getPostFileContent(post.id)}${NoticeForRSSReaders(post.id)}`; |
68 |
| - const dateNumber = post.frontMatter.time.split("-").map((num: string) => Number.parseInt(num)); |
69 |
| - const mdxSource = await serialize(postFileContent ?? "", { |
70 |
| - parseFrontmatter: true, |
71 |
| - mdxOptions: { |
72 |
| - remarkPlugins: [remarkPrism, externalLinks, remarkMath, remarkGfm], |
73 |
| - rehypePlugins: [rehypeMathJax, rehypeAutolinkHeadings, rehypeSlug, rehypePresetMinify as any, rehypeRaw], |
74 |
| - format: "md", |
75 |
| - }, |
76 |
| - }); |
77 |
| - const htmlContent = minifyHTMLCode(renderToString(<MDXRemote {...mdxSource} />)); |
78 |
| - |
79 |
| - feed.addItem({ |
80 |
| - title: post.frontMatter.title, |
81 |
| - id: post.id, |
82 |
| - link: `https://${Config.SiteDomain}/blog/${post.id}`, |
83 |
| - description: post.frontMatter.summary ?? undefined, |
84 |
| - content: htmlContent, |
85 |
| - author: [ |
86 |
| - { |
87 |
| - name: Config.AuthorName, |
88 |
| - email: Config.SocialLinks.email, |
89 |
| - link: `https://${Config.SiteDomain}/about`, |
90 |
| - }, |
91 |
| - ], |
92 |
| - category: post.frontMatter.tags?.map((tagname: string) => ({ |
93 |
| - name: tagname, |
94 |
| - })), |
95 |
| - date: new Date(dateNumber[0], dateNumber[1] - 1, dateNumber[2]), |
96 |
| - image: post.frontMatter.coverURL ?? undefined, |
97 |
| - }); |
98 |
| - } |
99 |
| - fs.writeFile("./public/rss.xml", feed.rss2(), "utf-8", (err) => {}); |
100 |
| -}; |
| 1 | +import fs from "fs"; |
| 2 | +import { CopyrightAnnouncement, LatestPostCountInHomePage, WebsiteURL } from "@/consts/consts"; |
| 3 | +import { Config } from "@/data/config"; |
| 4 | +import { Feed } from "feed"; |
| 5 | +import { JSDOM } from "jsdom"; |
| 6 | +import { MDXRemote } from "next-mdx-remote"; |
| 7 | +import { serialize } from "next-mdx-remote/serialize"; |
| 8 | +import { renderToString } from "react-dom/server"; |
| 9 | +import rehypeAutolinkHeadings from "rehype-autolink-headings"; |
| 10 | +import rehypeMathJax from "rehype-mathjax/svg"; |
| 11 | +import rehypePresetMinify from "rehype-preset-minify"; |
| 12 | +import rehypeRaw from "rehype-raw"; |
| 13 | +import rehypeSlug from "rehype-slug"; |
| 14 | +import externalLinks from "remark-external-links"; |
| 15 | +import remarkGfm from "remark-gfm"; |
| 16 | +import remarkMath from "remark-math"; |
| 17 | +import remarkPrism from "remark-prism"; |
| 18 | +import { getPostFileContent, sortedPosts } from "./post-process"; |
| 19 | + |
| 20 | +const NoticeForRSSReaders = (postId: string) => ` |
| 21 | +--- |
| 22 | +**NOTE:** Different RSS reader may have deficient even no support for svg formulations rendering. |
| 23 | +If it happens, [please read the origin web page](https://${Config.SiteDomain}/blog/${postId}) to have better experience |
| 24 | +`; |
| 25 | + |
| 26 | +function minifyHTMLCode(htmlString: string): string { |
| 27 | + const dom = new JSDOM(htmlString); |
| 28 | + const document = dom.window.document; |
| 29 | + const elements = document.querySelectorAll("*"); |
| 30 | + const unusedElements = document.querySelectorAll("script, style"); |
| 31 | + |
| 32 | + // Remove all class attributes. |
| 33 | + elements.forEach((element) => { |
| 34 | + element.removeAttribute("class"); |
| 35 | + }); |
| 36 | + |
| 37 | + // Remove all script and style tags. |
| 38 | + unusedElements.forEach((element) => { |
| 39 | + element.parentElement?.removeChild(element); |
| 40 | + }); |
| 41 | + |
| 42 | + return dom.serialize(); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Generate the RSS Feed File in `./public` so it could be visited by https://domain/rss.xml |
| 47 | + */ |
| 48 | +export const generateRSSFeed = async () => { |
| 49 | + const feed = new Feed({ |
| 50 | + title: Config.SiteTitle, |
| 51 | + description: Config.Sentence, |
| 52 | + id: Config.SiteDomain, |
| 53 | + link: WebsiteURL, |
| 54 | + image: Config.PageCovers.websiteCoverURL, |
| 55 | + favicon: `https://${Config.SiteDomain}/favcion.ico`, |
| 56 | + copyright: CopyrightAnnouncement, |
| 57 | + generator: "Node.js Feed", |
| 58 | + author: { |
| 59 | + name: Config.AuthorName, |
| 60 | + email: Config.SocialLinks.email, |
| 61 | + link: WebsiteURL, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + for (let i = 0; i < Math.min(LatestPostCountInHomePage, sortedPosts.allPostList.length); i++) { |
| 66 | + const post = sortedPosts.allPostList[i]; |
| 67 | + const postFileContent = `${getPostFileContent(post.id)}${NoticeForRSSReaders(post.id)}`; |
| 68 | + const dateNumber = post.frontMatter.time.split("-").map((num: string) => Number.parseInt(num)); |
| 69 | + const mdxSource = await serialize(postFileContent ?? "", { |
| 70 | + parseFrontmatter: true, |
| 71 | + mdxOptions: { |
| 72 | + remarkPlugins: [remarkPrism, externalLinks, remarkMath, remarkGfm], |
| 73 | + rehypePlugins: [rehypeMathJax, rehypeAutolinkHeadings, rehypeSlug, rehypePresetMinify as any, rehypeRaw], |
| 74 | + format: "md", |
| 75 | + }, |
| 76 | + }); |
| 77 | + const htmlContent = minifyHTMLCode(renderToString(<MDXRemote {...mdxSource} />)); |
| 78 | + |
| 79 | + feed.addItem({ |
| 80 | + title: post.frontMatter.title, |
| 81 | + id: post.id, |
| 82 | + link: `https://${Config.SiteDomain}/blog/${post.id}`, |
| 83 | + description: "feedId:68173131511981056+userId:69616630442815488", |
| 84 | + content: htmlContent, |
| 85 | + author: [ |
| 86 | + { |
| 87 | + name: Config.AuthorName, |
| 88 | + email: Config.SocialLinks.email, |
| 89 | + link: `https://${Config.SiteDomain}/about`, |
| 90 | + }, |
| 91 | + ], |
| 92 | + category: post.frontMatter.tags?.map((tagname: string) => ({ |
| 93 | + name: tagname, |
| 94 | + })), |
| 95 | + date: new Date(dateNumber[0], dateNumber[1] - 1, dateNumber[2]), |
| 96 | + image: post.frontMatter.coverURL ?? undefined, |
| 97 | + }); |
| 98 | + } |
| 99 | + fs.writeFile("./public/rss.xml", feed.rss2(), "utf-8", (err) => {}); |
| 100 | +}; |
0 commit comments