personal-site/utils/page_updater/pages.ts

107 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-01-03 21:50:12 -08:00
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';
2025-01-04 11:45:02 -08:00
export interface PageLocation {
title: string;
description: string;
tags: string[];
map: string;
root: string;
}
2025-01-03 21:50:12 -08:00
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;
}
2025-01-04 11:45:02 -08:00
export interface PageInfoCategory {
posts: PageInfo[];
title: string;
description: string;
tags: string[];
}
2025-01-03 21:50:12 -08:00
// 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
2025-01-04 11:45:02 -08:00
export function getPagesInfo(searchDirectory: string, pageLocation: PageLocation): Record<string, PageInfo> {
2025-01-03 21:50:12 -08:00
const pageInfo: Record<string, PageInfo> = {};
2025-01-04 11:45:02 -08:00
const currentDirectory = path.join(pageLocation.root, searchDirectory);
2025-01-03 21:50:12 -08:00
const files = fs.readdirSync(currentDirectory);
2025-01-04 11:45:02 -08:00
console.log(files);
2025-01-03 21:50:12 -08:00
files.forEach((file) => {
2025-01-04 13:03:03 -08:00
var fullPath = path.join(currentDirectory, file);
var localPath = fullPath.replace(pageLocation.root, pageLocation.map);
2025-01-04 11:45:02 -08:00
console.log(fullPath);
console.log(localPath);
2025-01-03 21:50:12 -08:00
if (fs.lstatSync(fullPath).isDirectory()) {
2025-01-04 11:45:02 -08:00
Object.assign(pageInfo, getPagesInfo(path.join(searchDirectory, file), pageLocation));
2025-01-03 21:50:12 -08:00
} else if (file.endsWith('.md')) {
2025-01-04 13:03:03 -08:00
2025-01-03 21:50:12 -08:00
const metadata = getMetadata(fullPath);
const sha256Hash = getSha256Hash(fullPath);
const charCount = getCharCount(fullPath);
const wordCount = getWordCount(fullPath);
2025-01-04 13:03:03 -08:00
// Remove the .md extension
localPath = localPath.replace('.md', '');
fullPath = fullPath.replace('.md', '');
2025-01-03 21:50:12 -08:00
pageInfo[fullPath] = {
local_path: localPath,
absolute_path: fullPath,
metadata: metadata,
hash: sha256Hash,
char_count: charCount,
word_count: wordCount,
path: fullPath
};
}
});
return pageInfo;
}