lots of fixes and more EJS
This commit is contained in:
71
src/functions.js
Normal file
71
src/functions.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import { createRequire } from 'module';
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
// 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() functions
|
||||
// returns the formatted date (string)
|
||||
export function unix_time_to_date_format(unix_time) {
|
||||
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||
const config = require("../config.json")
|
||||
let date = fromUnixTime(unix_time)
|
||||
let 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)
|
||||
export function unix_time_to_rss_date(unix_time) {
|
||||
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||
const config = require("../config.json")
|
||||
let date = fromUnixTime(unix_time)
|
||||
let formatted_date = format(date, "EEE, dd MMM yyyy HH:mm:ss")
|
||||
return `${formatted_date} ${config.time_zone}`
|
||||
}
|
||||
// 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>"
|
||||
// this is so you can have a list of tags that each point to their individual tag page
|
||||
// returns: string
|
||||
export function hyperlink_tags(tags) {
|
||||
let string = "" // Initialises the string
|
||||
for (let tag_index = 0; tag_index < tags.length; tag_index++) { // Loop over each tag
|
||||
string += `<a href="/tag/${tags[tag_index]}">${tags[tag_index]}</a>` // Adds the tag to the string as a HTML href
|
||||
if (tag_index < tags.length - 1) { // If there are more tags, then insert a comma
|
||||
string += ", ";
|
||||
}
|
||||
}
|
||||
return string
|
||||
}
|
||||
// The users are stored as a list of objects [ user_object, user_object, user_object ]
|
||||
// So you cannot easily find the userID (position in list) from the username
|
||||
// This function returns the username for a given userID by looping over every user
|
||||
// if the user is present, it returns the index of the user (integer)
|
||||
// if the user is not present it returns -1
|
||||
export function get_userID(username) {
|
||||
const users = require("../data/users.json")
|
||||
for (let i = 0; i < users.length; i++) { // Loop over every user
|
||||
if (users[i]['username'] == username) {
|
||||
return i // If the username matches then return the index of that user
|
||||
}
|
||||
}
|
||||
return -1 // If user is not present, return -1
|
||||
}
|
||||
// This escapes some potentially dangerous HTML characters with their HTML entities
|
||||
// https://www.freeformatter.com/html-entities.html
|
||||
// accepts a string
|
||||
// returns a string with some character replaced by their entities
|
||||
export function escape_input(input) {
|
||||
let output = input
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll("\\", "\")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'")
|
||||
.replaceAll("/", "/")
|
||||
.replaceAll("%", "%")
|
||||
return output
|
||||
}
|
Reference in New Issue
Block a user