-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
621 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
__tests__/ | ||
Dockerfile | ||
.dockerignore | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const capitalize = (content: string): string => | ||
content.substring(0, 1).toLocaleUpperCase() + content.substring(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { washMarkdownContent } from "../components/MarkdownTocBar/tools"; | ||
|
||
export function hasToc(content: string) { | ||
const r = /#+\s+/; | ||
return r.test(washMarkdownContent(content)); | ||
} | ||
const HASH_REG = /#+\s+/; | ||
|
||
export const hasToc = (content: string) => | ||
HASH_REG.test(washMarkdownContent(content)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export const isIp = (str: string) => { | ||
const re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | ||
return re.test(str); | ||
}; | ||
const IP_REGEXP = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | ||
|
||
export const isIP = (maybeIP: string): boolean => IP_REGEXP.test(maybeIP); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
const normalizeURL = (url: string) => new URL(url).toString(); | ||
|
||
// 从环境变量中读取. | ||
export const config = { | ||
baseUrl: washUrl(process.env.VAN_BLOG_SERVER_URL ?? "http://localhost:3000"), | ||
baseUrl: normalizeURL( | ||
process.env.VAN_BLOG_SERVER_URL ?? "http://localhost:3000" | ||
), | ||
}; | ||
|
||
function washUrl(url: string) { | ||
// 带反斜杠的 | ||
const u = new URL(url); | ||
return u.toString(); | ||
} | ||
|
||
// 改为服务端触发 isr | ||
// export const revalidate = {}; | ||
export const revalidate = process.env.VAN_BLOG_REVALIDATE == 'true' | ||
? { revalidate: parseInt(process.env.VAN_BLOG_REVALIDATE_TIME || "10") } | ||
: {}; | ||
export const revalidate = | ||
process.env.VAN_BLOG_REVALIDATE == "true" | ||
? { revalidate: parseInt(process.env.VAN_BLOG_REVALIDATE_TIME || "10") } | ||
: {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
export function decodeAuto() { | ||
const d = new Date().getHours(); | ||
const night = d > 18 || d < 8; | ||
if (typeof window == "undefined") { | ||
if (night) { | ||
return "auto-dark"; | ||
} else { | ||
return "auto-light"; | ||
} | ||
export const initTheme = () => { | ||
if (typeof localStorage === "undefined") { | ||
return "auto"; | ||
} | ||
if (night || window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
return "auto-dark"; | ||
} else { | ||
return "auto-light"; | ||
|
||
// 2种情况: 1. 自动。 2.手动 | ||
if (!("theme" in localStorage) || localStorage.theme == "auto") { | ||
return "auto"; | ||
} | ||
} | ||
export const decodeTheme = (t: "auto" | "light" | "dark") => { | ||
if (t == "auto") { | ||
return decodeAuto(); | ||
} else { | ||
return t; | ||
|
||
if (localStorage.theme === "dark") { | ||
return "dark"; | ||
} | ||
|
||
return "light"; | ||
}; | ||
export const applyTheme = (t: string, source: string, disableLog: boolean) => { | ||
if (t.includes("light")) { | ||
|
||
export const getAutoTheme = () => { | ||
const hour = new Date().getHours(); | ||
const isNight = hour > 18 || hour < 8; | ||
|
||
if (typeof window === "undefined") { | ||
return isNight ? "auto-dark" : "auto-light"; | ||
} | ||
|
||
if (isNight || window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
return "auto-dark"; | ||
} | ||
|
||
return "auto-light"; | ||
}; | ||
|
||
export const getTheme = (theme: "auto" | "light" | "dark") => | ||
theme == "auto" ? getAutoTheme() : theme; | ||
|
||
export const applyTheme = ( | ||
theme: string, | ||
source: string, | ||
disableLog = false | ||
) => { | ||
if (theme.includes("light")) { | ||
document.documentElement.classList.add("light"); | ||
document.documentElement.classList.remove("dark"); | ||
if (!disableLog) { | ||
console.log(`[Apply Theme][${source}] ${t}`); | ||
console.log(`[Apply Theme][${source}] ${theme}`); | ||
} | ||
} else { | ||
document.documentElement.classList.add("dark"); | ||
document.documentElement.classList.remove("light"); | ||
if (!disableLog) { | ||
console.log(`[Apply Theme][${source}] ${t}`); | ||
} | ||
} | ||
}; | ||
export const initTheme = () => { | ||
if (typeof localStorage == "undefined") { | ||
return "auto"; | ||
} | ||
// 2种情况: 1. 自动。 2.手动 | ||
if (!("theme" in localStorage) || localStorage.theme == "auto") { | ||
return "auto"; | ||
} else { | ||
if (localStorage.theme === "dark") { | ||
return "dark"; | ||
} else { | ||
return "light"; | ||
console.log(`[Apply Theme][${source}] ${theme}`); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import React from "react"; | ||
|
||
export type RealThemeType = "auto-dark" | "auto-light" | "dark" | "light"; | ||
|
||
export const ThemeContext = React.createContext<{ | ||
theme: RealThemeType; | ||
setTheme: (newState: RealThemeType) => void; | ||
}>({ | ||
theme: "auto-light", | ||
setTheme: (newState: RealThemeType) => {}, | ||
setTheme: () => {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
export const isMac = () => { | ||
try { | ||
return navigator.userAgent.toUpperCase().indexOf('MAC') >= 0; | ||
} catch(err) { | ||
return false; | ||
} | ||
} | ||
export const isMac = (): boolean => | ||
typeof navigator !== "undefined" && | ||
Boolean(navigator.userAgent?.toLowerCase().includes("mac")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
export function washArticlesByKey( | ||
export const washArticlesByKey = ( | ||
rawArticles: any[], | ||
getValueFn: (val: any) => any, | ||
isKeyArray: boolean | ||
) { | ||
) => { | ||
const articles = {} as any; | ||
let dates = []; | ||
if (isKeyArray) { | ||
dates = Array.from(new Set(rawArticles.flatMap((a) => getValueFn(a)))); | ||
} else { | ||
dates = Array.from(new Set(rawArticles.map((a) => getValueFn(a)))); | ||
} | ||
|
||
const dates = Array.from( | ||
new Set( | ||
isKeyArray | ||
? rawArticles.flatMap((a) => getValueFn(a)) | ||
: rawArticles.map((a) => getValueFn(a)) | ||
) | ||
); | ||
|
||
for (const date of dates) { | ||
let curArticles = rawArticles | ||
.filter((each) => { | ||
if (isKeyArray) { | ||
return getValueFn(each).includes(date); | ||
} else { | ||
return getValueFn(each) == date; | ||
} | ||
}) | ||
.map((each) => { | ||
return { | ||
title: each.title, | ||
id: each.id, | ||
createdAt: each.createdAt, | ||
updatedAt: each.updatedAt, | ||
}; | ||
}); | ||
curArticles = curArticles.sort( | ||
(a, b) => | ||
new Date(b.createdAt).valueOf() - new Date(a.createdAt).valueOf() | ||
); | ||
const curArticles = rawArticles | ||
.filter((each) => | ||
isKeyArray ? getValueFn(each).includes(date) : getValueFn(each) == date | ||
) | ||
.map((each) => ({ | ||
title: each.title, | ||
id: each.id, | ||
createdAt: each.createdAt, | ||
updatedAt: each.updatedAt, | ||
})) | ||
.sort( | ||
(prev, next) => | ||
new Date(next.createdAt).getTime() - | ||
new Date(prev.createdAt).getTime() | ||
); | ||
|
||
articles[String(date)] = curArticles; | ||
} | ||
|
||
return articles; | ||
} | ||
}; |
Oops, something went wrong.