:3
This commit is contained in:
parent
328a6bd178
commit
1dbf1e4074
21 changed files with 1046 additions and 545 deletions
|
@ -115,5 +115,5 @@ except FileNotFoundError:
|
|||
post_history = generate_post_history(pages_info, state)
|
||||
|
||||
# Output to assets/post_history.json (overwriting)
|
||||
with open("assets/post_history.json", "w") as f:
|
||||
with open("assets/meta/post_history.json", "w") as f:
|
||||
f.write(post_history)
|
84
utils/pageupdater/pages.ts
Normal file
84
utils/pageupdater/pages.ts
Normal file
|
@ -0,0 +1,84 @@
|
|||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as crypto from 'node:crypto';
|
||||
import fm, { type FrontMatterResult } from 'front-matter';
|
||||
|
||||
export interface PageInfoMetdata {
|
||||
title?: string;
|
||||
description?: string;
|
||||
date?: string;
|
||||
tags?: string[];
|
||||
background?: string;
|
||||
next?: string ;
|
||||
previous?: string;
|
||||
}
|
||||
|
||||
// Type for metadata and page info
|
||||
export interface PageInfo {
|
||||
local_path: string;
|
||||
absolute_path: string;
|
||||
metadata: PageInfoMetdata;
|
||||
hash: string;
|
||||
char_count: number;
|
||||
word_count: number;
|
||||
path: string;
|
||||
}
|
||||
|
||||
// Function to get metadata from a file
|
||||
function getMetadata(filePath: string): PageInfoMetdata {
|
||||
const fileContent = fs.readFileSync(filePath).toString();
|
||||
return fm(fileContent).attributes as PageInfoMetdata
|
||||
}
|
||||
|
||||
// Function to get SHA-256 hash of a file
|
||||
function getSha256Hash(filePath: string): string {
|
||||
const fileContent = fs.readFileSync(filePath);
|
||||
const hash = crypto.createHash('sha256');
|
||||
hash.update(fileContent.toString());
|
||||
return hash.digest('hex');
|
||||
}
|
||||
|
||||
// Function to get character count of a file
|
||||
function getCharCount(filePath: string): number {
|
||||
const fileContent = fs.readFileSync(filePath, 'utf8');
|
||||
return fileContent.length;
|
||||
}
|
||||
|
||||
// Function to get word count of a file
|
||||
function getWordCount(filePath: string): number {
|
||||
const fileContent = fs.readFileSync(filePath, 'utf8');
|
||||
return fileContent.split(/\s+/).length;
|
||||
}
|
||||
|
||||
// Function to get pages info
|
||||
export function getPagesInfo(searchDirectory: string, rootDirectory: string): Record<string, PageInfo> {
|
||||
const pageInfo: Record<string, PageInfo> = {};
|
||||
const currentDirectory = path.join(rootDirectory, searchDirectory);
|
||||
const files = fs.readdirSync(currentDirectory);
|
||||
|
||||
files.forEach((file) => {
|
||||
const fullPath = path.join(currentDirectory, file);
|
||||
const localPath = fullPath.replace(rootDirectory, '');
|
||||
|
||||
if (fs.lstatSync(fullPath).isDirectory()) {
|
||||
Object.assign(pageInfo, getPagesInfo(path.join(searchDirectory, file), rootDirectory));
|
||||
} else if (file.endsWith('.md')) {
|
||||
const metadata = getMetadata(fullPath);
|
||||
const sha256Hash = getSha256Hash(fullPath);
|
||||
const charCount = getCharCount(fullPath);
|
||||
const wordCount = getWordCount(fullPath);
|
||||
|
||||
pageInfo[fullPath] = {
|
||||
local_path: localPath,
|
||||
absolute_path: fullPath,
|
||||
metadata: metadata,
|
||||
hash: sha256Hash,
|
||||
char_count: charCount,
|
||||
word_count: wordCount,
|
||||
path: fullPath
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return pageInfo;
|
||||
}
|
51
utils/pageupdater/update_pagelist.ts
Normal file
51
utils/pageupdater/update_pagelist.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import * as fs from 'node:fs';
|
||||
import * as pages from './pages';
|
||||
import { format } from 'date-fns';
|
||||
|
||||
// Type for metadata and page info
|
||||
export interface Page {
|
||||
metadata: pages.PageInfoMetdata;
|
||||
id: string;
|
||||
url: string;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
export interface PageList {
|
||||
last_generated: string;
|
||||
posts: Page[];
|
||||
}
|
||||
|
||||
// Function to generate the page list
|
||||
function generatePageList(pagesInfo: Record<string, any>): PageList {
|
||||
const pageList: Page[] = [];
|
||||
|
||||
for (const [path, page] of Object.entries(pagesInfo)) {
|
||||
const pageDict: Page = {
|
||||
metadata: page.metadata,
|
||||
id: page.local_path,
|
||||
url: page.absolute_path.replace("public", ""),
|
||||
hash: page.hash
|
||||
};
|
||||
pageList.push(pageDict);
|
||||
}
|
||||
|
||||
pageList.forEach(page => {
|
||||
if (page.metadata.date) {
|
||||
page.metadata.date = format(new Date(page.metadata.date), 'yyyy-MM-dd HH:mm:ss');
|
||||
}
|
||||
});
|
||||
|
||||
const pageListDict: PageList = {
|
||||
last_generated: format(new Date(), 'yyyy-MM-dd HH:mm:ss'),
|
||||
posts: pageList
|
||||
};
|
||||
|
||||
return pageListDict;
|
||||
}
|
||||
|
||||
// Get the page list and print it
|
||||
const postList = generatePageList(pages.getPagesInfo("", "public/blog"));
|
||||
console.log(JSON.stringify(postList, null, 2));
|
||||
|
||||
// Output to assets/blog_list.json (overwriting)
|
||||
fs.writeFileSync("assets/meta/blog_list.json", JSON.stringify(postList, null, 2));
|
Loading…
Add table
Add a link
Reference in a new issue