Changed how comments are stored and how data is retrieved
This commit is contained in:
@@ -7,12 +7,15 @@ const locale = require(`../locales/${config.locale}.json`)
|
||||
// This function requires a module without caching it
|
||||
// So the server doesn't need to be restarted, though this can slow it down a bit.
|
||||
// https://stackoverflow.com/a/16060619
|
||||
export function require_module(module) {
|
||||
if (config.cache_data == false) {
|
||||
export function require_module(module)
|
||||
{
|
||||
if (config.cache_data == false)
|
||||
{
|
||||
delete require.cache[require.resolve(module)];
|
||||
return require(module);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return require(module);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +25,8 @@ export function require_module(module) {
|
||||
// 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) {
|
||||
export function unix_time_to_date_format(unix_time)
|
||||
{
|
||||
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||
let date = fromUnixTime(unix_time)
|
||||
let formatted_date = format(date, config.date_format)
|
||||
@@ -33,14 +37,16 @@ export function unix_time_to_date_format(unix_time) {
|
||||
// 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) {
|
||||
export function unix_time_to_rss_date(unix_time)
|
||||
{
|
||||
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||
let date = fromUnixTime(unix_time)
|
||||
let formatted_date = format(date, "EEE, dd MMM yyyy HH:mm:ss")
|
||||
return `${formatted_date} ${config.time_zone}`
|
||||
}
|
||||
// And again with atom's date format
|
||||
export function unix_time_to_atom_date(unix_time) {
|
||||
export function unix_time_to_atom_date(unix_time)
|
||||
{
|
||||
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
|
||||
let date = fromUnixTime(unix_time)
|
||||
let formatted_date = format(date, "yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||
@@ -51,13 +57,17 @@ export function unix_time_to_atom_date(unix_time) {
|
||||
// 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 render_tags(tags) {
|
||||
export function render_tags(tags)
|
||||
{
|
||||
let string = "" // Initialises the string
|
||||
if (tags.length == 1 && tags[0] == "") {
|
||||
if (tags.length == 1 && tags[0] == "")
|
||||
{
|
||||
string = ''; // If there are no tags, output nothing
|
||||
}
|
||||
else {
|
||||
for (let tag_index = 0; tag_index < tags.length; tag_index++) { // Loop over each tag
|
||||
else
|
||||
{
|
||||
for (let tag_index = 0; tag_index < tags.length; tag_index++)
|
||||
{ // Loop over each tag
|
||||
string += `<a href="/tag/${tags[tag_index].trim()}">#${tags[tag_index].trim()}</a> ` // Adds the tag to the string as a HTML href
|
||||
}
|
||||
}
|
||||
@@ -68,10 +78,13 @@ export function render_tags(tags) {
|
||||
// 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) {
|
||||
export function get_userID(username)
|
||||
{
|
||||
const users = require_module("../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
|
||||
}
|
||||
}
|
||||
@@ -82,7 +95,8 @@ export function get_userID(username) {
|
||||
// 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) {
|
||||
export function escape_input(input)
|
||||
{
|
||||
let output = input
|
||||
.replaceAll("&", "&") // This must be first
|
||||
.replaceAll("<", "<")
|
||||
@@ -97,7 +111,8 @@ export function escape_input(input) {
|
||||
|
||||
// Render comment content by replacing the >> int with a url link to that comment
|
||||
// Syntax: ">> postID-commentID"
|
||||
export function render_comment(comment_content) {
|
||||
export function render_comment(comment_content)
|
||||
{
|
||||
return comment_content
|
||||
.replaceAll(/>> ([0-9]*)-([0-9]*)/g, "<a href='/comment/$1-$2'>>> $1-$2</a>")
|
||||
.replaceAll(/>>([0-9]*)-([0-9]*)/g, "<a href='/comment/$1-$2'>>>$1-$2</a>")
|
||||
@@ -108,9 +123,11 @@ export function render_comment(comment_content) {
|
||||
};
|
||||
|
||||
// Renders a string into markdown using markdown-it library
|
||||
export function render_md(content) {
|
||||
export function render_md(content)
|
||||
{
|
||||
const markdownit = require("markdown-it")
|
||||
const md = markdownit({ // this is just defining some options for markdown-it, should I add this to config.json?
|
||||
const md = markdownit
|
||||
({ // this is just defining some options for markdown-it, should I add this to config.json?
|
||||
html: false,
|
||||
xhtmlOut: false,
|
||||
breaks: true,
|
||||
@@ -121,3 +138,11 @@ export function render_md(content) {
|
||||
return md.render(content)
|
||||
};
|
||||
|
||||
export function find_key_value_pair(data_array, key, value) {
|
||||
for (let i = 0; i < data_array.length; i++) {
|
||||
if (data_array[i][key] == value) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user