:3
This commit is contained in:
parent
c7f546f58a
commit
b62a50b12e
12 changed files with 444 additions and 638 deletions
|
@ -3,6 +3,14 @@ import * as path from 'node:path';
|
|||
import * as crypto from 'node:crypto';
|
||||
import fm, { type FrontMatterResult } from 'front-matter';
|
||||
|
||||
export interface PageLocation {
|
||||
title: string;
|
||||
description: string;
|
||||
tags: string[];
|
||||
map: string;
|
||||
root: string;
|
||||
}
|
||||
|
||||
export interface PageInfoMetdata {
|
||||
title?: string;
|
||||
description?: string;
|
||||
|
@ -24,6 +32,13 @@ export interface PageInfo {
|
|||
path: string;
|
||||
}
|
||||
|
||||
export interface PageInfoCategory {
|
||||
posts: PageInfo[];
|
||||
title: string;
|
||||
description: string;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
// Function to get metadata from a file
|
||||
function getMetadata(filePath: string): PageInfoMetdata {
|
||||
const fileContent = fs.readFileSync(filePath).toString();
|
||||
|
@ -51,17 +66,19 @@ function getWordCount(filePath: string): number {
|
|||
}
|
||||
|
||||
// Function to get pages info
|
||||
export function getPagesInfo(searchDirectory: string, rootDirectory: string): Record<string, PageInfo> {
|
||||
export function getPagesInfo(searchDirectory: string, pageLocation: PageLocation): Record<string, PageInfo> {
|
||||
const pageInfo: Record<string, PageInfo> = {};
|
||||
const currentDirectory = path.join(rootDirectory, searchDirectory);
|
||||
const currentDirectory = path.join(pageLocation.root, searchDirectory);
|
||||
const files = fs.readdirSync(currentDirectory);
|
||||
console.log(files);
|
||||
|
||||
files.forEach((file) => {
|
||||
const fullPath = path.join(currentDirectory, file);
|
||||
const localPath = fullPath.replace(rootDirectory, '');
|
||||
|
||||
const localPath = fullPath.replace(pageLocation.root, pageLocation.map);
|
||||
console.log(fullPath);
|
||||
console.log(localPath);
|
||||
if (fs.lstatSync(fullPath).isDirectory()) {
|
||||
Object.assign(pageInfo, getPagesInfo(path.join(searchDirectory, file), rootDirectory));
|
||||
Object.assign(pageInfo, getPagesInfo(path.join(searchDirectory, file), pageLocation));
|
||||
} else if (file.endsWith('.md')) {
|
||||
const metadata = getMetadata(fullPath);
|
||||
const sha256Hash = getSha256Hash(fullPath);
|
||||
|
|
|
@ -10,13 +10,19 @@ export interface Page {
|
|||
hash: string;
|
||||
}
|
||||
|
||||
export interface PageCategory {
|
||||
posts: Page[];
|
||||
title: string;
|
||||
description: string;
|
||||
tags: string[];
|
||||
}
|
||||
export interface PageList {
|
||||
last_generated: string;
|
||||
posts: Page[];
|
||||
categories: Record<string, PageCategory>;
|
||||
}
|
||||
|
||||
// Function to generate the page list
|
||||
function generatePageList(pagesInfo: Record<string, any>): PageList {
|
||||
function generatePageCategory(pagesInfo: Record<string, any>): PageCategory {
|
||||
const pageList: Page[] = [];
|
||||
|
||||
for (const [path, page] of Object.entries(pagesInfo)) {
|
||||
|
@ -35,16 +41,49 @@ function generatePageList(pagesInfo: Record<string, any>): PageList {
|
|||
}
|
||||
});
|
||||
|
||||
const pageListDict: PageList = {
|
||||
last_generated: format(new Date(), 'yyyy-MM-dd HH:mm:ss'),
|
||||
posts: pageList
|
||||
const pageListDict: PageCategory = {
|
||||
posts: pageList,
|
||||
title: "",
|
||||
description: "",
|
||||
tags: []
|
||||
};
|
||||
|
||||
return pageListDict;
|
||||
}
|
||||
|
||||
// Get the page list and print it
|
||||
const postList = generatePageList(pages.getPagesInfo("", "content/blog"));
|
||||
const postDirectories: pages.PageLocation[] = [
|
||||
{
|
||||
title: "Blog",
|
||||
description: "A collection of blog posts",
|
||||
tags: ["blog"],
|
||||
map: "blog",
|
||||
root: "content/blog"
|
||||
}
|
||||
]
|
||||
|
||||
var postList: PageList = {
|
||||
last_generated: format(new Date(), 'yyyy-MM-dd HH:mm:ss'),
|
||||
categories: {}
|
||||
}
|
||||
|
||||
for (const postDirectory of postDirectories) {
|
||||
const pagesInfo = pages.getPagesInfo("", postDirectory);
|
||||
postList.categories[postDirectory.title] = generatePageCategory(pagesInfo);
|
||||
}
|
||||
|
||||
// Sort the posts by date
|
||||
for (const category of Object.values(postList.categories)) {
|
||||
category.posts.sort((a, b) => {
|
||||
if (!a.metadata.date) {
|
||||
return 1;
|
||||
}
|
||||
if (!b.metadata.date) {
|
||||
return -1;
|
||||
}
|
||||
return new Date(b.metadata.date).getTime() - new Date(a.metadata.date).getTime();
|
||||
});
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(postList, null, 2));
|
||||
|
||||
// Output to assets/blog_list.json (overwriting)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue