mirror of
https://github.com/saicaca/fuwari.git
synced 2026-01-12 07:12:52 +01:00
format all code (#386)
This commit is contained in:
committed by
GitHub
parent
7ea2f7f40f
commit
286b050fa8
@@ -1,86 +1,84 @@
|
||||
import { getCollection } from 'astro:content'
|
||||
import I18nKey from '@i18n/i18nKey'
|
||||
import { i18n } from '@i18n/translation'
|
||||
import { getCollection } from "astro:content";
|
||||
import I18nKey from "@i18n/i18nKey";
|
||||
import { i18n } from "@i18n/translation";
|
||||
|
||||
export async function getSortedPosts() {
|
||||
const allBlogPosts = (await getCollection('posts', ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true
|
||||
}))
|
||||
const allBlogPosts = await getCollection("posts", ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true;
|
||||
});
|
||||
|
||||
const sorted = allBlogPosts.sort(
|
||||
(a, b) => {
|
||||
const dateA = new Date(a.data.published)
|
||||
const dateB = new Date(b.data.published)
|
||||
return dateA > dateB ? -1 : 1
|
||||
},
|
||||
)
|
||||
const sorted = allBlogPosts.sort((a, b) => {
|
||||
const dateA = new Date(a.data.published);
|
||||
const dateB = new Date(b.data.published);
|
||||
return dateA > dateB ? -1 : 1;
|
||||
});
|
||||
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
sorted[i].data.nextSlug = sorted[i - 1].slug
|
||||
sorted[i].data.nextTitle = sorted[i - 1].data.title
|
||||
}
|
||||
for (let i = 0; i < sorted.length - 1; i++) {
|
||||
sorted[i].data.prevSlug = sorted[i + 1].slug
|
||||
sorted[i].data.prevTitle = sorted[i + 1].data.title
|
||||
}
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
sorted[i].data.nextSlug = sorted[i - 1].slug;
|
||||
sorted[i].data.nextTitle = sorted[i - 1].data.title;
|
||||
}
|
||||
for (let i = 0; i < sorted.length - 1; i++) {
|
||||
sorted[i].data.prevSlug = sorted[i + 1].slug;
|
||||
sorted[i].data.prevTitle = sorted[i + 1].data.title;
|
||||
}
|
||||
|
||||
return sorted
|
||||
return sorted;
|
||||
}
|
||||
|
||||
export type Tag = {
|
||||
name: string
|
||||
count: number
|
||||
}
|
||||
name: string;
|
||||
count: number;
|
||||
};
|
||||
|
||||
export async function getTagList(): Promise<Tag[]> {
|
||||
const allBlogPosts = await getCollection<'posts'>('posts', ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true
|
||||
})
|
||||
const allBlogPosts = await getCollection<"posts">("posts", ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true;
|
||||
});
|
||||
|
||||
const countMap: { [key: string]: number } = {}
|
||||
allBlogPosts.map((post: { data: { tags: string[] } }) => {
|
||||
post.data.tags.map((tag: string) => {
|
||||
if (!countMap[tag]) countMap[tag] = 0
|
||||
countMap[tag]++
|
||||
})
|
||||
})
|
||||
const countMap: { [key: string]: number } = {};
|
||||
allBlogPosts.map((post: { data: { tags: string[] } }) => {
|
||||
post.data.tags.map((tag: string) => {
|
||||
if (!countMap[tag]) countMap[tag] = 0;
|
||||
countMap[tag]++;
|
||||
});
|
||||
});
|
||||
|
||||
// sort tags
|
||||
const keys: string[] = Object.keys(countMap).sort((a, b) => {
|
||||
return a.toLowerCase().localeCompare(b.toLowerCase())
|
||||
})
|
||||
// sort tags
|
||||
const keys: string[] = Object.keys(countMap).sort((a, b) => {
|
||||
return a.toLowerCase().localeCompare(b.toLowerCase());
|
||||
});
|
||||
|
||||
return keys.map(key => ({ name: key, count: countMap[key] }))
|
||||
return keys.map((key) => ({ name: key, count: countMap[key] }));
|
||||
}
|
||||
|
||||
export type Category = {
|
||||
name: string
|
||||
count: number
|
||||
}
|
||||
name: string;
|
||||
count: number;
|
||||
};
|
||||
|
||||
export async function getCategoryList(): Promise<Category[]> {
|
||||
const allBlogPosts = await getCollection<'posts'>('posts', ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true
|
||||
})
|
||||
const count: { [key: string]: number } = {}
|
||||
allBlogPosts.map((post: { data: { category: string | number } }) => {
|
||||
if (!post.data.category) {
|
||||
const ucKey = i18n(I18nKey.uncategorized)
|
||||
count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1
|
||||
return
|
||||
}
|
||||
count[post.data.category] = count[post.data.category]
|
||||
? count[post.data.category] + 1
|
||||
: 1
|
||||
})
|
||||
const allBlogPosts = await getCollection<"posts">("posts", ({ data }) => {
|
||||
return import.meta.env.PROD ? data.draft !== true : true;
|
||||
});
|
||||
const count: { [key: string]: number } = {};
|
||||
allBlogPosts.map((post: { data: { category: string | number } }) => {
|
||||
if (!post.data.category) {
|
||||
const ucKey = i18n(I18nKey.uncategorized);
|
||||
count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1;
|
||||
return;
|
||||
}
|
||||
count[post.data.category] = count[post.data.category]
|
||||
? count[post.data.category] + 1
|
||||
: 1;
|
||||
});
|
||||
|
||||
const lst = Object.keys(count).sort((a, b) => {
|
||||
return a.toLowerCase().localeCompare(b.toLowerCase())
|
||||
})
|
||||
const lst = Object.keys(count).sort((a, b) => {
|
||||
return a.toLowerCase().localeCompare(b.toLowerCase());
|
||||
});
|
||||
|
||||
const ret: Category[] = []
|
||||
for (const c of lst) {
|
||||
ret.push({ name: c, count: count[c] })
|
||||
}
|
||||
return ret
|
||||
const ret: Category[] = [];
|
||||
for (const c of lst) {
|
||||
ret.push({ name: c, count: count[c] });
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export function formatDateToYYYYMMDD(date: Date): string {
|
||||
return date.toISOString().substring(0, 10)
|
||||
return date.toISOString().substring(0, 10);
|
||||
}
|
||||
|
||||
@@ -1,54 +1,54 @@
|
||||
import type { LIGHT_DARK_MODE } from '@/types/config'
|
||||
import type { LIGHT_DARK_MODE } from "@/types/config";
|
||||
import {
|
||||
AUTO_MODE,
|
||||
DARK_MODE,
|
||||
DEFAULT_THEME,
|
||||
LIGHT_MODE,
|
||||
} from '@constants/constants.ts'
|
||||
AUTO_MODE,
|
||||
DARK_MODE,
|
||||
DEFAULT_THEME,
|
||||
LIGHT_MODE,
|
||||
} from "@constants/constants.ts";
|
||||
|
||||
export function getDefaultHue(): number {
|
||||
const fallback = '250'
|
||||
const configCarrier = document.getElementById('config-carrier')
|
||||
return Number.parseInt(configCarrier?.dataset.hue || fallback)
|
||||
const fallback = "250";
|
||||
const configCarrier = document.getElementById("config-carrier");
|
||||
return Number.parseInt(configCarrier?.dataset.hue || fallback);
|
||||
}
|
||||
|
||||
export function getHue(): number {
|
||||
const stored = localStorage.getItem('hue')
|
||||
return stored ? Number.parseInt(stored) : getDefaultHue()
|
||||
const stored = localStorage.getItem("hue");
|
||||
return stored ? Number.parseInt(stored) : getDefaultHue();
|
||||
}
|
||||
|
||||
export function setHue(hue: number): void {
|
||||
localStorage.setItem('hue', String(hue))
|
||||
const r = document.querySelector(':root') as HTMLElement
|
||||
if (!r) {
|
||||
return
|
||||
}
|
||||
r.style.setProperty('--hue', String(hue))
|
||||
localStorage.setItem("hue", String(hue));
|
||||
const r = document.querySelector(":root") as HTMLElement;
|
||||
if (!r) {
|
||||
return;
|
||||
}
|
||||
r.style.setProperty("--hue", String(hue));
|
||||
}
|
||||
|
||||
export function applyThemeToDocument(theme: LIGHT_DARK_MODE) {
|
||||
switch (theme) {
|
||||
case LIGHT_MODE:
|
||||
document.documentElement.classList.remove('dark')
|
||||
break
|
||||
case DARK_MODE:
|
||||
document.documentElement.classList.add('dark')
|
||||
break
|
||||
case AUTO_MODE:
|
||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
document.documentElement.classList.add('dark')
|
||||
} else {
|
||||
document.documentElement.classList.remove('dark')
|
||||
}
|
||||
break
|
||||
}
|
||||
switch (theme) {
|
||||
case LIGHT_MODE:
|
||||
document.documentElement.classList.remove("dark");
|
||||
break;
|
||||
case DARK_MODE:
|
||||
document.documentElement.classList.add("dark");
|
||||
break;
|
||||
case AUTO_MODE:
|
||||
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export function setTheme(theme: LIGHT_DARK_MODE): void {
|
||||
localStorage.setItem('theme', theme)
|
||||
applyThemeToDocument(theme)
|
||||
localStorage.setItem("theme", theme);
|
||||
applyThemeToDocument(theme);
|
||||
}
|
||||
|
||||
export function getStoredTheme(): LIGHT_DARK_MODE {
|
||||
return (localStorage.getItem('theme') as LIGHT_DARK_MODE) || DEFAULT_THEME
|
||||
return (localStorage.getItem("theme") as LIGHT_DARK_MODE) || DEFAULT_THEME;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
import i18nKey from '@i18n/i18nKey'
|
||||
import { i18n } from '@i18n/translation'
|
||||
import i18nKey from "@i18n/i18nKey";
|
||||
import { i18n } from "@i18n/translation";
|
||||
|
||||
export function pathsEqual(path1: string, path2: string) {
|
||||
const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase()
|
||||
const normalizedPath2 = path2.replace(/^\/|\/$/g, '').toLowerCase()
|
||||
return normalizedPath1 === normalizedPath2
|
||||
const normalizedPath1 = path1.replace(/^\/|\/$/g, "").toLowerCase();
|
||||
const normalizedPath2 = path2.replace(/^\/|\/$/g, "").toLowerCase();
|
||||
return normalizedPath1 === normalizedPath2;
|
||||
}
|
||||
|
||||
function joinUrl(...parts: string[]): string {
|
||||
const joined = parts.join('/')
|
||||
return joined.replace(/\/+/g, '/')
|
||||
const joined = parts.join("/");
|
||||
return joined.replace(/\/+/g, "/");
|
||||
}
|
||||
|
||||
export function getPostUrlBySlug(slug: string): string {
|
||||
return url(`/posts/${slug}/`)
|
||||
return url(`/posts/${slug}/`);
|
||||
}
|
||||
|
||||
export function getCategoryUrl(category: string): string {
|
||||
if (category === i18n(i18nKey.uncategorized))
|
||||
return url('/archive/category/uncategorized/')
|
||||
return url(`/archive/category/${category}/`)
|
||||
if (category === i18n(i18nKey.uncategorized))
|
||||
return url("/archive/category/uncategorized/");
|
||||
return url(`/archive/category/${category}/`);
|
||||
}
|
||||
|
||||
export function getDir(path: string): string {
|
||||
const lastSlashIndex = path.lastIndexOf('/')
|
||||
if (lastSlashIndex < 0) {
|
||||
return '/'
|
||||
}
|
||||
return path.substring(0, lastSlashIndex + 1)
|
||||
const lastSlashIndex = path.lastIndexOf("/");
|
||||
if (lastSlashIndex < 0) {
|
||||
return "/";
|
||||
}
|
||||
return path.substring(0, lastSlashIndex + 1);
|
||||
}
|
||||
|
||||
export function url(path: string) {
|
||||
return joinUrl('', import.meta.env.BASE_URL, path)
|
||||
return joinUrl("", import.meta.env.BASE_URL, path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user