Upgrade UI design and improve some functions
This commit is contained in:
22
lib/rss.tsx
22
lib/rss.tsx
@@ -2,6 +2,7 @@ import { CopyrightAnnouncement, LatestPostCountInHomePage, WebsiteURL } from "@/
|
||||
import { Config } from "@/data/config";
|
||||
import { Feed } from "feed";
|
||||
import fs from "fs";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { MDXRemote } from "next-mdx-remote";
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
import { renderToString } from "react-dom/server";
|
||||
@@ -17,6 +18,25 @@ import { getPostFileContent, sortedPosts } from "./post-process";
|
||||
|
||||
const NoticeForRSSReaders = `\n---\n**NOTE:** Different RSS reader may have deficient even no support for svg formulations rendering. If it happens, please read the origin page to have better experience.`;
|
||||
|
||||
function minifyHTMLCode(htmlString: string): string {
|
||||
const dom = new JSDOM(htmlString);
|
||||
const document = dom.window.document;
|
||||
const elements = document.querySelectorAll("*");
|
||||
const unusedElements = document.querySelectorAll("script, style");
|
||||
|
||||
// Remove all class attributes.
|
||||
elements.forEach((element) => {
|
||||
element.removeAttribute("class");
|
||||
});
|
||||
|
||||
// Remove all script and style tags.
|
||||
unusedElements.forEach((element) => {
|
||||
element.parentElement?.removeChild(element);
|
||||
});
|
||||
|
||||
return dom.serialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the RSS Feed File in `./public` so it could be visited by https://domain/rss.xml
|
||||
*/
|
||||
@@ -49,7 +69,7 @@ export const generateRSSFeed = async () => {
|
||||
format: "md",
|
||||
},
|
||||
});
|
||||
const htmlContent = renderToString(<MDXRemote {...mdxSource} />);
|
||||
const htmlContent = minifyHTMLCode(renderToString(<MDXRemote {...mdxSource} />));
|
||||
|
||||
feed.addItem({
|
||||
title: post.frontMatter.title,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { cutForSearch } from "@node-rs/jieba";
|
||||
import Colors from "colors";
|
||||
import minisearch from "minisearch";
|
||||
import sizeof from "object-sizeof";
|
||||
import { getPostFileContent, sortedPosts } from "./post-process";
|
||||
|
||||
// Due to the flaws of the word tokenizer,
|
||||
@@ -30,6 +32,8 @@ function makeSearchIndex() {
|
||||
content: content,
|
||||
});
|
||||
}
|
||||
const sizeofIndex = (sizeof(miniSearch) / 1024 ** 2).toFixed(3);
|
||||
console.log(Colors.cyan(`Search index is ready. And the size of index is ${sizeofIndex} mb`));
|
||||
return miniSearch;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user