headerLevelStart (for showdownjs) and some comments

This commit is contained in:
deadvey 2025-07-13 02:48:15 +01:00
parent 4fb12c54f8
commit 3efe4b7836
2 changed files with 23 additions and 21 deletions

42
app.js
View File

@ -3,11 +3,11 @@ const fs = require('fs'); // For modifying and reading files
const express = require('express'); // For running a webserver in nodejs
const showdown = require('showdown') // For converting markdown to html on demand, https://showdownjs.com/
const crypto = require('crypto'); // For encrypting passwords, I use sha512
// fromUnixTime creates a date object out of an integer
// format is used to format a date object into a defined format eg yyyy-MM-dd
// getUnixTime converts a date object into an integer (unix time)
// find out more at https://date-fns.org/ \or docs: https://date-fns.org/docs/Getting-Started
const { fromUnixTime, format, getUnixTime } = require("date-fns")
// fromUnixTime(): Create a date from a Unix timestamp (in seconds). Decimal values will be discarded.
// format(): Return the formatted date string in the given format. The result may vary by locale.
// getUnixTime(): Get the seconds timestamp of the given date.
// find out more at https://date-fns.org/ \or docs: https://date-fns.org/docs/Getting-Startedyyyyyyyy
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
// There's only one possible argument, so we can just check if the user passed that one
// TODO I plan on adding more such as --help and --post so I should make this more robust at some point
@ -17,7 +17,7 @@ if (process.argv[2] == "--first-time") {
// Define the modules now so they are global
let users // contains a list of users, each user is an object containing username,prettyname,hash and description
let posts // contains a list of posts,
let posts // contains a list of posts,
let comments
let config
@ -42,7 +42,8 @@ let converter = new showdown.Converter({
tables: true, // Enable support for tables syntax.
strikethrough: true, // Enable support for strikethrough: ~~text~~
tasklists: true, // Enable support for GitHub style tasklists. - [x] and - [ ]
encodeEmails: true //Enable automatic obfuscation of email addresses. emails are encoded via character entities
encodeEmails: true, //Enable automatic obfuscation of email addresses. emails are encoded via character entities
headerLevelStart: 3, //Set starting level for the heading tags.
})
// The footer div is globale because it's a site wide, so define it here
@ -109,20 +110,21 @@ function get_userID(username) {
return -1 // If user is not present, return -1
}
// The configuration defines a date format using the date-fns syntax
// The configuration defines a date format using the date-fns (a datetime library) syntax
// eg "yyyy-MM-dd"
// this converts unix time (an integer) into a string that is formatted according to config.js
// uses date-fns's fromUnixTime() and format()
// uses date-fns's fromUnixTime() and format() functions
// returns the formatted date (string)
function unix_time_to_date_format(unix_time) {
date = fromUnixTime(unix_time)
formatted_date = format(date, config.date_format)
return formatted_date
}
// This is similar to the above function, however, instead of formatting to the users
// configuration, it formats to RFC-822 which is the date format used by RSS feeds
// eg "Mon, 23 May 2025 18:59:59 +0100"
// accepts unix time (int)
// returns the formatted date (string)
function unix_time_to_rss_date(unix_time) {
date = fromUnixTime(unix_time)
@ -132,7 +134,7 @@ function unix_time_to_rss_date(unix_time) {
// This function accepts a list of strings eg ["string1","string2,"string3"] (any length)
// then returns a string of them each pointing to a seperate url
// eg "<a href="/tag/string1">string1</a>, <a href="/tag/string2">string2</a>, <a href="/tag/string3">string3</a>
// eg "<a href="/tag/string1">string1</a>, <a href="/tag/string2">string2</a>, <a href="/tag/string3">string3</a>"
// this is so you can have a list of tags that each point to their individual tag page
// returns: string
function hyperlink_tags(tags) {
@ -276,7 +278,7 @@ app.get("/", (req,res) => {
counter -= 1;
}
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body><footer>${footer_div}</footer></html>`);
});
}); // /
app.get("/user/:username", (req, res) => {
header_div = config.user_page_header
header_div = replace_format_indicators(header_div)
@ -288,13 +290,13 @@ app.get("/user/:username", (req, res) => {
}
}
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body><footer>${footer_div}</footer></html>`);
});
}); // /user/:username
app.get("/post/:post_index", (req, res) => {
post_div = "";
let post = config.post_page_format;
post_div += replace_format_indicators(post, req.params.post_index);
res.send(`<html><head><meta charset="${config.charset}"><style>${config.css}</style></head><body><div id="posts">${post_div}</div></body><footer>${footer_div}</footer></html>`);
});
}); // /post/:post_index
app.get("/tag/:tag", (req,res) => {
const tag = req.params.tag
let header_div = config.tag_page_header
@ -307,7 +309,7 @@ app.get("/tag/:tag", (req,res) => {
};
};
res.send(`<html><style>${config.css}</style><body><div id="header">${header_div}</div><div id="posts">${page_content}</div></body><footer>${footer_div}</footer></html>`);
});
}); // /tag/:tag
app.get("/post", (req,res) => {
res.send(`</html><head><meta charset="${config.charset}"><style>${config.css}</style></head><form action="/submit_post" method="POST">
@ -319,7 +321,7 @@ app.get("/post", (req,res) => {
<input type="submit" value="Submit"><br/>
<small>* Markdown supported</small>
</form></html>`);
});
}); // /post
app.get("/edit/:post_id", (req,res) => {
const post_id = req.params.post_id
const post = posts.posts[post_id]
@ -336,7 +338,7 @@ app.get("/edit/:post_id", (req,res) => {
<input type="submit" value="Submit"><br/>
<small>* Markdown supported</small>
</form></html>`);
});
}); // /edit/:post_id
app.post("/submit_comment", (req,res) => {
const unix_timestamp = getUnixTime(new Date())
@ -355,7 +357,7 @@ app.post("/submit_comment", (req,res) => {
fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${counter}`, 'utf-8');
res.redirect(301,`/post/${req.body.post_index}`)
});
}); // /submit_comment
app.post("/submit_edit", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const postID = req.body.postID
@ -385,7 +387,7 @@ app.post("/submit_edit", (req,res) => {
else {
res.send(`Invalid Password for user`,users.users[userID]['prettyname']);
}
});
}); // /submit_edit
app.post("/submit_post", (req,res) => {
const password = crypto.createHash('sha512').update(req.body.password).digest('hex');
const username = escape_input(req.body.username)
@ -416,7 +418,7 @@ app.post("/submit_post", (req,res) => {
else {
res.send(`Invalid Password for user`,username);
}
});
}); // /submit_post
app.listen(config.port, () => {
console.log(`Server is running at http://localhost:${config.port} in ${config.root_path}`);

View File

@ -1 +1 @@
253
259