BlogSharer/blog.js

52 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-07-07 22:32:52 -05:00
const path = require("path"),
paths = require("./fileManager.js"),
fs = require("fs")
const defaultConfig = {
"title": "Bruh!",
"public": false,
"hidden": true,
"pinned": false,
"desc": "No description... pluh!"
}
module.exports = {
blogPosts: () => {
var posts = []
2024-07-07 23:31:01 -05:00
var pinned = []
2024-07-07 22:32:52 -05:00
var dir = fs.readdirSync(paths.posts)
dir.sort(function(a, b) {
return fs.statSync(paths.posts + "/" + a).mtime.getTime() -
fs.statSync(paths.posts + "/" + b).mtime.getTime();
});
for (var i = 0; i < dir.length; i++) {
var folder = dir[i]
var folderPath = path.join(paths.posts, folder)
var configPath = path.join(folderPath, "config.json")
if (fs.statSync(folderPath).isDirectory) {
var config = {}
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath))
}
config = Object.assign({}, defaultConfig, config)
config.folder = folder
if (config.public) {
config.data = fs.readFileSync(path.join(folderPath, "index.md")).toString()
} else if (config.hidden) {
continue
}
if (config.pinned) {
2024-07-07 23:27:13 -05:00
pinned.unshift(config)
2024-07-07 23:18:43 -05:00
} else {
posts.unshift(config)
2024-07-07 22:32:52 -05:00
}
}
}
2024-07-07 23:29:34 -05:00
return pinned.concat(posts)
2024-07-07 22:32:52 -05:00
}
2024-07-07 23:29:55 -05:00
}