diff --git a/README.md b/README.md index 148fad2..3d322c5 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ bun run build - [x] build with github actions - [x] config in sub repo - [ ] nav bar on top +- [ ] date updated - [ ] archive page - [ ] link to previous/next article - [ ] proper docs \ No newline at end of file diff --git a/package.json b/package.json index f0946fb..bc8e4a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "md-blog", - "version": "1.1.0", + "version": "1.2.0", "private": true, "type": "module", "repository": "https://github.com/klemek/md-blog", diff --git a/post-build.ts b/post-build.ts index 95cfe8d..04de414 100644 --- a/post-build.ts +++ b/post-build.ts @@ -28,10 +28,6 @@ function readArticleMetadata(path: string): Record | null { const metadata: Record = { path: path.replaceAll('/index.md', ''), } - const dateMatch = path.match(/(\d{4}\/\d{2}\/\d{2})/) - if (dateMatch && dateMatch[1]) { - metadata['date'] = dateMatch[1].replaceAll('/', '-') - } do { subMatch = METADATA_REGEX.exec(match[1]) if (subMatch && subMatch[1] && subMatch[2]) { @@ -93,7 +89,7 @@ if (!indexContent) { } const metadatas = getFiles('articles') - .filter((path) => path.match(/\d{4}\/\d{2}\/\d{2}\/index.md$/)) + .filter((path) => path.match(/\/index.md$/)) .map((path) => readArticleMetadata(path)) .filter((metadata) => !!metadata) diff --git a/src/lib/articles.ts b/src/lib/articles.ts index fa68503..159c7fe 100644 --- a/src/lib/articles.ts +++ b/src/lib/articles.ts @@ -1,16 +1,14 @@ import type { MarkdownData, Article, ArticleMetadata } from '@interfaces' -import { dateFromParts } from './dates' import katex from 'katex' function parseMetadata( srcAttributes: Record, 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
{ +/** + * @deprecated + */ +export async function loadArticleOld(date: Date): Promise
{ 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
{ 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
{ + 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 { 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) diff --git a/src/lib/dates.ts b/src/lib/dates.ts index 12ae286..f71a3b4 100644 --- a/src/lib/dates.ts +++ b/src/lib/dates.ts @@ -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() + diff --git a/src/router/index.ts b/src/router/index.ts index ab20c2e..d5d3e2b 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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 }, ], }) diff --git a/src/views/ArticleView.vue b/src/views/ArticleView.vue index 063fe91..c3a18d3 100644 --- a/src/views/ArticleView.vue +++ b/src/views/ArticleView.vue @@ -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