refactor: code cleanup

This commit is contained in:
saicaca
2024-02-18 18:13:43 +08:00
committed by saica.go
parent 3cd21c2da9
commit af29b9160f
51 changed files with 872 additions and 2264 deletions

View File

@@ -1,11 +1,10 @@
import I18nKey from '@i18n/i18nKey'
import { i18n } from '@i18n/translation'
import { getCollection } from 'astro:content'
import {UNCATEGORIZED} from "@constants/constants.ts";
import {i18n} from "@i18n/translation.ts";
import I18nKey from "@i18n/i18nKey.ts";
export async function getSortedPosts() {
const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
return import.meta.env.PROD ? data.draft !== true : true
})
const sorted = allBlogPosts.sort((a, b) => {
const dateA = new Date(a.data.published)
@@ -32,7 +31,7 @@ export type Tag = {
export async function getTagList(): Promise<Tag[]> {
const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
return import.meta.env.PROD ? data.draft !== true : true
})
const countMap: { [key: string]: number } = {}
@@ -58,16 +57,18 @@ export type Category = {
export async function getCategoryList(): Promise<Category[]> {
const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
return import.meta.env.PROD ? data.draft !== true : true
})
const count: { [key: string]: number } = {}
allBlogPosts.map(post => {
if (!post.data.category) {
const ucKey = i18n(I18nKey.uncategorized);
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
count[post.data.category] = count[post.data.category]
? count[post.data.category] + 1
: 1
})
const lst = Object.keys(count).sort((a, b) => {

View File

@@ -0,0 +1,19 @@
export function getDefaultHue(): number {
const fallback = '250'
const configCarrier = document.getElementById('config-carrier')
return parseInt(configCarrier?.dataset.hue || fallback)
}
export function getHue(): number {
const stored = localStorage.getItem('hue')
return stored ? parseInt(stored) : getDefaultHue()
}
export function setHue(hue: number): void {
localStorage.setItem('hue', String(hue))
const r = document.querySelector(':root')
if (!r) {
return
}
r.style.setProperty('--hue', hue)
}

View File

@@ -1,5 +1,5 @@
import {i18n} from "@i18n/translation.ts";
import i18nKey from "@i18n/i18nKey.ts";
import i18nKey from '@i18n/i18nKey'
import { i18n } from '@i18n/translation'
export function pathsEqual(path1: string, path2: string) {
const normalizedPath1 = path1.replace(/^\/|\/$/g, '').toLowerCase()
@@ -18,17 +18,16 @@ export function getPostUrlBySlug(slug: string): string | null {
}
export function getCategoryUrl(category: string): string | null {
if (!category)
return null
if (!category) return null
if (category === i18n(i18nKey.uncategorized))
return '/archive/category/uncategorized'
return '/archive/category/uncategorized'
return `/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)
}