From d72eb0af6a50980cd41e5b4d127ff8fe27279765 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Fri, 3 Mar 2023 01:26:25 +0800 Subject: [PATCH 1/2] chore(website): tweaks --- packages/website/.dockerignore | 1 + packages/website/__tests__/capitalize.spec.ts | 12 + packages/website/components/Layout/index.tsx | 4 +- .../components/MarkdownTocBar/tools.ts | 2 + .../website/components/SocialIcon/index.tsx | 6 +- .../website/components/ThemeButton/core.tsx | 6 +- packages/website/package.json | 10 +- packages/website/pages/_document.tsx | 4 +- packages/website/utils/TopUpper.ts | 5 - packages/website/utils/capitalize.ts | 2 + packages/website/utils/hasToc.ts | 8 +- packages/website/utils/isIp.ts | 7 +- packages/website/utils/loadConfig.ts | 19 +- packages/website/utils/theme.ts | 77 +-- packages/website/utils/themeContext.ts | 3 +- packages/website/utils/ua.ts | 10 +- packages/website/utils/washArticles.ts | 58 +- packages/website/yarn.lock | 499 +++++++++++++++++- 18 files changed, 619 insertions(+), 114 deletions(-) create mode 100644 packages/website/__tests__/capitalize.spec.ts delete mode 100644 packages/website/utils/TopUpper.ts create mode 100644 packages/website/utils/capitalize.ts diff --git a/packages/website/.dockerignore b/packages/website/.dockerignore index c5500558b..261454abd 100644 --- a/packages/website/.dockerignore +++ b/packages/website/.dockerignore @@ -1,3 +1,4 @@ +__tests__/ Dockerfile .dockerignore node_modules diff --git a/packages/website/__tests__/capitalize.spec.ts b/packages/website/__tests__/capitalize.spec.ts new file mode 100644 index 000000000..7d9226e6e --- /dev/null +++ b/packages/website/__tests__/capitalize.spec.ts @@ -0,0 +1,12 @@ +import { describe, it, expect } from "vitest"; +import { capitalize } from "../utils/capitalize"; + +describe("capitalize", () => { + it("Should capitalize content", () => { + expect(capitalize("hello")).toBe("Hello"); + }); + + it("Should not throw", () => { + expect(capitalize("")).toBe(""); + }); +}); diff --git a/packages/website/components/Layout/index.tsx b/packages/website/components/Layout/index.tsx index a6c47c505..35dde460d 100644 --- a/packages/website/components/Layout/index.tsx +++ b/packages/website/components/Layout/index.tsx @@ -7,7 +7,7 @@ import GaAnalysis from "../gaAnalysis"; import { LayoutProps } from "../../utils/getLayoutProps"; import ImageProvider from "../ImageProvider"; import { RealThemeType, ThemeContext } from "../../utils/themeContext"; -import { decodeTheme } from "../../utils/theme"; +import { getTheme } from "../../utils/theme"; import CustomLayout from "../CustomLayout"; import { Toaster } from "react-hot-toast"; import Footer from "../Footer"; @@ -24,7 +24,7 @@ export default function (props: { // console.log("script", decode(props.option.customScript as string)); const [isOpen, setIsOpen] = useState(false); const { current } = useRef({ hasInit: false }); - const [theme, setTheme] = useState(decodeTheme("auto")); + const [theme, setTheme] = useState(getTheme("auto")); const handleClose = () => { console.log("关闭或刷新页面"); localStorage.removeItem("saidHello"); diff --git a/packages/website/components/MarkdownTocBar/tools.ts b/packages/website/components/MarkdownTocBar/tools.ts index 5682c7504..e2bb0ca86 100644 --- a/packages/website/components/MarkdownTocBar/tools.ts +++ b/packages/website/components/MarkdownTocBar/tools.ts @@ -4,6 +4,7 @@ export interface NavItem { listNo: string; text: string; } + export const washMarkdownContent = (source: string) => { if (!source) return ""; return ( @@ -19,6 +20,7 @@ export const washMarkdownContent = (source: string) => { .trim() + "\n" ); }; + export const parseNavStructure = (source: string): NavItem[] => { const contentWithoutCode = washMarkdownContent(source); const pattOfTitle = /#+\s(.+)\n/g; diff --git a/packages/website/components/SocialIcon/index.tsx b/packages/website/components/SocialIcon/index.tsx index c2e6c1fce..256be308e 100644 --- a/packages/website/components/SocialIcon/index.tsx +++ b/packages/website/components/SocialIcon/index.tsx @@ -2,7 +2,7 @@ import { useContext, useMemo, useState } from "react"; import { SocialItem } from "../../api/getAllData"; import { getIcon } from "../../utils/getIcon"; import { Popover, ArrowContainer } from "react-tiny-popover"; -import { topUpper } from "../../utils/TopUpper"; +import { capitalize } from "../../utils/capitalize"; import ImageBoxFuture from "../ImageBoxFuture"; import { ThemeContext } from "../../utils/themeContext"; @@ -99,7 +99,7 @@ export default function (props: { item: SocialItem }) { {getIcon(props.item.type, iconSize)} - {topUpper(props.item.type)} + {capitalize(props.item.type)} @@ -119,7 +119,7 @@ export default function (props: { item: SocialItem }) { {getIcon(props.item.type, iconSize)} - {topUpper(props.item.type)} + {capitalize(props.item.type)} ); diff --git a/packages/website/components/ThemeButton/core.tsx b/packages/website/components/ThemeButton/core.tsx index 904186ddd..3faf72ccd 100644 --- a/packages/website/components/ThemeButton/core.tsx +++ b/packages/website/components/ThemeButton/core.tsx @@ -1,5 +1,5 @@ import { useContext, useLayoutEffect, useRef } from "react"; -import { applyTheme, decodeTheme, initTheme } from "../../utils/theme"; +import { applyTheme, getTheme, initTheme } from "../../utils/theme"; import { ThemeContext } from "../../utils/themeContext"; export default function (props: { defaultTheme: "auto" | "dark" | "light" }) { @@ -11,7 +11,7 @@ export default function (props: { defaultTheme: "auto" | "dark" | "light" }) { clearTimer(); localStorage.setItem("theme", newTheme); // 设置真实的主题,然后把真实的主题搞到 state 里。 - const realTheme = decodeTheme(newTheme); + const realTheme = getTheme(newTheme); applyTheme(realTheme, "setTheme", true); setState(realTheme); if (realTheme.includes("auto")) { @@ -25,7 +25,7 @@ export default function (props: { defaultTheme: "auto" | "dark" | "light" }) { const setTimer = () => { clearTimer(); currentTimer.timer = setInterval(() => { - const realTheme = decodeTheme("auto"); + const realTheme = getTheme("auto"); applyTheme(realTheme, "autoThemeTimer", true); }, 10000); }; diff --git a/packages/website/package.json b/packages/website/package.json index 96bdd4889..2dc7f559e 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -1,10 +1,11 @@ { "private": true, "scripts": { - "dev": "cross-env NODE_ENV=development next dev -p 3001 --hostname 0.0.0.0", + "anlysis": "cross-env ANALYZE=true next build", "build": "next build", - "start": "next start", - "anlysis": "cross-env ANALYZE=true next build" + "dev": "cross-env NODE_ENV=development next dev -p 3001 --hostname 0.0.0.0", + "test": "vitest", + "start": "next start" }, "dependencies": { "@next/bundle-analyzer": "^12.2.5", @@ -51,6 +52,7 @@ "cross-env": "^7.0.3", "postcss": "^8.4.14", "tailwindcss": "^3.1.2", - "typescript": "4.7.2" + "typescript": "4.7.2", + "vitest": "0.29.2" } } diff --git a/packages/website/pages/_document.tsx b/packages/website/pages/_document.tsx index 02276a06e..06518f590 100644 --- a/packages/website/pages/_document.tsx +++ b/packages/website/pages/_document.tsx @@ -1,10 +1,10 @@ import { Head, Html, Main, NextScript } from "next/document"; import Script from "next/script"; -import { decodeTheme, initTheme } from "../utils/theme"; +import { getTheme, initTheme } from "../utils/theme"; export default function Document() { return ( - +