const fs = require('fs'); const express = require('express'); const showdown = require('showdown') const crypto = require('crypto'); // For encrypting passwords const { fromUnixTime, format, getUnixTime } = require("date-fns") if (process.argv[2] == "--first-time") { initialise() } let users let posts let comments let config try { users = require('./users.js'); posts = require('./posts.js'); comments = require('./comments.js'); config = require('./config.js'); } catch (error) { console.log("A file is missing!") console.log("Run with --first-time to initialise the program") console.log(error) process.exit(1) } let converter = new showdown.Converter({ simpleLineBreaks: true, tables: true, strikethrough: true, tasklists: true, encodeEmails: true }) const app = express(); let footer_div = config.site_wide_footer footer_div = replace_format_indicators(footer_div) app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(express.static(config.root_path)); function initialise() { try { const users = require("./users.js"); } catch (error) { console.log("Creating users file") fs.writeFileSync(`${__dirname}/users.js`, `export const users = []`) } try { const posts = require("./posts.js"); } catch (error) { console.log("Creating posts file") fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = []`) } try { const comments = require("./comments.js"); } catch (error) { console.log("Creating comments file") fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = []\nexport const counter = 0`) } try { const config = require("./config.js"); } catch (error) { console.log("Copying the example config to config.js") console.log("!!! PLEASE MODIFY config.js TO YOUR NEEDS !!!") fs.copyFile('example-config.js', 'config.js', (err) => { console.log("Error copying file") }) } process.exit(0) } function get_userID(username) { for (let i = 0; i < users.users.length; i++) { if (users.users[i]['username'] == username) { return i } } return -1 } function unix_time_to_date_format(unix_time) { date = fromUnixTime(unix_time) formatted_date = format(date, config.date_format) return formatted_date } function unix_time_to_rss_date(unix_time) { date = fromUnixTime(unix_time) formatted_date = format(date, "EEE, dd MMM yyyy HH:mm:ss") return `${formatted_date} ${config.time_zone}` } function hyperlink_tags(tags) { string = "" for (let tag_index = 0; tag_index < tags.length; tag_index++) { string += `${tags[tag_index]}` if (tag_index < tags.length - 1) { string += ", "; } } return string } function replace_format_indicators(input_string, post_index=0, tag_name="tag") { post_object = posts.posts[post_index] output_string = input_string .replaceAll("%%", "%") .replaceAll("%A", (post_object["tags"])) .replaceAll("%B", (hyperlink_tags(post_object["tags"]))) .replaceAll("%C", converter.makeHtml(post_object["content"])) .replaceAll("%D", unix_time_to_date_format(post_object["pubdate"])) .replaceAll("%E", unix_time_to_date_format(post_object["editdate"])) .replaceAll("%F", users.users[post_object["userID"]]['prettyname']) .replaceAll("%G", tag_name) .replaceAll("%I", users.users[post_object['userID']]['description']) .replaceAll("%L", `/post/${post_index}`) .replaceAll("%M", return_comments(post_index)) .replaceAll("%N", users.users[post_object["userID"]]['username']) .replaceAll("%P", "/post") .replaceAll("%O", `/edit/${post_index}`) .replaceAll("%R", "/rss") .replaceAll("%S", config.seperator) .replaceAll("%T", post_object["title"]) .replaceAll("%U", `/user/${users.users[post_object["userID"]]['username']}`) .replaceAll("%X", `
`) .replaceAll("%Y", config.site_name) .replaceAll("%W", config.site_description) .replaceAll("%Z", config.attribution) if (config.enable_hitcount == true) { output_string = output_string .replaceAll("%H", fs.readFileSync('hitcount.txt')) } return output_string } function escape_input(input) { let output = input .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll("\\", "\") .replaceAll('"', """) .replaceAll("'", "'") .replaceAll("/", "/") return output } // TODO make the formatting customisable function return_comments(post_id) { const post_comments = comments.comments[post_id] let comment_content = "" for (let comment_index = 0; comment_index < post_comments.length; comment_index++) { let comment = {...post_comments[comment_index]}; comment['content'] = comment['content'] .replaceAll(/>> ([0-9]*)/g, ">> $1") .replaceAll("\n", "