Fixed broken app-loading path
X-Lovable-Edit-ID: edt-b04549e8-9883-41b1-ba3f-b23ed2160a89 Co-authored-by: Damaj301damaj-lol <90093996+Damaj301damaj-lol@users.noreply.github.com>
This commit is contained in:
commit
e788d37347
1 changed files with 37 additions and 9 deletions
|
|
@ -1,11 +1,3 @@
|
|||
import matter from 'gray-matter';
|
||||
|
||||
// Make Buffer available for gray-matter in the browser
|
||||
import { Buffer } from 'buffer';
|
||||
if (typeof window !== 'undefined' && !(window as any).Buffer) {
|
||||
(window as any).Buffer = Buffer;
|
||||
}
|
||||
|
||||
export interface PostMeta {
|
||||
slug: string;
|
||||
title: string;
|
||||
|
|
@ -25,11 +17,47 @@ const files = import.meta.glob('../content/blog/*.md', {
|
|||
eager: true,
|
||||
}) as Record<string, string>;
|
||||
|
||||
function parseValue(value: string): string | string[] | boolean {
|
||||
const trimmed = value.trim();
|
||||
|
||||
if (trimmed === 'true') return true;
|
||||
if (trimmed === 'false') return false;
|
||||
if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
|
||||
return trimmed
|
||||
.slice(1, -1)
|
||||
.split(',')
|
||||
.map((item) => item.trim().replace(/^['"]|['"]$/g, ''))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
return trimmed.replace(/^['"]|['"]$/g, '');
|
||||
}
|
||||
|
||||
function parseFrontmatter(raw: string): { data: Partial<PostMeta>; content: string } {
|
||||
if (!raw.startsWith('---')) return { data: {}, content: raw };
|
||||
|
||||
const end = raw.indexOf('\n---', 3);
|
||||
if (end === -1) return { data: {}, content: raw };
|
||||
|
||||
const frontmatter = raw.slice(3, end).trim();
|
||||
const content = raw.slice(end + 4).replace(/^\s*\n/, '');
|
||||
const data = frontmatter.split('\n').reduce<Partial<PostMeta>>((acc, line) => {
|
||||
const separator = line.indexOf(':');
|
||||
if (separator === -1) return acc;
|
||||
|
||||
const key = line.slice(0, separator).trim() as keyof PostMeta;
|
||||
const value = parseValue(line.slice(separator + 1));
|
||||
return { ...acc, [key]: value };
|
||||
}, {});
|
||||
|
||||
return { data, content };
|
||||
}
|
||||
|
||||
function parseAll(): Post[] {
|
||||
return Object.entries(files)
|
||||
.map(([path, raw]) => {
|
||||
const slug = path.split('/').pop()!.replace(/\.md$/, '');
|
||||
const { data, content } = matter(raw);
|
||||
const { data, content } = parseFrontmatter(raw);
|
||||
return {
|
||||
slug,
|
||||
title: data.title ?? slug,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue