feat: do not use path as date
This commit is contained in:
@@ -20,6 +20,7 @@ bun run build
|
|||||||
- [x] build with github actions
|
- [x] build with github actions
|
||||||
- [x] config in sub repo
|
- [x] config in sub repo
|
||||||
- [ ] nav bar on top
|
- [ ] nav bar on top
|
||||||
|
- [ ] date updated
|
||||||
- [ ] archive page
|
- [ ] archive page
|
||||||
- [ ] link to previous/next article
|
- [ ] link to previous/next article
|
||||||
- [ ] proper docs
|
- [ ] proper docs
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "md-blog",
|
"name": "md-blog",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"repository": "https://github.com/klemek/md-blog",
|
"repository": "https://github.com/klemek/md-blog",
|
||||||
|
|||||||
+1
-5
@@ -28,10 +28,6 @@ function readArticleMetadata(path: string): Record<string, string> | null {
|
|||||||
const metadata: Record<string, string> = {
|
const metadata: Record<string, string> = {
|
||||||
path: path.replaceAll('/index.md', ''),
|
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 {
|
do {
|
||||||
subMatch = METADATA_REGEX.exec(match[1])
|
subMatch = METADATA_REGEX.exec(match[1])
|
||||||
if (subMatch && subMatch[1] && subMatch[2]) {
|
if (subMatch && subMatch[1] && subMatch[2]) {
|
||||||
@@ -93,7 +89,7 @@ if (!indexContent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const metadatas = getFiles('articles')
|
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))
|
.map((path) => readArticleMetadata(path))
|
||||||
.filter((metadata) => !!metadata)
|
.filter((metadata) => !!metadata)
|
||||||
|
|
||||||
|
|||||||
+23
-11
@@ -1,16 +1,14 @@
|
|||||||
import type { MarkdownData, Article, ArticleMetadata } from '@interfaces'
|
import type { MarkdownData, Article, ArticleMetadata } from '@interfaces'
|
||||||
import { dateFromParts } from './dates'
|
|
||||||
import katex from 'katex'
|
import katex from 'katex'
|
||||||
|
|
||||||
function parseMetadata(
|
function parseMetadata(
|
||||||
srcAttributes: Record<string, unknown>,
|
srcAttributes: Record<string, unknown>,
|
||||||
pathPrefix: string,
|
pathPrefix: string,
|
||||||
date: Date,
|
|
||||||
): ArticleMetadata {
|
): ArticleMetadata {
|
||||||
return {
|
return {
|
||||||
path: pathPrefix,
|
path: pathPrefix,
|
||||||
title: decodeURIComponent((srcAttributes.title as string) ?? 'Untitled'),
|
title: decodeURIComponent((srcAttributes.title as string) ?? 'Untitled'),
|
||||||
date: date,
|
date: new Date(Date.parse((srcAttributes.date as string) ?? '')),
|
||||||
author: decodeURIComponent((srcAttributes.author as string) ?? ''),
|
author: decodeURIComponent((srcAttributes.author as string) ?? ''),
|
||||||
thumbnail: (srcAttributes.thumbnail as string) ?? '',
|
thumbnail: (srcAttributes.thumbnail as string) ?? '',
|
||||||
draft: !!srcAttributes.draft,
|
draft: !!srcAttributes.draft,
|
||||||
@@ -55,7 +53,10 @@ function transformHtml(srcHtml: string): string {
|
|||||||
return outHtml
|
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 year = date.getFullYear().toString()
|
||||||
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
||||||
const day = date.getDate().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 {
|
try {
|
||||||
const data = (await import(`@articles/${year}/${month}/${day}/index.md`)) as MarkdownData
|
const data = (await import(`@articles/${year}/${month}/${day}/index.md`)) as MarkdownData
|
||||||
return {
|
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),
|
html: transformHtml(data.html),
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@@ -79,12 +96,7 @@ export async function listArticles(): Promise<ArticleMetadata[]> {
|
|||||||
Object.keys(raw_articles).map(async (key) => {
|
Object.keys(raw_articles).map(async (key) => {
|
||||||
if (!raw_articles[key]) return null
|
if (!raw_articles[key]) return null
|
||||||
const data = (await raw_articles[key]()) as MarkdownData
|
const data = (await raw_articles[key]()) as MarkdownData
|
||||||
const match = key.match(/\/(\d+)\/(\d+)\/(\d+)\//)
|
return parseMetadata(data.attributes, key.replace('index.md', ''))
|
||||||
if (match === null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
const date = dateFromParts(match[1], match[2], match[3])
|
|
||||||
return parseMetadata(data.attributes, key.replace('index.md', ''), date)
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
).filter((item) => item !== null)
|
).filter((item) => item !== null)
|
||||||
|
|||||||
@@ -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 {
|
export function simpleDateFormat(date: Date): string {
|
||||||
return (
|
return (
|
||||||
date.getFullYear() +
|
date.getFullYear() +
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ const router = createRouter({
|
|||||||
history: createWebHistory(),
|
history: createWebHistory(),
|
||||||
routes: [
|
routes: [
|
||||||
{ path: '/', component: HomeView },
|
{ 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 },
|
{ path: '/:pathMatch(.*)', component: NotFoundView },
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { ref, onBeforeMount } from 'vue'
|
|||||||
import { loadArticle } from '@lib/articles'
|
import { loadArticle } from '@lib/articles'
|
||||||
import { useRoute, onBeforeRouteUpdate, type RouteLocation } from 'vue-router'
|
import { useRoute, onBeforeRouteUpdate, type RouteLocation } from 'vue-router'
|
||||||
import NotFoundView from './NotFoundView.vue'
|
import NotFoundView from './NotFoundView.vue'
|
||||||
import { dateFromParts, simpleDateFormat } from '@lib/dates'
|
import { simpleDateFormat } from '@lib/dates'
|
||||||
import { SIGNATURE, TITLE } from '@lib/meta'
|
import { SIGNATURE, TITLE } from '@lib/meta'
|
||||||
import PageFooter from '@components/PageFooter.vue'
|
import PageFooter from '@components/PageFooter.vue'
|
||||||
import { stripHTML } from '@/lib/strings'
|
import { stripHTML } from '@/lib/strings'
|
||||||
@@ -15,13 +15,7 @@ const route = useRoute()
|
|||||||
|
|
||||||
async function loadPage(target: RouteLocation) {
|
async function loadPage(target: RouteLocation) {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
article.value = await loadArticle(
|
article.value = await loadArticle(target.params.pathMatch as string)
|
||||||
dateFromParts(
|
|
||||||
target.params.year as string,
|
|
||||||
target.params.month as string,
|
|
||||||
target.params.day as string,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
window.document.title =
|
window.document.title =
|
||||||
stripHTML(TITLE) + ' — ' + stripHTML(article.value?.metadata.title ?? 'Not Found')
|
stripHTML(TITLE) + ' — ' + stripHTML(article.value?.metadata.title ?? 'Not Found')
|
||||||
loading.value = false
|
loading.value = false
|
||||||
|
|||||||
Reference in New Issue
Block a user