forked from ourworld_web/www_engage_os
chore: add gray-matter dependency for markdown frontmatter parsing
This commit is contained in:
53
scripts/add-title-to-people.mjs
Normal file
53
scripts/add-title-to-people.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
|
||||
const peopleDirectory = path.join(process.cwd(), 'public/images/people');
|
||||
|
||||
async function updatePeopleFiles() {
|
||||
try {
|
||||
const peopleFolders = await fs.readdir(peopleDirectory, { withFileTypes: true });
|
||||
|
||||
for (const dirent of peopleFolders) {
|
||||
if (dirent.isDirectory()) {
|
||||
const personDir = path.join(peopleDirectory, dirent.name);
|
||||
const files = await fs.readdir(personDir);
|
||||
const markdownFile = files.find(file => file.endsWith('.md') || file.endsWith('.mdx'));
|
||||
|
||||
if (markdownFile) {
|
||||
const markdownFilePath = path.join(personDir, markdownFile);
|
||||
const fileContents = await fs.readFile(markdownFilePath, 'utf8');
|
||||
const { data, content } = matter(fileContents);
|
||||
|
||||
if (!data.title) {
|
||||
let title = data.excerpt || data.description || '';
|
||||
if (title.endsWith('.')) {
|
||||
title = title.slice(0, -1);
|
||||
}
|
||||
|
||||
if (data.title !== undefined) continue;
|
||||
|
||||
if (data.description) {
|
||||
data.name = data.title;
|
||||
data.excerpt = data.description;
|
||||
}
|
||||
data.title = title;
|
||||
|
||||
if(data.extra && data.extra.imgPath) {
|
||||
data.image = `./${data.extra.imgPath}`;
|
||||
delete data.extra.imgPath;
|
||||
}
|
||||
|
||||
const newContent = matter.stringify(content, data);
|
||||
await fs.writeFile(markdownFilePath, newContent);
|
||||
console.log(`Updated ${markdownFilePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating people files:', error);
|
||||
}
|
||||
}
|
||||
|
||||
updatePeopleFiles();
|
||||
Reference in New Issue
Block a user