feat: do not use path as date

This commit is contained in:
2026-04-26 15:10:30 +02:00
parent af9b20485e
commit 34c410b687
7 changed files with 29 additions and 34 deletions
+23 -11
View File
@@ -1,16 +1,14 @@
import type { MarkdownData, Article, ArticleMetadata } from '@interfaces'
import { dateFromParts } from './dates'
import katex from 'katex'
function parseMetadata(
srcAttributes: Record<string, unknown>,
pathPrefix: string,
date: Date,
): ArticleMetadata {
return {
path: pathPrefix,
title: decodeURIComponent((srcAttributes.title as string) ?? 'Untitled'),
date: date,
date: new Date(Date.parse((srcAttributes.date as string) ?? '')),
author: decodeURIComponent((srcAttributes.author as string) ?? ''),
thumbnail: (srcAttributes.thumbnail as string) ?? '',
draft: !!srcAttributes.draft,
@@ -55,7 +53,10 @@ function transformHtml(srcHtml: string): string {
return outHtml
}
export async function loadArticle(date: Date): Promise<Article | null> {
/**
* @deprecated
*/
export async function loadArticleOld(date: Date): Promise<Article | null> {
const year = date.getFullYear().toString()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
@@ -63,7 +64,23 @@ export async function loadArticle(date: Date): Promise<Article | null> {
try {
const data = (await import(`@articles/${year}/${month}/${day}/index.md`)) as MarkdownData
return {
metadata: parseMetadata(data.attributes, path, date),
metadata: parseMetadata(data.attributes, path),
html: transformHtml(data.html),
}
} catch (ex) {
console.error(ex)
return null
}
}
export async function loadArticle(path: string): Promise<Article | null> {
const raw_articles = import.meta.glob('@articles/**/index.md')
const key = `/articles/${path}index.md`
if (!raw_articles[key]) return null
try {
const data = (await raw_articles[key]()) as MarkdownData
return {
metadata: parseMetadata(data.attributes, path),
html: transformHtml(data.html),
}
} catch (ex) {
@@ -79,12 +96,7 @@ export async function listArticles(): Promise<ArticleMetadata[]> {
Object.keys(raw_articles).map(async (key) => {
if (!raw_articles[key]) return null
const data = (await raw_articles[key]()) as MarkdownData
const match = key.match(/\/(\d+)\/(\d+)\/(\d+)\//)
if (match === null) {
return null
}
const date = dateFromParts(match[1], match[2], match[3])
return parseMetadata(data.attributes, key.replace('index.md', ''), date)
return parseMetadata(data.attributes, key.replace('index.md', ''))
}),
)
).filter((item) => item !== null)
-8
View File
@@ -1,11 +1,3 @@
export function dateFromParts(
year: string | undefined,
month: string | undefined,
day: string | undefined,
): Date {
return new Date(parseInt(year ?? ''), parseInt(month ?? '') - 1, parseInt(day ?? ''))
}
export function simpleDateFormat(date: Date): string {
return (
date.getFullYear() +
+1 -1
View File
@@ -7,7 +7,7 @@ const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: HomeView },
{ path: '/articles/:year(\\d{4})/:month(\\d{2})/:day(\\d{2})/', component: ArticleView },
{ path: '/articles/:pathMatch(.*)/', component: ArticleView },
{ path: '/:pathMatch(.*)', component: NotFoundView },
],
})
+2 -8
View File
@@ -4,7 +4,7 @@ import { ref, onBeforeMount } from 'vue'
import { loadArticle } from '@lib/articles'
import { useRoute, onBeforeRouteUpdate, type RouteLocation } from 'vue-router'
import NotFoundView from './NotFoundView.vue'
import { dateFromParts, simpleDateFormat } from '@lib/dates'
import { simpleDateFormat } from '@lib/dates'
import { SIGNATURE, TITLE } from '@lib/meta'
import PageFooter from '@components/PageFooter.vue'
import { stripHTML } from '@/lib/strings'
@@ -15,13 +15,7 @@ const route = useRoute()
async function loadPage(target: RouteLocation) {
loading.value = true
article.value = await loadArticle(
dateFromParts(
target.params.year as string,
target.params.month as string,
target.params.day as string,
),
)
article.value = await loadArticle(target.params.pathMatch as string)
window.document.title =
stripHTML(TITLE) + ' — ' + stripHTML(article.value?.metadata.title ?? 'Not Found')
loading.value = false