-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (82 loc) · 3.02 KB
/
index.js
File metadata and controls
97 lines (82 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const path = require('path');
const { readingTime } = require('reading-time-estimator');
function scanDirectory(directoryPath, filesArray, routesArray) {
const files = fs.readdirSync(directoryPath);
files.forEach(async file => {
const filePath = path.join(directoryPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Recursively scan subdirectories
scanDirectory(filePath, filesArray, routesArray);
} else if (file === 'post.md') {
// If the file is named "meta.json", read its content and add it to the array
const fileContent = fs.readFileSync(filePath, 'utf8');
const metaData = fileContent.split('---')[0];
filesArray.push({
title: metaData.match(/^# ([^\n]+)/m)?.[1],
excerpt: metaData.match(/^#[^\n]+\n+([^\n]+)/s)?.[1],
teaser: metaData.match(/^\!\[[^\(]+\(([^\)]+)/im)?.[1],
authors: metaData
.match(/^Authors: ([^\n]+)/im)?.[1]
?.split(',')
.map(n => n.trim()),
category: metaData.match(/^Category: ([^\n]+)/im)?.[1],
tags: metaData
.match(/^Tags: ([^\n]+)/im)?.[1]
?.split(',')
.map(n => n.trim()),
date: metaData.match(/^Date: ([^\n]+)/im)?.[1],
readingTime: readingTime(fileContent, 238).text,
});
routesArray.push(directoryPath.replace('content/posts', ''));
}
});
// Sort the array based on the "date" field
filesArray.sort((a, b) =>
a.date && a.date !== 'unpublished'
? new Date(b.date) - new Date(a.date)
: -1
);
}
async function getAuthorRoutes(source) {
if (fs.existsSync(source)) {
const authors = JSON.parse(fs.readFileSync(source, 'utf8'));
const utils = await loadEsmModule(
'../../dist/utils/esm2022/lib/permalink.mjs'
);
return Object.keys(authors).map(name =>
`/people/${utils.getAuthorPermalink(name)}`);
}
return [];
}
async function main() {
const startDirectory = 'content/posts'; // Change this to the starting directory path
const outputFilePath = 'content/posts/posts.json'; // Change this to the desired output file path
const authorsFilePath = 'content/authors/authors.json';
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
const filesArray = [];
const routesArray = [];
scanDirectory(startDirectory, filesArray, routesArray);
// Write the sorted array to the output file
const outputContent = JSON.stringify(filesArray);
fs.writeFileSync(outputFilePath, outputContent, 'utf8');
routesArray.push(
'/category/tech-life',
'/category/devops',
'/category/backend',
'/category/career',
'/category/frontend',
'/category/sdlc',
'/404',
...(await getAuthorRoutes(authorsFilePath))
);
fs.writeFileSync('dist/routes.txt', routesArray.join('\r\n'), 'utf8');
console.log(`Scanning completed. Output written to ${outputFilePath}`);
}
main();
function loadEsmModule(modulePath) {
return new Function('modulePath', `return import(modulePath);`)(modulePath);
}